Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can I use SQLDataSource to populate form fields like textboxes?

Status
Not open for further replies.

JGALEY

IS-IT--Management
May 21, 2003
105
US
I have SQLDataSource working to load a series of records in a GridView control. Works great for that, and I can insert and update using the SQLDataSource, also with no problem.

What if I want to load a single database row for editing on a form, with typical controls like text boxes - can I use SQLDataSource and some form of the select command, telling the SQLDataSource to put the right db fields in the right controls?

Seems like I should, but I have not found anything that discusses it (everything I have seen discusses a single databound control like a GridView, listbox, etc). Or should I just use the "old fashioned" way and use a DataReader? I can do that, but the declarative binding in ADO.NET 2.0 seems great, and I would love to use it fully.

Thanks in advance,

Jason
 
If it was me I would:

1. Get the data from SQL Server using a SQL Statement.

2. Using C# SqlConnection, SqlCommand & SqlDataAdapter objects run the SQL Statement and populate a DateTable

3. After the DataTable is loaded set up DataBindings to the controls you want to populate.

Eg:
Code:
textBox1.DataBindings.Add("Text", dataTable, "FirstName");

If you set the SqlDataAdapter up with Select, Insert & Update statements, all you need do is call dataAdapter.UpdateData(); and it will take care of any changes to the data the textBox is showing.

Hope this is of some help... [smile]
 
The code for a checkBox would be:

Code:
checkBox1.Databinding.Add("Checked", dataTable, "UserEnabled");

Where "Checked" is the property you want to bind

"dataTable" is the DataSource to bind to

and "UserEnabled" would be the boolean or bit in TSql column in the DataSource you want to bind to.

Regards... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top