Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi dudes

I am a beginner of c#.So i want a full explain and coding of how to load a image in database.
Posted
Comments
Sumit_Kumar_Sinha 15-Oct-12 2:23am    
we can upload image in database not load..........

Such question have been ask'e so many times on this website, see here[^].

I hope you're not beginner to google[^], try there first and then post your questions if you get stuck'd somewhere. This is not at all appropriate. Give a try at least.
 
Share this answer
 
put a file upload control,Two textboxes txtImagid,txtImageName ,Upload button


StoreImages is table with imageid int,imagename vachar(20), myimage image

Write the below code in upload button click


C#
protected void UploadImage_Click(object sender, EventArgs e)
   {
       int retValue = 0;

       byte[] buffer=new byte[(int)fileUpload1.FileContent.Length];

       fileUpload1.FileContent.Read(buffer, 0, buffer.Length);

       string SqlCmd = "insert into StoreImages(imageid,ImageName,MyImage) values(@id,@Name,@MyImage)";

       using (SqlConnection con = new SqlConnection(ConnString))
       {
           using (SqlCommand cmd = new SqlCommand(SqlCmd, con))
           {
               SqlParameter[] prm = new SqlParameter[3];
               prm[0] = new SqlParameter("@id", SqlDbType.Int);
               prm[0].Value = txtImgId.Text.Trim();
               prm[1] = new SqlParameter("@Name", SqlDbType.VarChar,20);
               prm[1].Value = fileUpload1.FileName;
               prm[2] = new SqlParameter("@MyImage", SqlDbType.VarBinary);
               prm[2].Value = buffer;
               cmd.Parameters.AddRange(prm);
               con.Open();
               try
               {
                   retValue = cmd.ExecuteNonQuery();
                   if (retValue != 0)
                   {
                       Response.Write("Successfully uploaded");
                   }
                   else
                   {
                       Response.Write("Not uploaded");
                   }
                   con.Close();
               }
               catch (Exception ee)
               {
                   Response.Write(ee.Message + "");
               }
           }
       }
   }
 
Share this answer
 
v3
 
Share this answer
 
You may wish to put what technology as your question is very loose in definition.

I would suggest you look at Working with SQL Server BLOB Data in .NET[^]. This should get you started with BLOB's and ADO.NET
 
Share this answer
 
SqlDataAdapter da;
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
PhotoPanel.Visible = true;
SubmitLabel.Visible = false;
}
protected void SubmitLinkButton_Click(object sender, EventArgs e)
{


if (PhotoFileUpload.HasFile)
{
string str1 = PhotoFileUpload.FileName;
string str2 = Path.GetFileName(str1);
string str = "~/DataImage/" + str2;
PhotoFileUpload.SaveAs(MapPath("~/DataImage/" + str2));

da = new SqlDataAdapter("insert into Photogallery (Name,Photo) values ('" + NameTextBox.Text + "','" + str + "')", cstring);
da.Fill(ds);
PhotoPanel.Visible = false;
SubmitLabel.Visible = true;

}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "msg", "alert('Please insert image...');", true);

}




}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900