Hi Aryan,
ASP:
Select Image Here:
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" Text="Upload & Save" OnClick="SaveIamgeIntoDatabase" />
C# Code:
protected void SaveImageIntoDatabase(object sender, EventArgs e)
{
string fileName = FileUpload1.PostedFile.FileName;
int fileLength = FileUpload1.PostedFile.ContentLength;
byte[] imageBytes = new byte[fileLength];
FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, fileLength);
SqlConnection conn = new SqlConnection(_connStr)
SqlCommand cmd = new SqlCommand("InsertImage", conn)
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] prms = new SqlParameter[2];
prms[0] = new SqlParameter("@fileName", SqlDbType.VarChar, 50);
prms[0].Value = fileName;
prms[1] = new SqlParameter("@fileContent", SqlDbType.Image);
prms[1].Value = imageBytes;
cmd.Parameters.AddRange(prms);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
Aryan1982, if this helps please login to Mark As Answer. | Alert Moderator