24
Binding a web control to data
l We examined the HTML + controls in this application (the Webform1.aspx page), and the the actual HTML sent to the browser.  We will now examine the code behind the HTML + controls in the page (Webform1.aspx.cs) by acutally running Visual Studio .NET (not in these slides), and will use the visual interface to change properties of a control.  We will see that this code attaches data to the controls in the page as follows:
l
l// create the dataset to hold the data
l System.Data.DataSet oDS=new System.Data.DataSet();
l// connect to the database
l System.Data.SqlClient.SqlConnection oSqlClient=new
l    System.Data.SqlClient.SqlConnection(“DB connect string”);
l// create the query to be run
l System.Data.SqlClient.SqlDataAdapter oSqlDA=new
l   System.Data.SqlClient.SqlDataAdapter(“sql query”,oSqlClient);
l oDS.Clear();   /* optional – only needed if previously filled */
l// run the query to fill up a table in the data set
l oSqlDA.Fill(oDS,“table_name");
l// tell the control which table in the data set is connected to the control
l oGrid.DataSource = oDS.Tables["table_name"];
l// load the data into the control
l oGrid.DataBind();