0

I wanted to test storing images on a database, so I did a Google search and found this code:

.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data.SqlClient;
using System.IO;
using System.Data;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    string strImageName = Path.GetFileName(FileUploadControl.FileName);
    FileUploadControl.SaveAs(Server.MapPath("~/") + strImageName);


    Bitmap bNewImage = new Bitmap(strImageName);
    FileStream fs = new FileStream(strImageName, FileMode.Open, FileAccess.Read);

    //creating byte array to read image

    byte[] bImage = new byte[fs.Length];


    fs.Read(bImage, 0, Convert.ToInt32(fs.Length));

    fs.Close();
    fs = null;



    string connstr = "Server=WINSP3UE\\SqlExpress;Database=ImageStore;Trusted_Connection=True;";
    SqlConnection conn = new SqlConnection(connstr);
    conn.Open();
    string strQuery;

    strQuery = "insert into [dbo].[ImageStore](id,[ImageContent]) values(" + "1," + " @pic)";//"INSERT INTO ImageStore (ID, ImageContent) values (" + "1,"  + " @pic)";

    SqlParameter ImageParameter= new SqlParameter();
    ImageParameter.SqlDbType = SqlDbType.Image;
    ImageParameter.ParameterName = "pic";
    ImageParameter.Value = bImage;



    SqlCommand cmd = new SqlCommand(strQuery, conn);
    cmd.Parameters.Add(ImageParameter);
    cmd.ExecuteNonQuery();
    Response.Write("Image has been added to database successfully");



    cmd.Dispose();
    conn.Close();
    conn.Dispose();


}

}

.aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"  Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
   <asp:FileUpload id="FileUploadControl" runat="server" />
    <asp:Button ID="Button1" runat="server" Text="Upload" />
</div>
</form>

It should work but it isn't, and by that I mean it doesn't update the database, the values in the tables remain null. And the message "Image has been uploaded to your database successfully" doesn't appear either. The database I am using has one table named ImageStore with two attributes, ID and ImageContent.

I am new to asp.net and c#, could anyone please check the code for any errors?

1
  • Your parameter name should be @pic instead of pic. And like others suggested, you should run it through debugger. There are so many things that can go wrong. What kind of error message are you getting btw? Commented Apr 30, 2011 at 2:37

3 Answers 3

1

First of all you imageid is hardcoded in the insert statement which is not good: first off, if ID is set to auto increment identity column, it will throw an exception unless you set identity insert on that table on, if it's not set to auto increment, you will get duplicate IDs, that will eventually result in primary key violation. Rather, leave out ID insert and just perform the content insert. As far as the parameters go, igonre the parameter declaration removing everything from strQuery definition to cmd creation. Instead use this:

strQuery = "insert into [dbo].[ImageStore]([ImageContent]) values(@pic)";

SqlCommand cmd = new SqlCommand(strQuery, conn);
cmd.Parameters.AddWithValue("pic", bImage);

if(cmd.ExecuteNonQuery()>0)
    Response.Write("Image has been added to database successfully");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for answering. I tried that, but still the same problem, I'll try debugging it, thanks once again!
0

I suggest that you look at your code in the debugger. There is just too much that can go wrong for a random guess to make sense.

1 Comment

Thank for the reply, yeah I am gonna do that, wish me luck! :)
0

ImageParameter.ParameterName = "pic";

ImageParameter.ParameterName = "@pic";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.