Click here to Skip to main content
Rate this: bad
good
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 20:57pm
Comments
Sunasara Imdadhusen at 15-Oct-10 2:57am
   
Why you are binding text instead of assign value to Text?
MrProgrammer_78 at 15-Oct-10 10:05am
   
Sorry,
I want to bind Label.text & Hyperlink.text with database not Textbox
Please help me..........
MrProgrammer_78 at 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 at 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 at 15-Oct-10 14:46pm
   
Any body help me please............
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hey this is nice solution. thanks
  Permalink  
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
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 DamithSL 165
4 Tomas Takac 144
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
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