How can i pull an image and data from a Database?
Posted
by
user1851377
on Stack Overflow
See other posts from Stack Overflow
or by user1851377
Published on 2012-11-25T16:24:06Z
Indexed on
2012/11/25
17:04 UTC
Read the original article
Hit count: 182
I am trying to pull data from a Database using C#.net and use a Foreach loop to make it visible on a page. Every time i run the code i only get one item that shows up when i know that there is at least 7 items in the DB. i have placed the code below for the C#.
SqlConnection oConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["HomeGrownEnergyConnectionString"].ToString());
string sqlEnergy = "Select * from Product p where p.ProductTypeId=3";
SqlCommand oCmd = new SqlCommand(sqlEnergy, oConnection);
DataTable dtenergy = new DataTable();
SqlDataAdapter oDa = new SqlDataAdapter(oCmd);
try
{
oConnection.Open(); ;
oDa.Fill(dtenergy);
}
catch (Exception ex)
{
lblnodata.Text = ex.Message;
return;
}
finally
{
oConnection.Close();
}
DataTableReader results = dtenergy.CreateDataReader();
if (results.HasRows)
{
results.Read();
foreach(DataRow result in dtenergy.Rows)
{
byte[] imgProd = result["ThumnailLocation"] as byte[];
ID.Text = result["ProductID"].ToString();
Name.Text = result["Name"].ToString();
price.Text = FormatPriceColumn(result["Price"].ToString());
}
}
Here is the code for the asp.net.
<div>
<asp:Image ID="imgProd" CssClass="ProdImg" runat="server" />
<asp:Label runat="server" ID="ID" />
<asp:Label runat="server" ID="Name" />
<asp:Label runat="server" ID="price" />
<asp:TextBox ID="txtQty" MaxLength="3" runat="server" Width="30px" />
<asp:Button runat="server" ID="Addtocart" Text="Add To Cart" CommandName="AddToCart" ItemStyle-CssClass="btnCol" />
If someone could please help me that would be great thanks.
© Stack Overflow or respective owner