0
    string casetype6(HiddenField HiddenField1,DropDownList DropDownList3)
        {
            String casetype1="";

            try
            {
                OdbcConnection casetype = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=10.155.160.130;Database=testcase;User=root;Password=;Option=3;");
                casetype.Open();

                //************to get case type    
                string casetypequery = "select casename from casetype where skey=?";

                //************to get case type
                OdbcCommand casetypecmd = new OdbcCommand(casetypequery, casetype);
                String casetypefromdropdown = DropDownList3.SelectedItem.ToString();
                casetypecmd.Parameters.AddWithValue("?", casetypefromdropdown);
                using (OdbcDataReader casetypeMyReader = casetypecmd.ExecuteReader())
                {
                    while (casetypeMyReader.Read())
                    {
                        String casename = casetypeMyReader["casename"].ToString();
                        HiddenField1.Value = casename;
                        casetype1=HiddenField1.Value.ToString();
return casetype1;
                    }
                }

            }
            catch(Exception ep)
            { }
        }

I want to use casetype1 outside this method. How can i do it? If i say return casetype1 then error comes as:

    'Data.casetype6(System.Web.UI.WebControls.HiddenField, System.Web.UI.WebControls.DropDownList)': not all code paths return a value
1

3 Answers 3

3

Your method doesn't return anything if somehow you don't enter the while loop (in case your casetypeMyReader is empty) or some exception is thrown (connection failed to database). Following is changed code. Have a look.

string casetype6(HiddenField HiddenField1,DropDownList DropDownList3)
{
    String casetype1="";

    try
    {
        OdbcConnection casetype = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=10.155.160.130;Database=testcase;User=root;Password=;Option=3;");
        casetype.Open();

        //************to get case type    
        string casetypequery = "select casename from casetype where skey=?";

        //************to get case type
        OdbcCommand casetypecmd = new OdbcCommand(casetypequery, casetype);
        String casetypefromdropdown = DropDownList3.SelectedItem.ToString();
        casetypecmd.Parameters.AddWithValue("?", casetypefromdropdown);
        using (OdbcDataReader casetypeMyReader = casetypecmd.ExecuteReader())
        {
            while (casetypeMyReader.Read())
            {
                String casename = casetypeMyReader["casename"].ToString();
                HiddenField1.Value = casename;
                casetype1 = HiddenField1.Value.ToString();
                break; // instead of returning from here, break the loop
            }
        }

    }
    catch(Exception ep)
    { }

    return casetype1; // and return here.
}
Sign up to request clarification or add additional context in comments.

Comments

1

Put return casetype1; after ending braces of catch section.

Comments

1

You need to add something like return string.Empty, or preferably do something with the exception in your catch block.

There is always debate on where to put the return, be it at the end of the method, or at the point where you want to return. I personally like to return where the value can be returned, and then deal with the other problems where they need to be dealt with - in this instance, the catch block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.