Click here to Skip to main content
Sign Up to vote bad
good
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............

3 solutions

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  
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............
hey this is nice solution. thanks
  Permalink  
textbox1.text='<%#Eval("columnname")%>'
 
Same for label or linkbutton text
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Prasad_Kulkarni 407
1 Sergey Alexandrovich Kryukov 340
2 _Amy 331
3 Christian Graus 268
4 OriginalGriff 235
0 Sergey Alexandrovich Kryukov 6,649
1 Prasad_Kulkarni 3,281
2 _Amy 3,065
3 OriginalGriff 2,989
4 CPallini 2,696


Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid