简体   繁体   中英

Problem using return() in asp.net c#

    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

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.
}

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.

Put return casetype1; after ending braces of catch section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM