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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

*simple* textbox databinding not working...

Status
Not open for further replies.

vixen925

IS-IT--Management
Oct 3, 2005
7
0
0
US
I simply *cannot* get this dang textbox to bind...
below is my code, throwing the following error:

An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

Additional information: Cannot bind to property or column CUSTID on DataSource.


Any idea what I might be doing wrong?



private void btnNew_Click(object sender, System.EventArgs e)
{
SqlConnection oSQLConn = new SqlConnection();
oSQLConn.ConnectionString=("Data Source=SERVER;Initial Catalog=DATABASE;User Id=user;Password=test");
oSQLConn.Open();
DataSet dsCustomers = new DataSet();
SqlDataAdapter daCustomers = new SqlDataAdapter("SELECT * FROM customers WHERE (CUSTID = 14442182)", oSQLConn);
daCustomers.Fill(dsCustomers); txtSettleCtrlNum.DataBindings.Add("Text",dsCustomers,"CUSTID");
}
 
Are you sure your query is returning anything? You would have problems binding if you had an empty DataSet.

--Brad

"Life is too important to be taken seriously" --Albert Einstein
 
Yes,
My query is fine, it returns a single record, which is what I want.

Anyone else know why the code aboce might not be working?
 
txtSettleCtrlNum.DataBindings.Add("Text",dsCustomers,"customers.CUSTID");

should work.

 
Added 'customers' to the binding syntax, but now get the following error:
'Cannot create a child list for field customers'

I'm totally baffled as to why a simple binding should be this difficult - but have no idea what I could be doing wrong.
 
I think the problem is that a text box can only display one value, and that binding doesn't specify a particular row. Therefore the binding doesn't know what to show.

Here is an example
SqlConnection conn = new SqlConnection("Data Source=(Local);Initial Catalog=Northwind;Integrated Security=SSPI");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("Select * from Orders",conn);
DataSet ds = new DataSet();
da.Fill(ds);

textBox1.DataBindings.Add("Text",ds.Tables[0],"OrderDate");
 
You could just pull out the value from the dataset.
Like this:
textBox1.Text = ds.Tables[0].Rows[0]

I always assign the tables a name when using da.fill.
da.Fill(ds,"table_name"); This allows you to access the tables by name.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top