Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Are the images stored in the database, or are they stored on the disk?

Bob
SOLUTION
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MikeMCSD

ASKER

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

Bob
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 ")) %>'
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
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.
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


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

Bob
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks Bob