Boris Vainrub
Boris Vainrub

Reputation: 13

Call multiple stored procedure to the 2 different gridview in ASP.NET

I have two stored procedures:

  1. spRO_Status_OrderInfo
  2. spRO_Status_Change

Each procedure returns a different table. I have only one user input parameter TN. This parameter I'm reading from search txt line (inputTNReturn.Value)

Each procedure posts data in a different grid view

  1. spRO_Status_OrderInfo -->> gvSearchResults
  2. spRO_Status_Change -->> GridView1

This code returns only LAST stored procedure to last gridView

  1. spRO_Status_Change -->> GridView1

I don't have any idea where is my mistake and why I can get 2 grid view from 2 procedure.

I'll appreciate for any help

protected void btnSearch_Click(object sender, EventArgs e)
{
    string connectionStr = ConfigurationManager
            .ConnectionStrings["ConnectionString"].ConnectionString;

    using (SqlConnection con = new SqlConnection(connectionStr))
    {
            con.Open();

            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "[spRO_Status_OrderInfo]";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.CommandText = "spRO_Status_Change";
                cmd.CommandType = CommandType.StoredProcedure;

                if (inputTNReturn.Value.Trim() != "")
                {
                    SqlParameter param = new SqlParameter("@TN", inputTNReturn.Value);
                    cmd.Parameters.Add(param);

                    SqlDataReader rdr = cmd.ExecuteReader();

                    gvSearchResults.DataSource = rdr;
                    gvSearchResults.DataBind();

                    GridView1.DataSource = rdr;
                    GridView1.DataBind();

                    lblinputTNReturn.Text = inputTNReturn.Value;
                }
            }

            if (inputTNReturn.Value == "")
            {
                inputTNReturn.Value = "Please add tracking number";
            }

            con.Close();
        }
}

Upvotes: 0

Views: 585

Answers (1)

Alexander Johansen
Alexander Johansen

Reputation: 11

Here is where you're setting the first stored procedure

cmd.CommandText = "[spRO_Status_OrderInfo]";    
cmd.CommandType = CommandType.StoredProcedure;

Here is where you're overwriting the above

cmd.CommandText = "spRO_Status_Change";    
cmd.CommandType = CommandType.StoredProcedure;

You're not adding a second command text / type, you're just over writing the first one which is why you're only seeing the second one come out in the output.

Upvotes: 1

Related Questions