In this Tutorial we are setting up values from database table to dropdownlist present on aspx page using SqlDataAdapter and DataSet.
SqlDataAdapter Represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database.
DataSet is a collection of data. Most commonly a dataset corresponds to the contents of a single database table, or a single statistical data matrix, where each column of the table represents a particular variable, and each row corresponds to a given member of the dataset in question.
Here we have just included the main code that is database connectivity which is available to our dropdownlist on Page_Load event. which means that when the aspx page will get loaded it will be loaded with all the entries from Database.mdf's Table1's column Name.
Page_Load Event code on aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;
User Instance=True");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT (Name_id),(Name) from [Table1]", conn);
SqlDataAdapter dadapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dadapter.Fill(ds);
conn.Close();
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataValueField = "Name_id";
DropDownList1.DataTextField = "Name";
DropDownList1.DataBind();
}
Also Read How to bind data to DorpDownList & TextBox using SqlDataReader
Also Read How to compare, select and bind data from Database Table into DropDownList & TextBox using SqlDataAdapter and DataSet
No comments:
Post a Comment