Click to See Complete Forum and Search --> : [RESOLVED] Selecting Records from SQL


admdev
April 1st, 2009, 01:43 PM
I have a list of 12 text boxes laid out in three rows (4 boxes per row). Each of these rows represents a record in SQL. Now what I need to do is select the records in SQL and populate them in the text boxes. How do I go through the retrieved records and assign them to the corresponding boxes?

what I need to do.

SQL fileds

field1, field2, field3, field4

First SQL record populates this TextBox1=field1------TextBox2=field2------TextBox3=field3-----TextBox4=field4

Second SQL record populates this TextBox5=field1------TextBox6=field2------TextBox7=field3-----TextBox8=field4

And so on......

Below is the code I am Using



SqlDataAdapter adapt = new SqlDataAdapter(
"Select * from table where id=" + refid + "order by this" , SqlConnstring);

DataSet dsdata = new DataSet();

//Create a table based on the query result
adapt.TableMappings.Add("Table", "dsdata");
adapt.Fill(dsdata);

System.Data.DataTable dtdata= dsdata.Tables["dsdata"];


foreach (DataRow row in dtdata.Rows)
{
First SQL record TextBox1=field1------TextBox2=field2------TextBox3=field3-----TextBox4=field4
Second SQL record TextBox5=field1------TextBox6=field2------TextBox7=field3-----TextBox8=field4
}


Thanks in advance.

Shuja Ali
April 1st, 2009, 02:26 PM
Why not use a GridView instead? You can just fill a dataset and bind it to GridView.

And the way you have written your SQL is not the correct way of doing it. It is better to write parametrized queries. Look at the example that MSDN has for SQLCommand class.

admdev
April 1st, 2009, 02:50 PM
Shuja Ali,

Thanks for your reply.

I thought about using the Gridview, but the Gridview presents another issue for what I am doing. That is why I desided to stick with this aproach. As for the parameterized query, I will check into it. Normally, I use store procedures, but because this query does not return a lot of data, I desided to do it this way.

Thanks.

eclipsed4utoo
April 1st, 2009, 03:01 PM
Shuja Ali,

Thanks for your reply.

I thought about using the Gridview, but the Gridview presents another issue for what I am doing. That is why I desided to stick with this aproach. As for the parameterized query, I will check into it. Normally, I use store procedures, but because this query does not return a lot of data, I desided to do it this way.

Thanks.

and what is that issue?

admdev
April 2nd, 2009, 08:35 AM
OK guys. I solved my problem.

This is how I am doing it.



SqlDataAdapter adapt = new SqlDataAdapter(
"Select * from table where id=" + refid + "order by this" , SqlConnstring);

DataSet dsdata = new DataSet();

//Create a table based on the query result
adapt.TableMappings.Add("Table", "dsdata");
adapt.Fill(dsdata);

System.Data.DataTable dtdata= dsdata.Tables["dsdata"];

int i = 0;
//Build an array of the comboboxes
ComboBox[] comboBoxes = { cboComboBox1, cboComboBox2, cboComboBox3, cboComboBox4, cboComboBox5 };

foreach (DataRow row in dtdata.Rows)
{
i++;
comboBoxes[i - 1].Text = row["field1"].ToString();

}



This is actually populating comboboxes, but you get the idea.

Hope this helps anybody.