sql - Updating and retrieving images (Asp.net c#) -
protected void upimg_about_click(object sender, eventargs e) { con.open(); string sqlquery = " update [dbo].[tbldetails] set [image]=@image,[image2]=@image2 id=@id"; sqlcommand cmd2 = new sqlcommand(sqlquery, con); cmd2.parameters.addwithvalue("@id", session["email"].tostring()); int img = image1.postedfile.contentlength; int img2 = image2.postedfile.contentlength; byte[] msdata = new byte[img]; byte[] msdata2 = new byte[img2]; image1.postedfile.inputstream.read(msdata, 0, img); image2.postedfile.inputstream.read(msdata2, 0, img2); cmd2.parameters.addwithvalue("@image", msdata); cmd2.parameters.addwithvalue("@image2", msdata2); if (con.state == connectionstate.closed) { con.open(); } cmd2.executenonquery(); con.close(); data1.text="image updated successfully"; } this code using update images in database.
the user when required can update images (eg: in firstpage.aspx) , can retrieve in next page (nextpage.aspx).
but problem is: suppose user wants update single image , he/she upload's image , clicks update button , when retrieving images in next page image updated visible 1 present in database not. not sure during updation other fileupload empty why happening? there other way it?
ps: have textboxes in firstpage.aspx in retrieving text he/she has put in database , hence when user wants make changes can done easily.
textbox1.text = dr["name"].tostring(); textbox2.text = dr["address"].tostring(); so, possible retrieve image path user has submitted? or way in user can update single image , during retrieval both images can retrieved?
thank you! :)
break code can send 1 image @ time db. pass corresponding fileupload , sql column name function. conditionally send new file database depending on whether fileupload contains file. can check looking @ hasfile property.
protected void upimg_about_click(object sender, eventargs e) { // make sure @ least 1 file if (!image1.hasfile && !image2.hasfile) { data1.text="no images uploaded"; return; } con.open(); uploadimage(image1, "[image]"); uploadimage(image2, "[image2]"); con.close(); data1.text = "image updated successfully"; } void uploadimage(fileupload fileupload, string columnname) { if (!fileupload.hasfile) { return; } string sqlquery = "update [dbo].[tbldetails] set " + columnname + "=@image id=@id"; sqlcommand cmd = new sqlcommand(sqlquery, con); cmd.parameters.addwithvalue("@id", session["email"].tostring()); int img = fileupload.postedfile.contentlength; byte[] msdata = new byte[img]; fileupload.postedfile.inputstream.read(msdata, 0, img); cmd.parameters.addwithvalue("@image", msdata); cmd.executenonquery(); }
Comments
Post a Comment