locked
Coding RRS feed

  • Question

  • User-1499457942 posted

    Hi

     I have below code . Is it possible that both condition should work and i define variable only once like cmd , sqldataadapter. Now i have define 2 different variables 

    using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("Sp_Employee", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Action", "S");
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
     
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        if (dt.Rows.Count == 0)
                        {
                            SqlCommand cmd0 = new SqlCommand("Sp_Employee", con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@Action", "I");
                            SqlDataAdapter sda0 = new SqlDataAdapter(cmd0);
     
                            DataTable dt0 = new DataTable();
                            sda0.Fill(dt0);
                            if (dt0.Rows.Count > 0)
                            {
                                gvwEmployee.DataSource = dt0;
                                gvwEmployee.DataBind();
                            }
                        }
                        if (dt.Rows.Count > 0)
                        {
                            gvwEmployee.DataSource = dt;
                            gvwEmployee.DataBind();
                        }
                        
                        dt.Clear();
                    }
    Thanks
    Saturday, November 17, 2018 6:17 AM

Answers

  • User283571144 posted

    Hi JagjitSingh,

    Is it possible that both condition should work and i define variable only once like cmd , sqldataadapter. Now i have define 2 different variables 

    Yes, we could just define one varible and reuse it in another condition, since the first query return null table.

    I suggest you could use  cmd.Parameters.Clear(); dt.Clear(); to clear the datatable outside the if you want to reuse them.

    More details, you could refer to below codes:

                using (SqlConnection con = new SqlConnection(@"yourconnectionstring"))
                {
                    SqlCommand cmd = new SqlCommand("Sp_Employee", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "S");
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        cmd.Parameters.Clear();
                        dt.Clear();
                        cmd.Parameters.AddWithValue("@Action", "I");
                        sda.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            gvwEmployee.DataSource = dt;
                            gvwEmployee.DataBind();
                        }
                    }
    
                    //if (dt.Rows.Count > 0)
                    //{
                    //    gvwEmployee.DataSource = dt;
                    //    gvwEmployee.DataBind();
                    //}
                    dt.Clear();
                }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2018 6:26 AM

All replies

  • User1120430333 posted

    JagjitSingh

    Hi

     I have below code . Is it possible that both condition should work and i define variable only once like cmd , sqldataadapter. Now i have define 2 different variables 

    <div>using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))</div> <div>                {</div> <div>                    SqlCommand cmd = new SqlCommand("Sp_Employee", con);</div> <div>                    cmd.CommandType = CommandType.StoredProcedure;</div> <div>                    cmd.Parameters.AddWithValue("@Action", "S");</div> <div>                    if (con.State == ConnectionState.Closed)</div> <div>                    {</div> <div>                        con.Open();</div> <div>                    }</div> <div>                    SqlDataAdapter sda = new SqlDataAdapter(cmd);</div> <div> </div> <div>                    DataTable dt = new DataTable();</div> <div>                    sda.Fill(dt);</div> <div>                    if (dt.Rows.Count == 0)</div> <div>                    {</div> <div>                        SqlCommand cmd0 = new SqlCommand("Sp_Employee", con);</div> <div>                        cmd0.CommandType = CommandType.StoredProcedure;</div> <div>                        cmd0.Parameters.AddWithValue("@Action", "I");</div> <div>                        SqlDataAdapter sda0 = new SqlDataAdapter(cmd0);</div> <div> </div> <div>                        DataTable dt0 = new DataTable();</div> <div>                        sda0.Fill(dt0);</div> <div>                        if (dt0.Rows.Count > 0)</div> <div>                        {</div> <div>                            gvwEmployee.DataSource = dt0;</div> <div>                            gvwEmployee.DataBind();</div> <div>                        }</div> <div>                    }</div> <div>                    if (dt.Rows.Count > 0)</div> <div>                    {</div> <div>                        gvwEmployee.DataSource = dt;</div> <div>                        gvwEmployee.DataBind();</div> <div>                    }</div> <div>                    </div> <div>                    dt.Clear();</div> <div>                }</div> <div></div> <div>Thanks</div>

    And you expect someone to read that? You can try formatting the HTML by using a formatted correctly HTML and then using the {;} icon on the toolbar that formats code for a post.

    Saturday, November 17, 2018 12:43 PM
  • User-1499457942 posted
    using (SqlConnection con = new SqlConnection(CommonFunction.connectionString))
                    {
                        SqlCommand cmd = new SqlCommand("Sp_Employee", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Action", "S");
                        if (con.State == ConnectionState.Closed)
                        {
                            con.Open();
                        }
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
    
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        if (dt.Rows.Count == 0)
                        {
                            SqlCommand cmd0 = new SqlCommand("Sp_Employee", con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@Action", "I");
                            SqlDataAdapter sda0 = new SqlDataAdapter(cmd0);
    
                            DataTable dt0 = new DataTable();
                            sda0.Fill(dt0);
                            if (dt0.Rows.Count > 0)
                            {
                                gvwEmployee.DataSource = dt0;
                                gvwEmployee.DataBind();
                            }
                        }
                        if (dt.Rows.Count > 0)
                        {
                            gvwEmployee.DataSource = dt;
                            gvwEmployee.DataBind();
                        }
                        dt.Clear();
                    }

    Saturday, November 17, 2018 1:02 PM
  • User839733648 posted

    Hi JagjitSingh,

    You could try to use cmd.Parameters.Clear() to clear the cmd and then add new value like below.

    SqlCommand cmd = new SqlCommand("Sp_Employee", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Action", "S");
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@Action", "I");
    

    But you need define another SqlDataAdapter for another action. And you could use dt.clear() to reuse the datatable.

    Best Regards,

    Jenifer

    Monday, November 19, 2018 7:07 AM
  • User283571144 posted

    Hi JagjitSingh,

    Is it possible that both condition should work and i define variable only once like cmd , sqldataadapter. Now i have define 2 different variables 

    Yes, we could just define one varible and reuse it in another condition, since the first query return null table.

    I suggest you could use  cmd.Parameters.Clear(); dt.Clear(); to clear the datatable outside the if you want to reuse them.

    More details, you could refer to below codes:

                using (SqlConnection con = new SqlConnection(@"yourconnectionstring"))
                {
                    SqlCommand cmd = new SqlCommand("Sp_Employee", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "S");
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        cmd.Parameters.Clear();
                        dt.Clear();
                        cmd.Parameters.AddWithValue("@Action", "I");
                        sda.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            gvwEmployee.DataSource = dt;
                            gvwEmployee.DataBind();
                        }
                    }
    
                    //if (dt.Rows.Count > 0)
                    //{
                    //    gvwEmployee.DataSource = dt;
                    //    gvwEmployee.DataBind();
                    //}
                    dt.Clear();
                }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 22, 2018 6:26 AM
  • User1281381861 posted

    Hi Jagjit,

    Just a humble request..

    Try to format your code via "<>" icon present in Editor while posting questions.

    And one more thing, try add specific title to your question. Title like "Coding" is not telling what exactly is the question for :)

    Thursday, November 22, 2018 7:00 AM