| Author: sujatha jillela 20 Aug 2008 | Member Level: Silver | Rating: Points: 6 |
Uploading an image and saving it into database
protected void save_Click(object sender, EventArgs e) { // create a byte[] for the image file that is uploaded int imagelen = FileUpload1.PostedFile.ContentLength; byte[] picbyte = new byte[imagelen]; FileUpload1.PostedFile.InputStream.Read(picbyte, 0, imagelen); // Insert the image and image id into the database SqlConnection conn = new SqlConnection("server=192.168.0.1;User ID=practice1;password=teamwork;database=practice"); try { conn.Open(); SqlCommand cmd = new SqlCommand("insert into TableImage"+"(ImageId,ImageName) values (@imageid,@pic)", conn); cmd.Parameters.AddWithValue("@pic", picbyte); cmd.Parameters.AddWithValue("@imageid", TextBox1.Text); cmd.ExecuteNonQuery(); } finally { conn.Close(); } }
|
| Author: rishika 20 Aug 2008 | Member Level: Gold | Rating: Points: 1 |
see this link http://www.aspfree.com/c/a/ASP.NET/Uploading-Images-to-a-Database--C---Part-I/
|
| Author: jeeva 20 Aug 2008 | Member Level: Silver | Rating: Points: 6 |
Hello Ramya
For this you use
Fileupload control and create a database to upload image
and create a directory at within your project directory
i think this code will be useful to you
{ con = new SqlConnection("server=localhost;database=jee;uid=sa;pwd=sa"); com = new SqlCommand("select iid from images order by iid desc", con); //com.CommandType = CommandType.StoredProcedure; da = new SqlDataAdapter(); da.SelectCommand = com; dt = new DataTable(); da.Fill(dt);
if (dt.Rows.Count > 0) { i = Convert.ToInt32(dt.Rows[0]["iid"].ToString()); i = i + 1; } else { i = 1; } } { if (FileUpload2.HasFile) { //string sm = Server.MapPath("upload//" + FileUpload1.FileName); string sm = Server.MapPath("images//" + FileUpload2.FileName); FileUpload2.SaveAs(sm);
Label6.Text = "File Uploaded : " + FileUpload2.FileName;
} else { Label6.Text = "No File Uploaded."; }
}
{ con = new SqlConnection("server=localhost;database=jee;uid=sa;pwd=sa"); com = new SqlCommand("images_pro1", con); com.CommandType = CommandType.StoredProcedure; da = new SqlDataAdapter(); da.SelectCommand = com; com.Parameters.AddWithValue("@iidp", i); com.Parameters.AddWithValue("@pnamep", DropDownList2.SelectedItem.Text); com.Parameters.AddWithValue("@inamep", TextBox2.Text); com.Parameters.AddWithValue("@ipathp", FileUpload2.FileName); com.Parameters.AddWithValue("@istatusp", S); // com.Parameters.AddWithValue("@productId", Convert.ToInt32(Session["productId"]).ToString()); dt = new DataTable(); da.Fill(dt);
TextBox2.Text = ""; }
|