Click here to Skip to main content
13,737,216 members
Rate this:
 
Please Sign up or sign in to 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:

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 14-Jun-18 7:38am
Updated 19-Jun-18 5:55am
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.

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

GridView1.DataSource = ds.Table[0];
GridView1.DataBind();
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180920.1 | Last Updated 19 Jun 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100