asp.net - insert Image in database -
i new asp.net , databases! trying save image database file upload control. have tried isn't working upon clicking submit button, data not added database neither showing error! code have tried
protected void buttonsubmit_click(object sender, eventargs e) { if (fileupload1.hasfile && page.isvalid) //fileupload , submit { string fileextension = system.io.path.getextension(fileupload1.filename); if (fileextension.tolower() != ".jpg") { labelupload.text = "only files .jpg extension allowed"; labelupload.forecolor = system.drawing.color.red; } else { fileupload1.saveas(server.mappath("~/uploads/" + fileupload1.filename)); labelupload.text = "file uploaded"; labelupload.forecolor = system.drawing.color.deepskyblue; labelsubmit.text = "submitted succesfully"; labelsubmit.forecolor = system.drawing.color.deepskyblue; } } else { labelupload.text = "please select file"; labelupload.forecolor = system.drawing.color.red; labelsubmit.text = "failed submit"; labelsubmit.forecolor = system.drawing.color.red; } // insert database work obj = new work(); /* stream fs = fileupload1.postedfile.inputstream; binaryreader br = new binaryreader(fs); byte[] bytes = br.readbytes((int32)fs.length);*/ obj.listitem_1 = dropdownlist1.selectedvalue; obj.listitem_2 = dropdownlist2.selectedvalue; obj.description = textboxdescription.text; obj.date = textboxdate.text; //obj.uploadedimage = bytes; int k = obj.insertmethod(); textboxdescription.text = ""; }
here work
class contains insertmethod()
logic:
public class work { clssqlconnection obj = new clssqlconnection(); public string listitem_1 { get; set; } public string listitem_2 { get; set; } public string description { get; set; } public string date { get; set; } //public byte[] uploadedimage { get; set; } public int insertmethod() { obj.str = @"insert [assign_work] (listitem_1, listitem_2, description, date, uploadedimage)" + "values('" + listitem_1 + "','" + listitem_2 + "','" + description + "','" + date + "','" + uploadedimage + "')"; return obj.executenonquery(); } }
the image needs go database via parameter. cannot have in raw sql statement. try this:
public int insertmethod() { obj.str = @"insert [assign_work] (listitem_1, listitem_2, description, date, uploadedimage)" + "values('" + listitem_1 + "','" + listitem_2 + "','" + description + "','" + date + "', ?)"; obj.parameters.addwithvalue("file", uploadedimage); return obj.executenonquery(); }
also, btw, might want consider using parameters of these values avoid injection attacks. instance, if description field had apostrophe in it?
Comments
Post a Comment