Solved

Data bind to a lable

Posted on 2011-09-14
4
236 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

729 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