Solved

Help displaying images pulled from Sql Server into Web Applicatoin

Posted on 2014-12-05
2
130 Views
Last Modified: 2014-12-10
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
Comment
Question by:jasonbrandt3
  • 2
2 Comments
 
LVL 26

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 40482833
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
 
LVL 26

Expert Comment

by:Zberteoc
ID: 40482835
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question