MikeMCSD
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(Categ oryId)
Public Shared Function GetProdInCat(ByVal categoryId As String) As SqlDataReader
Dim connection As New SqlConnection(connectionSt ring)
Dim command As New SqlCommand("GetProdInCat", connection)
command.CommandType = CommandType.StoredProcedur e
command.Parameters.Add("@C ategoryID" , SqlDbType.Int, 4)
command.Parameters("@Categ oryID").Va lue = categoryId
Try
connection.Open()
Return command.ExecuteReader(Comm andBehavio r.CloseCon nection)
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
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(Categ
Public Shared Function GetProdInCat(ByVal categoryId As String) As SqlDataReader
Dim connection As New SqlConnection(connectionSt
Dim command As New SqlCommand("GetProdInCat",
command.CommandType = CommandType.StoredProcedur
command.Parameters.Add("@C
command.Parameters("@Categ
Try
connection.Open()
Return command.ExecuteReader(Comm
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
Bob
ASKER
I build the path. For example, this is how I did it in the DataGrid:
<img src='<%# server.URLEncode("pd\" & Request.QueryString("depar tmentID") & "\" & Request.QueryString("Categ oryID") & "\" &
DataBinder.Eval(Container. DataItem, "ImagePath ")) %>'
<img src='<%# server.URLEncode("pd\" & Request.QueryString("depar
DataBinder.Eval(Container.
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
Bob
ASKER
I guess I didn't state the question right. Here is what I want to do:
*** use this:
???????????? = Catalog.GetProdInCat(Categ oryId)
Public Shared Function GetProdInCat(ByVal categoryId As String) As SqlDataReader
Dim connection As New SqlConnection(connectionSt ring)
Dim command As New SqlCommand("GetProdInCat", connection)
command.CommandType = CommandType.StoredProcedur e
command.Parameters.Add("@C ategoryID" , SqlDbType.Int, 4)
command.Parameters("@Categ oryID").Va lue = categoryId
Try
connection.Open()
Return command.ExecuteReader(Comm andBehavio r.CloseCon nection)
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.
*** use this:
???????????? = Catalog.GetProdInCat(Categ
Public Shared Function GetProdInCat(ByVal categoryId As String) As SqlDataReader
Dim connection As New SqlConnection(connectionSt
Dim command As New SqlCommand("GetProdInCat",
command.CommandType = CommandType.StoredProcedur
command.Parameters.Add("@C
command.Parameters("@Categ
Try
connection.Open()
Return command.ExecuteReader(Comm
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("depar tmentID") & "\" & Request.QueryString("Categ oryID") & "\" & GetProdInCat("id")("ImageP ath")
imgBig.ImageURL = imageURL
Bob
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("depar
imgBig.ImageURL = imageURL
Bob
ASKER
I'll build the path in code like:
dim imgPath as String = "pd\" & Request.QueryString("depar tmentID") & "\" & Request.QueryString("Categ oryID") & "\"
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.
dim imgPath as String = "pd\" & Request.QueryString("depar
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
Bob
ASKER
How would I fill a Label with a field returned from the DataReader:
???????? = Catalog.GetProdInCat(Categ oryId)
<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(Categ oryId)
If customerReader.Read = . . . . .
But I want to avoid the overhead of the Imports. Is there anyway to access those
fields without using Imports?
???????? = Catalog.GetProdInCat(Categ
<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(Categ
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks Bob
Bob