Link to home
Start Free TrialLog in
Avatar of jasonbrandt3
jasonbrandt3

asked on

Help displaying images pulled from Sql Server into Web Applicatoin

I've created a store front.  I am able to view everything in my loop I'm attaching except the images will not display.  Here is the database code, the images are stored in the varbinary(max) columns named Images of part etc.

CREATE TABLE [dbo].[Inventory_Table](
[Row_Index] [int] NULL,
[Part_Name] [char](50) NULL,
[Brand_of_Part] [char](15) NULL,
[Year_of_Part] [int] NULL,
[Price] [money] NULL,
[Type_of_Part] [char](15) NULL,
[Condition] [char](15) NULL,
[Images_of_Part] [varbinary](max) NULL,
[Images_of_Part_2] [varbinary](max) NULL,
[Images_of_Part_3] [varbinary](max) NULL,
[Images_of_Part_4] [varbinary](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

I've created a connector and everything comes through fine on the page except the images and I can't seem to understand why.  Here is my code to pull the images, any assistance is very welcome!

        protected void Page_Load(object sender, EventArgs e)
        {
            string connectionString = ("Data Source=localhost\\test;Initial Catalog=Store Inventory;User ID = dbaStore;" + "Password = test;");
            SqlConnection dbConnection = new SqlConnection(connectionString);
            string query = "SELECT * FROM INVENTORY_TABLE";

            dbConnection.Open();

            SqlDataAdapter adapter = new SqlDataAdapter(query, dbConnection);
            DataSet dsStore = new DataSet();
            DataTable dtInventory = new DataTable();

            adapter.FillSchema(dsStore, SchemaType.Source, "Inventory_Table");
            adapter.Fill(dsStore, "Inventory_Table");
            dtInventory = dsStore.Tables["Inventory_Table"];

            foreach (DataRow row in dtInventory.Rows)
            {
                itemList.Add(new Item());

                foreach (DataColumn column in dtInventory.Columns)
                {
                    itemList[dtInventory.Rows.IndexOf(row)].RowIndex = Convert.ToInt32(row.ItemArray[0]);
                    itemList[dtInventory.Rows.IndexOf(row)].NameOfPart = Convert.ToString(row.ItemArray[1]);
                    itemList[dtInventory.Rows.IndexOf(row)].BrandOfPart = Convert.ToString(row.ItemArray[2]);
                    itemList[dtInventory.Rows.IndexOf(row)].YearOfPart = Convert.ToInt32(row.ItemArray[3]);
                    itemList[dtInventory.Rows.IndexOf(row)].Price = Convert.ToDecimal(row.ItemArray[4]);
                    itemList[dtInventory.Rows.IndexOf(row)].TypeOfPart = Convert.ToString(row.ItemArray[5]);
                    itemList[dtInventory.Rows.IndexOf(row)].Condition = Convert.ToString(row.ItemArray[6]);
                    itemList[dtInventory.Rows.IndexOf(row)].Data_1 = (Byte[])(row.ItemArray[7]);
                    itemList[dtInventory.Rows.IndexOf(row)].Data_2 = (Byte[])(row.ItemArray[8]);

                    //Add images that are null
                }
            }
ASKER CERTIFIED SOLUTION
Avatar of Zberteoc
Zberteoc
Flag of Canada 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
I also found a reference with this:
To store image into SQL server(C#):
byte[] image = File.ReadAllBytes("D:\\11.jpg");

SqlCommand sqlCommand = new SqlCommand("INSERT INTO imageTest (pic_id, pic) VALUES (1, @Image)", yourConnectionReference);
sqlCommand.Parameters.AddWithValue("@Image", image);
sqlCommand.ExecuteNonQuery();

Open in new window

To read it(C#):
SqlDataAdapter dataAdapter = new SqlDataAdapter(new SqlCommand("SELECT pic FROM imageTest WHERE pic_id = 1", yourConnectionReference));
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);

if (dataSet.Tables[0].Rows.Count == 1)
{
    Byte[] data = new Byte[0];
    data = (Byte[])(dataSet.Tables[0].Rows[0]["pic"]);
    MemoryStream mem = new MemoryStream(data);
    yourPictureBox.Image= Image.FromStream(mem);
} 

Open in new window