Welcome

Welcome Anonymous

Monday, September 20, 2010

How to store image in a database in binary format

Create one empty web page and do the following things...

1.Drag & Drop FileUpload Control and one Button from standard Toolbox
2.Double click on button control it'll generate a button click event. In that event write the following code
3.business_logic is a class file and which is used for opening database conections, executing database queries,etc. You can may even directly declare directly the connections, commands in codebehind file(*.cs,*.vb)

business_logic obj = new business_logic();

if (FileUpload1.HasFile)
{

// Storing image in Database
BinaryReader reader = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] image = reader.ReadBytes(FileUpload1.PostedFile.ContentLength);

string temp= obj.assignconn();
con = new SqlConnection(temp);
con.Open();
cmd.Connection = con;
cmd.CommandText = "sp_imageInsert";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@img", image);
cmd.ExecuteNonQuery();
Label2.Visible = true;
Label2.Text = "File Successfully Uploaded";

No comments:

Post a Comment