Click here to Skip to main content
12,448,343 members (51,246 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi every body,

I want to bind textBox1 or Label with SQL Server in ASP.net application with C#.
I want to write code like this:

textBox1.DataBindings.Add("Text", ds.Tables["Products"], "ProductName");

But this property not appear for me: (DataBindings )
appear that only: (DataBind) , (DataBinding) so (Add method) not appear accordingly.

Any one can tell me why this property not appear for me (DataBindings )

please help me
Thanks.
Posted 14-Oct-10 19:57pm
Comments
Sunasara Imdadhusen 15-Oct-10 2:57am
   
Why you are binding text instead of assign value to Text?
MrProgrammer_78 15-Oct-10 10:05am
   
Sorry,
I want to bind Label.text & Hyperlink.text with database not Textbox
Please help me..........
MrProgrammer_78 15-Oct-10 14:46pm
   
Any body help me please............
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Have a read of Two-Way Data Binding in ASP.NET[^]

** UPDATE **
You are trying to use WinForm data binding with a WebForm this does not work.
If you read the article you should have read the section entitled "Simple DataBinding - a (brief) refresher" and seen the DataBinder.Eval() Method[^] which is what is used to databind controls in WebForms.

To use the DataBinder.Eval() method in CodeBehind;
TextBox1.Text = DataBinder.Eval(ds.Tables["Products"].DefaultView[0], "ProductName").ToString();
This method also has the option to specify a format string;
TextBox2.Text = DataBinder.Eval(ds.Tables["Products"].DefaultView[0], "ProductPrice", "{0:c}"); //Display using currency format

If however all you want is to do is change the text of a label based on a database field you could just use string.Format() like;
Label1.Text = string.Format("{0}", ds.Tables["Products"].DefaultView[0]["Description"]);

Keep in mind that WebForm databinding does not work the same way as WinForm databinding and attempt to comprehend how things work.
  Permalink  
v2
Comments
MrProgrammer_78 15-Oct-10 10:07am
   
I do not understand right from this article
Please i want clear and definite code to use it.
MrProgrammer_78 15-Oct-10 14:46pm
   
Any body help me please............
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

textbox1.text='<%#Eval("columnname")%>'

Same for label or linkbutton text
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 31 Mar 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100