0
        protected string getEmailAddr()
        {
            SqlConnection conGet = new SqlConnection(ConfigurationManager.ConnectionStrings["connMSJ"].ConnectionString);
            SqlCommand cmdGet = new SqlCommand("SELECT Email FROM Member WHERE Username=@username", conGet);
            cmdGet.Parameters.AddWithValue("@username", strUsername);
            conGet.Open();
            SqlDataReader dtrGet = cmdGet.ExecuteReader();
            if (dtrGet.Read())
                return dtrGet[0].ToString();
            else
                return null;
            dtrGet.Close();
            conGet.Close();
        }

dtrGet.Close(); Occured the Unreachable Code Detected with Green Colour Underline.
How can i avoid this happen even though there is no problem for me to continue execute my webpage,
But it just seem not nice in the code behind. :P

2
Contributors
3
Replies
9
Views
3 Years
Discussion Span
Last Post by pritaeas
1

Unreachable because it's after return. So capture the result/null in a variable and return it at the end of the function, or use a try/finally.

0

i did tried used the try and finally method enclosed the entire code,
but the error said that the dtrGet.Close cannot be found?
I though is should be okay if i enclose the entire close in Try then finally with Close.?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.