Click here to Skip to main content
15,886,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a simple database set up and I would like to create a small app so I can add and remove from the database in the question. I have got the basic code for the gui complete and I know how to make a connection to a db. I am using sql server 2008. The main question is how do I populate 4 textboxes with a row of data? so for example I would like first textbox to have the first name second box surname third box dob and fourth gender.

Thank you in advance for any help
Posted

When you have opened the database and have your SELECT statement ready, use a DataTable[^] to hold all informations from the database.

C#
sqlConnection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM tbl WHERE id=42", sqlConnection);

DataTable result = new DataTable();
dataAdapter.Fill(result);

sqlConnection.Close();


When result (DataTable) holds all rows that match the WHERE Clause.
Get value from result and populate TextBox.
C#
// Check for any result
if (result.Rows.Length > 0)
{
  // Fill in textbox values
  textBox_FirstName.Text = result["firstname"].ToString();
  textBox_Surname.Text = result["surname"].ToString();
  textBox_Dob.Text = result["dob"].ToString();
  textBox_Gender.Text = result["gender"].ToString();
}
else
{
  // No record found
  MessageBox.Show("No record found");
}


The DataTable result can be saved for future use.
 
Share this answer
 
v2
Comments
DanHodgson88 5-Jun-11 15:16pm    
thank you very much that is perfect :) appreciate the reply and your time
Kim Togo 6-Jun-11 3:09am    
You are welcome.
[no name] 5-Jun-11 15:26pm    
Give a man a code and you solve his problem for the day. Teach a man to code and research and you give him skills for a lifetime.
Kim Togo 6-Jun-11 2:56am    
I just wanted to give OP a 'little' push in the right direction. But perhaps I have pushed too hard :-)
Tarun.K.S 5-Jun-11 16:25pm    
Good answer Kim and I do agree with Mark's comment.
Every basic book and article on .NET development covers databinding to some degree or another. Have you at least done some research?
 
Share this answer
 
Comments
DanHodgson88 5-Jun-11 15:15pm    
yes I have and with respect pointing to a website or a book would have been appreciated snidy comments aren't
[no name] 5-Jun-11 15:27pm    
Then don't ask questions that could be answered with very little research and effort on your part.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900