|
-
April 1st, 2009, 01:43 PM
#1
[RESOLVED] Selecting Records from SQL
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
Code:
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.
-
April 1st, 2009, 02:26 PM
#2
Re: Selecting Records from SQL
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.
-
April 1st, 2009, 02:50 PM
#3
Re: Selecting Records from SQL
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.
-
April 1st, 2009, 03:01 PM
#4
Re: Selecting Records from SQL
 Originally Posted by admdev
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?
-
April 2nd, 2009, 08:35 AM
#5
Re: Selecting Records from SQL
OK guys. I solved my problem.
This is how I am doing it.
Code:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|