Databinding in dropdownlist from database in asp.net with C#.NET
I gave four columns in a table in the next page i take one dropdownlist and three textboxes if select name in dropdownlist the other should be come in textboxes
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jul 19, 2011, 9:43 AM
First Enable AutoPostBack of DropDownList to true after that in Selected_index changed event
using System.Data.SqlClient;
SqlConnection con=new SqlConnection("connection string");
SqlCommand com;
string str;
void Page_Load()
{
if(!IsPostBack)
{
con.Open();
str = "select * from student";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
dropdownlist.Items.Add(reader["name"]);
}
reader.Close();
con.Close();
}
}
void DropDownList1_SelectedIndexChanged()
{
con.Open();
str = "select * from student where name='" + DropDownList1.SelectedValue + "'";
com = new SqlCommand(str, con);
SqlDataReader reader=com.ExecuteReader();
if( reader.Read())
{
textBox1.Text = reader["id"].ToString();
textBox2.Text = reader["age"].ToString();
textBox3.Text = reader["marks"].ToString();
textBox4.Text = reader["address"].ToString();
}
con.Close();
reader.Close();
}
}
If this help you mark it as Answer.
belly hitsPosted Jul 19, 2011, 1:50 AM
Here is a little help for you, may be it work.
Creating an editable GridView whose data comes from a SqlDataSource is amazingly easy. To start, the GridView's SqlDataSource must contain an UpdateCommand with appropriate parameters. Creating such a SqlDataSource is as simple as checking the Generate Insert, Update, and Delete Statements from the dialog box(Recall that this dialog box is accessed through the second step of the SqlDataSource wizard, by clicking the Advanced button.)
You are doing an exciting practice, let me know what you get out of it.
Krishna GaradPosted Jun 21, 2011, 6:43 AM
For your requirment first bind the Dropdownlist with values while page is loading and next in dropdown selected index changed event write the code to selected value and get the values for other textboxes.