sql - ASP.Net C# failed to UPDATE picture but success when INSERT new picture to Database -
i using .ashx file imagehandler.
the error message received exception of type 'system.invalidoperationexception' occurred in system.data.dll not handled in user code
additional information: invalid attempt read when no data present.
this part showing error sqldatareader dr = cmd.executereader(); dr.read(); context.response.contenttype = dr["image_type"].tostring(); context.response.binarywrite((byte[])dr["profile_picture"]); dr.close();
this imagehandler.ashx:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.sqlclient; using system.data; using system.configuration; using system.io; public class imagehandler : ihttphandler { public void processrequest (httpcontext context) { sqlconnection myconnection = new sqlconnection(configurationmanager.connectionstrings["register"].connectionstring); myconnection.open(); string sql = "select profile_picture, image_type member login_id=@name"; sqlcommand cmd = new sqlcommand(sql, myconnection); cmd.parameters.add("@name", sqldbtype.nvarchar).value = context.request.querystring["login_id"]; sqldatareader dr = cmd.executereader(); dr.read(); context.response.contenttype = dr["image_type"].tostring(); context.response.binarywrite((byte[])dr["profile_picture"]); dr.close(); myconnection.close(); } public bool isreusable { { return false; } } }
this updateprofile.aspx.cs:
protected void btnsave_click(object sender, eventargs e) { string constr = configurationmanager.connectionstrings["register"].connectionstring; string filename = path.getfilename(fuprofilepicture.postedfile.filename); string contenttype = fuprofilepicture.postedfile.contenttype; using (stream fs = fuprofilepicture.postedfile.inputstream) { using (binaryreader br = new binaryreader(fs)) { byte[] bytes = br.readbytes((int32)fs.length); using (sqlconnection con1 = new sqlconnection(constr)) { string query1 = "update member set name = @name, email = @email, phone_number = @phonenumber, gender = gender, date_of_birth = @dob, password = @password, login_id = @loginid, student_id = @studentid, profile_picture = @profilepicture login_id = '" + request.querystring["login_id"] + "'"; using (sqlcommand cmd1 = new sqlcommand(query1)) { cmd1.connection = con1; cmd1.parameters.addwithvalue("@imagename", filename); cmd1.parameters.addwithvalue("@imagetype", contenttype); cmd1.parameters.addwithvalue("@profilepicture", bytes); cmd1.parameters.addwithvalue("@name", txtname.text); cmd1.parameters.addwithvalue("@email", txtemail.text); cmd1.parameters.addwithvalue("@phonenumber", txtcontactnumber.text); cmd1.parameters.addwithvalue("@gender", ddlgender.selecteditem.tostring()); cmd1.parameters.addwithvalue("@dob", ddlday.selecteditem.tostring() + "/" + ddlmonth.selecteditem.tostring() + "/" + txtyear.text); cmd1.parameters.addwithvalue("@password", txtpassword.text); cmd1.parameters.addwithvalue("@loginid", txtusername.text); cmd1.parameters.addwithvalue("@studentid", txtstudentid.text); con1.open(); cmd1.executenonquery(); con1.close(); } } } } system.text.stringbuilder sb = new system.text.stringbuilder(); sb.append("<script type = 'text/javascript'>"); sb.append("window.onload=function(){"); sb.append("alert('"); sb.append("update successfuly!"); sb.append("')};"); sb.append("</script>"); clientscript.registerclientscriptblock(this.gettype(), "alert", sb.tostring()); } }
maybe because forget @ sign on gender:
phone_number = @phonenumber, gender = gender,
Comments
Post a Comment