?
Solved

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

Posted on 2009-02-18
12
Medium Priority
?
470 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

0
Comment
Question by:NorthArrow
  • 7
  • 5
12 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 23676946
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")
0
 

Author Comment

by:NorthArrow
ID: 23682033
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!
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 23682468
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:NorthArrow
ID: 23683595
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
0
 

Author Comment

by:NorthArrow
ID: 23683657
" 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
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 2000 total points
ID: 23683920
("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")
0
 

Author Comment

by:NorthArrow
ID: 23685189
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.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 23685421
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.




0
 

Author Comment

by:NorthArrow
ID: 23692436
"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.
0
 

Author Closing Comment

by:NorthArrow
ID: 31548480
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.
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 23692639
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
0
 

Author Comment

by:NorthArrow
ID: 23711701
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!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

615 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