?
Solved

Data bind to a lable

Posted on 2011-09-14
4
Medium Priority
?
242 Views
Last Modified: 2012-05-12
I have a stored procedure that populates to a data table. I was wondering if I can get this to populate to a label instead? I have never done this before and I have tried the databind feature but cant get it to work. Below is my code.



SqlConnection con = new SqlConnection(Settings.GetConfigValue("PARK_IRB_V1_11_19_2010", "IRB2010ConnectionString"));
               con.Open();
               SqlCommand cmd = new SqlCommand("P_sp_GetPendingIRBApplications", con);

               cmd.CommandType = CommandType.StoredProcedure;
               SqlDataReader sdr = cmd.ExecuteReader();
               DataTable dt = new DataTable();
               dt.Load(sdr);
               GridView1.DataSource = dt;
               GridView1.DataBind();
// As you can see I was trying below but to no avail               
Label2.DataBind();

Open in new window

0
Comment
Question by:HawaiiDragon
  • 2
4 Comments
 
LVL 39

Expert Comment

by:appari
ID: 36540542
try this

Label1.DataBindings.Add(New Binding("Text", dt, "FieldName"))

replace FieldName with the actual field name.
0
 

Author Comment

by:HawaiiDragon
ID: 36542549
but how would I do all 8 fields can I do an 8 overload?
0
 
LVL 15

Accepted Solution

by:
x77 earned 2000 total points
ID: 36543524
What is overload for you ?

You use Copy Paste an wrote 8 lines, then replace Labelname and fieldname on each line:
You can do Bindings initialization on New or Load from form

Private Bs as BindingSource

'This Go on Sub New (After 'Your Code Here) or Form_load
Bs = new BindingSource
Label1.DataBindings.Add(New Binding("Text", Bs, "FieldName1"))
Label2.DataBindings.Add(New Binding("Text", Bs, "FieldName2"))
Label3.DataBindings.Add(New Binding("Text", Bs, "FieldName3"))
...

Note that when use Know DataSource at Dessign time you can do it in Ide Graphical Interface.
Also can do it using Drag-Drop from DataSource that creates Labels, TexBoxes...

Next Bind Your Bindingsource to Your DataTable each time After load it:
Bs.DataSource = Dt
   
Note. The  "New Binding( ... " constructor has some overloads and allow include Format especification (Dates, numbers...)
0
 

Author Closing Comment

by:HawaiiDragon
ID: 36816767
I ended up going a diffrent route but thank you for all the help
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question