CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2008
    Posts
    91

    [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.

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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.

  3. #3
    Join Date
    Sep 2008
    Posts
    91

    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.

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Selecting Records from SQL

    Quote Originally Posted by admdev View Post
    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?

  5. #5
    Join Date
    Sep 2008
    Posts
    91

    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
  •  





Click Here to Expand Forum to Full Width

Featured