To Upload an Image in SQL database - "ASP.NET CODE"



protected void Button1_Click(object sender, EventArgs e)


{


string strImageName = TextBox9.Text.ToString();


if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")

{

byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];


HttpPostedFile uploadedImage = FileUpload1.PostedFile;


uploadedImage.InputStream.Read (imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);


SqlConnection db = new SqlConnection("database=master;uid=sa;server=.;");


SqlCommand cmd = new SqlCommand();


cmd.CommandText="insert into new_account(mno,mname,dob,pname,address,phoneno,ban,ib,Image,ImageName) values(" + TextBox1.Text + ",' " + TextBox2.Text + " ',' " + TextBox3.Text + " ',' " + TextBox4.Text + " ',' " + TextBox5.Text + " '," + TextBox6.Text + ",' " + TextBox7.Text + " '," + TextBox8.Text + ",@Image,@ImageName)";


cmd.CommandType = CommandType.Text;


SqlParameter UploadedImage = new SqlParameter ("@Image", SqlDbType.Image, imageSize.Length);


UploadedImage.Value = imageSize;


cmd.Parameters.Add(UploadedImage);


SqlParameter ImageName = new SqlParameter ("@ImageName", SqlDbType.VarChar, 50);


ImageName.Value = strImageName.ToString();


cmd.Parameters.Add(ImageName);


db.Open();


cmd.Connection = db;


cmd.ExecuteNonQuery();


db.Close();


Label11.Visible = true;

}
}

0 comments:

Post a Comment

Popular Posts