• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Data bind to a lable

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"));
               SqlCommand cmd = new SqlCommand("P_sp_GetPendingIRBApplications", con);

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

Open in new window

  • 2
1 Solution
try this

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

replace FieldName with the actual field name.
HawaiiDragonAuthor Commented:
but how would I do all 8 fields can I do an 8 overload?
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...)
HawaiiDragonAuthor Commented:
I ended up going a diffrent route but thank you for all the help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now