Populate Labels from database

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
LVL 16
MikeMCSDAsked:
Who is Participating?
 
Bob LearnedCommented:
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
 
Bob LearnedCommented:
Are the images stored in the database, or are they stored on the disk?

Bob
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
MikeMCSDAuthor Commented:
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
 
Bob LearnedCommented:
What is the relationship between the data record and the image URL?  Do you have the path stored in the database?

Bob
0
 
MikeMCSDAuthor Commented:
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
 
Bob LearnedCommented:
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
 
MikeMCSDAuthor Commented:
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
 
Bob LearnedCommented:
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
 
MikeMCSDAuthor Commented:
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
 
Bob LearnedCommented:
So, then set imgBig.ImageURL to the full path specified by imgPath + ImagePath from the database.

Bob
0
 
MikeMCSDAuthor Commented:
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
 
MikeMCSDAuthor Commented:
thanks Bob
0
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.

All Courses

From novice to tech pro — start learning today.