C# - Database Application - Data from SQL into Text box?

50cal
50cal used Ask the Experts™
on
Hey All,

I'm trying to create a basic application to store contact information ... first name, surname, etc etc...

What I want to be able to do is pull out information from the database and enter the relevant fields from the database into the relevant fields on the screen....
So First name column in the database goes to the first name field Text box.

I've seen some information flying about with regards to datasets and dataSources but Have not been able to get them working.... Or rather... cant bind the datasource to the text box.

Any help would be appreciated.

Cheers,
50cal.

qlConnection MyTestDatabaseCnct = new SqlConnection("Data Source = " + lblServerName.Text.ToString() + "; Initial Catalog = MyTestDatabase; User Id = SA; Password = password;");
            //Open the Database Connection
            try
            {
                MyTestDatabaseCnct.Open();
            }
            catch (Exception fieldException)
            {
                MessageBox.Show(fieldException.ToString());
            }
 
            // create the data adapter 
            SqlDataAdapter dataAdapter = new SqlDataAdapter("SELECT * FROM dbo.studentDetails", MyTestDatabaseCnct);
            // create an SqlCommandBuilder - this will automatically generate the
            // commands, and set the appropriate properties in the dataAdapter
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
            // create the DataSet 
            DataSet dataSet = new DataSet();
            // fill the DataSet using our DataAdapter into a table called fields
            dataAdapter.Fill(dataSet, "fields");
 
            BindingSource bindingSource1 = new BindingSource();
            bindingSource1.DataSource = dataSet;
 
            this.textBox1.Text = bindingSource1[];
 
            
            //Set the field text to the relevant data from the database - NOT WORKING :(.
            CField field = (CField)arrField[0];
            field.setFieldText(bindingSource1[0].ToString());
 
 
 
            //Close the SQL Connection
            try
            {
                MyTestDatabaseCnct.Close();
            }
            catch (Exception except)
            {
                MessageBox.Show(except.ToString());
            }
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2013
Commented:
This is what you should be doing:
dataAdapter.Fill(dataSet, "fstudentDetails");
 DataTable dt = ds.Tables["fstudentDetails"];

this.textBox1.Text = dt.Rows[0]["SutdentId"].ToString();
this.textBox2.Text = dt.Rows[0]["FirstName"].ToString();
this.textBox3.Text = dt.Rows[0]["LastName"].ToString();
and so on....

Hope this gives you the idea.

Author

Commented:
That's brilliant - working fine,

I'm assuming then you just do a count on the datatable rows.
dt.rows.count
in order to make the navigations buttons work?

Also - how easy would it be to write the changes to the dataTable back to the SQL database?
As creating a new student I assume would write a new record to the dataTable - and then from there back to the database?

Cheers,
50cal.

Top Expert 2013
Commented:
Yes you can loop through using rows.count...
For updates just make changes to the DataTable and all dataadapter.Update method...
like below:
dataAdapter.Update(dataSet, "fstudentDetails");

Look here for basic operations with DataSet and DataAdapter...
http://www.java2s.com/Tutorial/CSharp/0560__ADO.Net/UpdateSqlDataAdapterwithDataSet.htm
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson05.aspx

Author

Commented:
Thanks allot for your help!
50cal.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial