Solved

Data bind to a lable

Posted on 2011-09-14
4
231 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 500 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now