Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have four DropDownList in Asp.Net. So I want to send values selected from the DropDownLists as four parameters to my stored procedure in Sql server and when I hit the Submit button, I want to get the data back from Sql Server to Asp.Net and publish it on a GridView. I have tried some codes. Since I am fairly new in Asp.Net, so I am not sure what is wrong with my code below.

What I have tried:

C#
protected void Button1_Click1(object sender, EventArgs e)
{
    string s = "Submit";
    SqlCommand cmd = new SqlCommand(s, con);
    DataSet ds = new DataSet();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@Address", SqlDbType.VarChar).Value = DropDownList5.SelectedItem.Value;
    cmd.Parameters.Add("@Number", SqlDbType.VarChar).Value = DropDownList2.SelectedItem.Value;
    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = DropDownList3.SelectedItem.Value;
    cmd.Parameters.Add("@Type", SqlDbType.VarChar).Value = DropDownList4.SelectedItem.Value;
    con.Open();
    cmd.ExecuteNonQuery();
    GridView1.DataSource = ds;
    GridView1.DataBind();
    con.Close();
}
Posted
Updated 19-Jun-18 5:55am
v3

1 solution

Replace your cmd.ExecuteNonQuery() by below and it should work. ExecuteNonQuery will execute the query but no data return instead it will return no of record affected and should be used to insert/delete/update statements only.

C#
//cmd.ExecuteNonQuery(); 
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);

GridView1.DataSource = ds.Table[0];
GridView1.DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900