Solved

Help displaying images pulled from Sql Server into Web Applicatoin

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now