Use Image DataType to store Image in Sql Database
protected void SaveImage_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(img, 0, (int)FileUpload1.PostedFile.ContentLength);
string str = "insert img values(@img)";
SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=DBName;user id=sa;password=password");
cn.Open();
SqlCommand cmd = new SqlCommand(str, cn);
cmd.Parameters.AddWithValue("@img", img);
SqlDataReader dr = cmd.ExecuteReader();
cn.Close();
}
}
protected void ViewReport_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=DBName;user id=sa;password=password");
cn.Open();
SqlDataAdapter cmd = new SqlDataAdapter("select * from img", cn);
DataTable dt = new DataTable();
cmd.Fill(dt);
string file = Server.MapPath("") + "\\CrystalReport.rpt";
CrystalDecisions.CrystalReports.Engine.ReportDocument orpt = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
orpt.Load(file);
orpt.SetDataSource(dt);
CrystalReportViewer1.ReportSource = orpt;
}