Is this the best way to write DataSet, DataAdapter etc... code to populate TextBox and dropdown list

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all,
I have a bunch of textBoxes, dropDownLists etc on a webform.
I have coded the pageload_event to populate these objects through a DataSet, DataAdapter etc. Code below:
<pre class="prettyprint protected void Page_Load(object sender, EventArgs e)
{
//ADO.NET SET-UP
//create a new sql connection
SqlConnection sqlConn = new SqlConnection("Data Source=ukfil239win;Initial Catalog=FILPublishingSolutions;Integrated Security=True");
//Open the new connection
sqlConn.Open();
//set up the sql query+command
string query = "SELECT * FROM main WHERE id=40";
string query1 = "SELECT m.id, m.product, m.language, m.dateCreated, m.dataIn, m.dataInDate, m.fmRun, m.fmRunDate,m.fmRunDurationHrs, m.fmRunDurationMins, m.fmRunCompleted, u.name, m.fmRunComments FROM main m INNER JOIN ssUsers u ON u.id = m.fmRunUSer WHERE m.id=39";
SqlCommand cmd = new SqlCommand(query1, sqlConn);
//Create a new sql adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
//Create a new sql dataset
DataSet ds = new DataSet();
//fill the dataset (without the second paramter, the overload method creates a name based upon the SELECT statement in the Command
da.Fill(ds, "main");
//Use dataSet results to fill objects
ChooserDropDownList.Text = ds.Tables[0].Rows[0][0].ToString();
ProductLabel.Text = ds.Tables[0].Rows[0][1].ToString();
LanguageLabel.Text = ds.Tables[0].Rows[0][2].ToString();
TimeStarted.Text = ds.Tables[0].Rows[0][7].ToString();
UserDropDownList.Text = ds.Tables[0].Rows[0][11].ToString();
//HoursDropDownList.Text = ds.Tables[0].Rows[0][8].ToString();
//MinutesDropDownList.Text = ds.Tables[0].Rows[0][9].ToString();
CommentsTextBox.Text = ds.Tables[0].Rows[0][12].ToString();

//Close the connection
sqlConn.Close();

}[/code]
Q1. Is this the most functional way to acheive this? ie. Do I really need to use...
<pre class="prettyprint Tables[0].Rows[0][/code]
each time?
Q2. I have a dropDown menu that has an Id reference in the main table and needs something like an INNER JOIN to get the Text value from another table. I have added a new String for the query - ie Query1 - but it doesnt work. How do I do this?



<
Lee Warren
<br/>

View the full article
 
Back
Top