locked
Unreachable code detected warning RRS feed

  • Question

  • User-807418713 posted

    Hello

    This is my code

     private DataTable Date_Pass_Filter_Gridview()
        {
            DataTable dt = new DataTable();
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            con1.Open();
            SqlCommand cmd1 = new SqlCommand("DECLARE @return_value int EXEC @return_value = [dbo].[LFilter] @FD ='" + FromDate.Text + "',@TD ='" + ToDate.Text + "',@CN='" + Dropdownlist1.Text + "'", con1);
            cmd1.CommandTimeout = 90000;
            SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
            ada1.Fill(dt);
            return dt;        
            ada1.Dispose();
            cmd1.Dispose();
            con1.Close();
    
        }

    Is that any error in the above code so that its coming Unreachable code detected warning

    Friday, October 12, 2018 10:06 AM

Answers

  • User1120430333 posted

    return dt;

    It should be the last line in the method. You put the 'return' before any lines of code then that code is unreachable and will not be executed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 12, 2018 10:20 AM
  • User-821857111 posted

    I have to remove this 

    retrun dt; right

    No. You have to move that line so that it is the last line in the method:

    private DataTable Date_Pass_Filter_Gridview()
    {
            DataTable dt = new DataTable();
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            con1.Open();
            SqlCommand cmd1 = new SqlCommand("DECLARE @return_value int EXEC @return_value = [dbo].[LFilter] @FD ='" + FromDate.Text + "',@TD ='" + ToDate.Text + "',@CN='" + Dropdownlist1.Text + "'", con1);
            cmd1.CommandTimeout = 90000;
            SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
            ada1.Fill(dt);
                    
            ada1.Dispose();
            cmd1.Dispose();
            con1.Close();
    
        return dt;
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 12, 2018 11:40 AM
  • User-893317190 posted

    Hi Gopi.MCA,

    If you want to release the resource , you could also use the keyword using in your code.It will automatically  release the resource.

     private DataTable Date_Pass_Filter_Gridview()
        {
    
     DataTable dt = new DataTable();
                using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    con1.Open();
                    using (SqlCommand cmd1 = new SqlCommand("DECLARE @return_value int EXEC @return_value = [dbo].[LFilter] @FD ='" + FromDate.Text + "',@TD ='" + ToDate.Text + "',@CN='" + Dropdownlist1.Text + "'", con1))
                    {
                        cmd1.CommandTimeout = 90000;
                        using (SqlDataAdapter ada1 = new SqlDataAdapter(cmd1))
                        {
                            ada1.Fill(dt);
                            return dt;
                        } 
                       
                       
                    }
    
        }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2018 6:57 AM

All replies

  • User1120430333 posted

    return dt;

    It should be the last line in the method. You put the 'return' before any lines of code then that code is unreachable and will not be executed.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 12, 2018 10:20 AM
  • User-807418713 posted

    hello

    thanks for your reply

    I have to remove this 

    retrun dt; right?

    Friday, October 12, 2018 10:34 AM
  • User-821857111 posted

    I have to remove this 

    retrun dt; right

    No. You have to move that line so that it is the last line in the method:

    private DataTable Date_Pass_Filter_Gridview()
    {
            DataTable dt = new DataTable();
            SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            con1.Open();
            SqlCommand cmd1 = new SqlCommand("DECLARE @return_value int EXEC @return_value = [dbo].[LFilter] @FD ='" + FromDate.Text + "',@TD ='" + ToDate.Text + "',@CN='" + Dropdownlist1.Text + "'", con1);
            cmd1.CommandTimeout = 90000;
            SqlDataAdapter ada1 = new SqlDataAdapter(cmd1);
            ada1.Fill(dt);
                    
            ada1.Dispose();
            cmd1.Dispose();
            con1.Close();
    
        return dt;
    
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 12, 2018 11:40 AM
  • User-893317190 posted

    Hi Gopi.MCA,

    If you want to release the resource , you could also use the keyword using in your code.It will automatically  release the resource.

     private DataTable Date_Pass_Filter_Gridview()
        {
    
     DataTable dt = new DataTable();
                using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    con1.Open();
                    using (SqlCommand cmd1 = new SqlCommand("DECLARE @return_value int EXEC @return_value = [dbo].[LFilter] @FD ='" + FromDate.Text + "',@TD ='" + ToDate.Text + "',@CN='" + Dropdownlist1.Text + "'", con1))
                    {
                        cmd1.CommandTimeout = 90000;
                        using (SqlDataAdapter ada1 = new SqlDataAdapter(cmd1))
                        {
                            ada1.Fill(dt);
                            return dt;
                        } 
                       
                       
                    }
    
        }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2018 6:57 AM