Thursday, February 9, 2012

ASP.Net(Save Image Into Database)


#region Add Record

            try
            {
                //User Define Function To Check The Duplicate Record
                if (checkDuplicate())
                {
                    layer = new DALayer();
                    SqlConnection cn = [Your Connection Here];
                    FileUpload img = (FileUpload)fUpldLogo;
                    Byte[] imgByte = null;
                    if (img.HasFile && img.PostedFile != null)
                    {
                        //To create a PostedFile
                        HttpPostedFile File = fUpldLogo.PostedFile;
                        //Create byte Array with file len
                        imgByte = new Byte[File.ContentLength];
                        //force the control to load data in array
                        File.InputStream.Read(imgByte, 0, File.ContentLength);
                    }
                  
                    //Command
                    string strSqlInsert = "INSERT INTO Vendors(Column01, Column02, Column03,
                                          ) VALUES ('" + Element01.Text + "', '" 
                                                       + Element01.Text .
                                                       + "', @logo, ")";

                    SqlCommand command = new SqlCommand(strSqlInsert, cn);
                    command.Parameters.AddWithValue("@logo", imgByte);
                    command.ExecuteNonQuery();

                    Response.Write("<script>alert('Record successfully saved!')</script>");
                }
                else
                {
                    //If Record Already Exists
                    lblError.Visible = true;
                    lblError.Text = "Record already exists!";
                }
            }
            catch (Exception ae)
            {
                throw ae;
            }
                #endregion

No comments:

Post a Comment