?
Solved

Populate Labels from database

Posted on 2005-05-16
13
Medium Priority
?
226 Views
Last Modified: 2010-04-07
I have been using DataGrids to get data from my database, and now I need to populate
just a few Labels ands and image boxes and I don't know the best way to do it.

Here is what I use to get data for my DataGrids:

list.DataSource = Catalog.GetProdInCat(CategoryId)

Public Shared Function GetProdInCat(ByVal categoryId As String) As SqlDataReader
      Dim connection As New SqlConnection(connectionString)
      Dim command As New SqlCommand("GetProdInCat", connection)
      command.CommandType = CommandType.StoredProcedure
      command.Parameters.Add("@CategoryID", SqlDbType.Int, 4)
      command.Parameters("@CategoryID").Value = categoryId
      Try
         connection.Open()
         Return command.ExecuteReader(CommandBehavior.CloseConnection)
      Catch e As Exception
         connection.Close()
         Throw e
      End Try
End Function

How can I use the above to fill Labels and Images?
Example fields from database:   "Name"   "ImagePath"

<asp:Image Runat=server id="imgBig"></asp:Image>
<asp:Label Runat=server ID="txtName"></asp:Label>

Also, I have a bit field, "Engraving" that when "True" I want to display an Image control.
Thanks
0
Comment
Question by:MikeMCSD
  • 6
  • 6
13 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14011406
Are the images stored in the database, or are they stored on the disk?

Bob
0
 
LVL 25

Assisted Solution

by:nauman_ahmed
nauman_ahmed earned 400 total points
ID: 14011557
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 14011773
The images are stored in folders.  I store the filename (myPic.jpg) in the
ImagePath field.
Nauman, that link is about DataGrids, which I'm not using.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 14012006
What is the relationship between the data record and the image URL?  Do you have the path stored in the database?

Bob
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 14012086
I build the path. For example, this is how I did it in the DataGrid:

<img src='<%# server.URLEncode("pd\" & Request.QueryString("departmentID") & "\" & Request.QueryString("CategoryID") & "\" & 
DataBinder.Eval(Container.DataItem, "ImagePath ")) %>'
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14012291
That's looks like a good way to me.  Are you looking for a better way?  Is this too slow?  What prompted this question?

Bob
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 14012545
I guess I didn't state the question right. Here is what I want to do:

*** use this:

???????????? = Catalog.GetProdInCat(CategoryId)

Public Shared Function GetProdInCat(ByVal categoryId As String) As SqlDataReader
      Dim connection As New SqlConnection(connectionString)
      Dim command As New SqlCommand("GetProdInCat", connection)
      command.CommandType = CommandType.StoredProcedure
      command.Parameters.Add("@CategoryID", SqlDbType.Int, 4)
      command.Parameters("@CategoryID").Value = categoryId
      Try
         connection.Open()
         Return command.ExecuteReader(CommandBehavior.CloseConnection)
      Catch e As Exception
         connection.Close()
         Throw e
      End Try
End Function

*** to fill (or populate) this:

<asp:Image Runat=server id="imgBig"></asp:Image>
<asp:Label Runat=server ID="txtName"></asp:Label>

Example fields from database:   "Name"   "ImagePath"


Also, I have a bit field, "Engraving" that when "True" I want to display an Image control.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14012704
Oops, I missed that in all those words :)

Does GetProdInCat return the fields necessary to populate the Image.ImageURL property?

In the code-behind, you can have:

Dim imageURL As String = "pd\" & Request.QueryString("departmentID") & "\" & Request.QueryString("CategoryID") & "\" & GetProdInCat("id")("ImagePath")

imgBig.ImageURL = imageURL


Bob


0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 14012843
I'll build the path in code like:
dim imgPath as String = "pd\" & Request.QueryString("departmentID") & "\" & Request.QueryString("CategoryID") & "\"

then add on the ImagePath filename from database.
Will look like this: "pd\58\20\image.gif"

So the full path with image is: imgPath + ImagePath

ImagePath is the field in the database . . but it is not a path, just a filename.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 14012867
So, then set imgBig.ImageURL to the full path specified by imgPath + ImagePath from the database.

Bob
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 14013019
How would I fill a Label with a field returned from the DataReader:

???????? = Catalog.GetProdInCat(CategoryId)

<asp:Label Runat=server ID="txtName"></asp:Label>

It contains a field "Name" that I want to put in the Label.

I could put: Imports System.Data.SqlClient and then do this
Dim customerReader As SqlDataReader = Catalog.GetProdInCat(CategoryId)
      If customerReader.Read =  . . . . .
But I want to avoid the overhead of the Imports. Is there anyway to access those
fields without using Imports?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1600 total points
ID: 14013067
Dim customerReader As System.Data.SqlClient.SqlDataReader = Catalog.GetProdInCat(CategoryId)

Me.txtName.Text = customerReader("Name")

You can combine this code with the code to set the image control's ImageURL property.

Bob


0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 14014964
thanks Bob
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

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