Solved

Help displaying images pulled from Sql Server into Web Applicatoin

Posted on 2014-12-05
2
128 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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