Solved

Data bind to a lable

Posted on 2011-09-14
4
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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