Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 135
  • Last Modified:

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
                }
            }
0
jasonbrandt3
Asked:
jasonbrandt3
  • 2
1 Solution
 
ZberteocCommented:
This might help:

http://www.sqlusa.com/bestpractices/imageimportexport/

Storing images in SQL server is not something I prefer. It is way better to store images on local folder and store in the table only the path to it.
0
 
ZberteocCommented:
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now