Click here to Skip to main content
15,173,031 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: (untagged)
hey all.. i need a guidence to my problem im facing
i want to display data in gridview binded to a sql datasource
but the problem part is i want to bind a field of that datasource to a dropdownlist and when a item is selected in that dropdownlist then correcposnding to that data will be displayed in gridview...
i have done the binding of datasource and gridview... but not of cropdownlist...plz help buddies... i'll be very grateful ... thanks in advance :)
here is my code which im using
<br />protected void  DropDownList1_SelectedIndexChanged(object sender, EventArgs e)<br />    {  ///int count=0;<br />        con = new SqlConnection("Data Source=.;Initial Catalog=abc;Integrated Security=True");<br /><br /><br />        <br />        <br />            con = new SqlConnection("Data Source=.;Initial Catalog=abc;Integrated Security=True");<br />            cmd = new SqlCommand("select * from salescounts where ='"+DropDownList1.Text+"'", con);<br />            con.Open();<br />            SqlDataReader dr = cmd.ExecuteReader();<br />            if (dr.HasRows)<br />            {<br />                GridView1.DataSource = dr;<br />                GridView1.DataBind();<br />                Label1.Text = "Result found:" + GridView1.Rows.Count;<br /><br /><br />            }<br />            else<br />            {<br />                Label1.Text = "not found";<br />            }<br /><br />            con.Close();<br />        <br />}<br />


modified on Sunday, July 26, 2009 12:51 PM
Posted

1 solution

Just Try this one

protected void  DropDownList1_SelectedIndexChanged(object sender, EventArgs e)<br />    {  <br />       DataSet Ds=new DataSet();<br />        con = new SqlConnection("Data Source=.;Initial Catalog=abc;Integrated Security=True");<br />        cmd = new SqlCommand("select * from salescounts where ='"+DropDownList1.Text+"'", con);<br />        SqlDataAdapter DA = new SqlDataAdapter(cmd);<br />                DA.Fill(ds);  <br />          if(Ds.Tables[0].Rows.Count>0)<br />           {<br />                GridView1.DataSource = ds;<br />                GridView1.DataBind();<br />                Label1.Text = "Result found:" + GridView1.Rows.Count;<br /><br />            }<br />            else<br />            {<br />                Label1.Text = "not found";<br />            }<br /><br />            con.Close();<br />        <br />}


Quick Suggestion :

solo_gaurav wrote:
cmd = new SqlCommand("select * from salescounts where ='"+DropDownList1.Text+"'", con);


Never Execute SQL Query like this from UI level. It will causes SQL Injection. Always used Parameterized query / Stored Procedure .

Thansk :-D

   



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