We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How to populate VB.NET llabels with data from a SQL table?

NorthArrow
NorthArrow asked
on
Medium Priority
491 Views
Last Modified: 2013-11-05
I've created an interface containing label controls that will display data from a SQL database. The code snippet below shows the header label (which I name) and the label control beside it is "empty" but will display the data from the database. How do I "bind" the label control that will display the data to the SQL database?


<fieldset id="Fieldset1" runat=server style="width: 809px;" >
    <legend class="style1" >Organization Location</legend>
   <br />
 
<asp:Table ID="tblOrganizationInfo" runat="server" BorderColor="Beige" BorderWidth="1" Width="790">
 
 
<asp:TableRow>
<asp:TableCell Width="150"><asp:Label ID="lblOrgNameHeader" runat="server" 
        style="font-weight: 700; font-family: Arial, Helvetica, sans-serif; font-size: small" 
        Text="Organization Name"></asp:Label></asp:TableCell>
 
<asp:TableCell Width="300"><asp:Label ID="lblOrgName" runat="server"
        style="font-weight: 700; font-family: Arial, Helvetica, sans-serif; font-size: small" 
        Text=""></asp:Label></asp:TableCell>
               
        
<asp:TableCell Width="90"><asp:Label ID="lblUICCodeHeader" runat="server" 
        style="font-family: Arial, Helvetica, sans-serif; font-size: small; font-weight: 700" 
        Text="UIC Code"></asp:Label>
        </asp:TableCell>
        
        <asp:TableCell Width="250"><asp:Label ID="lblUICCode" runat="server"
        style="font-family: Arial, Helvetica, sans-serif; font-size: small; font-weight: 700" 
        Text=""></asp:Label>
        </asp:TableCell>
</asp:TableRow>

Open in new window

Comment
Watch Question

Here's a bunch of info for databinding ASP.NET controls: http://www.15seconds.com/Issue/040630.htm

The simplest version is when you bind the form, to manually update the label text: myLabel.Text = myDataRow("myColumnName")

Author

Commented:
Thank you, Chaosian, for your response and the link.  I read the article.  It was alot of information and for some reason I thought binding a control to a data source was a bit simpler.  I'm using ASP.Net 3.5 and MS Visual Studio 2008 and SQL Server Management Studio .  The article talks about ASP.Net 1.x and 2.0.  Is there an updated version of databinding?  

Thanks for providing code.  Question: In "myLabel.Text = myDataRow("myColumnName")" what does "myDataRow" represent?

Could I just write lblOrgName.Text = ("OrgName") and leave out "myDataRow" ?  (This would depend on your answer to my previous question, I suppose ...)

 *OrgName is the column name in the data source.

Thanks!
In general, I would assume that you have some datasource that is based on a datatable/dataset. (Note: this isn't the best design, and I'd recommend using objects instead.)

Having said that, I'm assuming that you're trying to display a single record on the page -- otherwise, I'd expect to see grids rather than labels.

myDataRow is the current System.Data.DataRow that contains the record you're trying to display.

Author

Commented:
Thanks, Chaosian, for the explanation.

Yes, I was going to use the GridView control, but I didn't want the gridlines and I haven't researched on how to turn them off, set spacing between columns/rows, etc.  So since I didn't want the data displaying as a "grid", I will use HTML table code.  

For instance,
a user selects Unit 1 from a dropdown list control.  Data connected to Unit 1 (Org Name, PM, location, Cost Center, etc.) will display within labels.  

If there is a way to hide the gridlines and format the spacing/alignment using the GridView control, please let me know, and I will use that control instead of twelve labels w/i an HTML table.

Thanks

Author

Commented:
" I'm assuming that you're trying to display a single record on the page"

Yes, a single record on the page.  

"myDataRow is the current System.Data.DataRow that contains the record you're trying to display.'

So would  "myLabel.Text = ("OrgName") work,

or do I need to add "myDataRow" or "System.Data.DataRow" to  "myLabel.Text = ("OrgName")

Thanks
("OrgName") will not work. You're not telling it where to get the data.

When you pick the item from the dropdown list, you'll need to get the UnitId (or some other way to specifically identify the record in the datatable).

Assuming that the DataTable that contains data is "dt" and the combo box with your units is "cboUnitList" and cboUnitList has some sort of identifier as the value, then:

        Dim dv As New DataView(dt)
        dv.RowFilter = "UnitId =" & cboUnitList.SelectedValue
        lblOrgName.Text = dv(0)("OrgName")

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks Chaosian, for your help.  I appreciate your providing the code snippet above.  Labels aren't the best way to do this since it requires more time to code vs. using the GridView control. Right now, I'm on a deadline and I don't have time to write alot of code, especially when VS 2008 has controls that can do most of the coding through the smart tags or properties window or using SSMS.  

"("OrgName") will not work. You're not telling it where to get the data."

In my connectionstring code I'm naming and connecting to the datasource.  "OrgName" is a column name in the table within the datasource.  I thought that is what you meant by "myColumnName" in your example above.  I just substituted "OrgName" (the actual column name) instead of using "myColumnName" (a generic name).

I've done a few of the data access tutorials in MS Visual Studio.Net 2008 a few months ago and I remember there being an easier way to expose/access data using the ASP.Net controls -- some with little or no code. While I've been doing sporadic VB.Net work over the years, I'm still a beginner and learning on my own.

Thanks again for your help.
I agree -- GridView is the easiest way to go. However, when you're only displaying a single record, it's kind of a poor UI choice.

The "OrgName" piece -- you're correct as far as you went. However, you need to specify the context of that column, by providing the record/DataRow that the column belongs to.




Author

Commented:
"I agree -- GridView is the easiest way to go. However, when you're only displaying a single record, it's kind of a poor UI choice."

I see your point of poor UI choice.  What do you suggest?  Currently, I'm using HTML table for layout and asp.net label controls to display the data w/i the table cells.

Thanks again, Chaosian.  I appreciate your expertise.

Author

Commented:
Your answers were very good.  I think if I understood VB.Net better, I would use your solution.  So although I selected "Partially" for "Was the solution easy to understand" it was not a criticism of your explanation, but due to my being a novice VB.Net programmer.
You're almost always better off displaying a single record using dedicated controls (e.g. label, textbox, combobox).

Here's a C# tutorial on using ObjectDataSource, which might be of use. It's mostly wizards, so the fact it's C# shouldn't be too big a deal. Particularly interesting is the section titled "Displaying One Record at a Time..."

http://www.asp.net/Learn/data-access/tutorial-04-cs.aspxhttp://www.asp.net/Learn/data-access/tutorial-04-cs.aspx

Author

Commented:
Thanks, Chaosian, for all your help and the tutorial URL you provided in your last post.  I'm reading the tutorial now, and I see that I can switch to VB code by clicking a link in the top right corner of the tutorial.  The developers I will be working with are VB programmers.  So, I'll stick to that language.

There are several tutorials on this page, so I will be using this page alot as a resource.  Thanks again!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.