We help IT Professionals succeed at work.

Retrieving the SQL databound field name

MrFantastic6
MrFantastic6 asked
on
I have a windows vb .net form with some databound sql fields tied to various textbox and combobox controls.  I have a variable that has the textbox control name established, but I need a way to retrieve the databound sql field name within that textbox or combobox control.  Thanks in advance.
Comment
Watch Question

Commented:
Does this return the value you are looking for?
TextBox.DataBindings(0).PropertyName

Open in new window

Author

Commented:
I'll try it this morning and let you know.  Also is there a way to identify the sqldatatype from any controls, like varchar, int etc.

Author

Commented:
vdefabis: that did not work. it returned the string "text" instead of the sql field name.
Commented:
Hi There -

Sorry about the bad solution - Was doing it from memory. This will return the value you are looking for:
TextBox.DataBindings(0).BindingMemberInfo.BindingField

As for returning the SQL Data Type, there's nothing I can see in the .NET Framework to easily return that information. However, you could use a SQLDataAdapter to run the following SQL query that will return the value you are looking for.

SELECT DATA_TYPE FROM Information_Schema.Columns WHERE Column_Name = 'YOUR_COLUMN_NAME'

If 'YOUR_COLUMN_NAME' is declared as Varchar(50), this query will return 'varchar'. If you need the length, you could select 'CHARACTER_MAXIMUM_LENGTH' as well. For numeric datatypes, 'NUMERIC_PRECISION' is another good column to look at.

Author

Commented:
VDEFABIS is magnificense in the flesh.  Thank you for your quick response my friend.