c# - Internal Server Error in JavaScript ajax with asp.net -
i'm still beginner in javascript , ajax. have problem in code try upload image ajaxfileupload error javascript popup, saying has internal server error. possible mistakes made in code?
here aspx code:
<%@ page language="c#" autoeventwireup="true" codefile="createbrands.aspx.cs" inherits="pages_createbrands" %> <%@ register assembly="ajaxcontroltoolkit" namespace="ajaxcontroltoolkit" tagprefix="asp"%> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>music store</title> <script src="../javascript/jquery-1.11.2.min.js"></script> <script type="text/javascript"> function uploadcomplete(sender, args) { var txt = document.getelementbyid("validatepicture1");//your hiddenfield id txt.value = "1"; $.ajax({ type: "post", url: "createbrands.aspx/getpath1", contenttype: "application/json; charset=utf-8", datatype: "json", success: function (result) { onsuccess1(result.d); }, error: function (xhr, status, error) { onfailure1(xhr,status,error); } }); } function onsuccess1(result) { var pp = document.getelementbyid("picturepath1"); pp.value = result; } function onfailure1(xhr,status,error) { alert("request: " + xhr + "\n\nstatus: " + status + "\n\nerror: " + error); } </script> <script type="text/javascript"> function uploadcomplete2(sender, args) { var txt = document.getelementbyid("validatepicture2");//your hiddenfield id txt.value = "1"; $.ajax({ type: "post", url: "createbrands.aspx/getpath2", contenttype: "application/json; charset=utf-8", datatype: "json", success: function (result) { onsuccess1(result.d); }, error: function (xhr, status, error) { onfailure1(xhr,status,error); } }); } function onsuccess1(result2) { var pp = document.getelementbyid("picturepath2"); pp.value = result; } function onfailure1(xhr,status,error) { alert("request: " + xhr + "\n\nstatus: " + status + "\n\nerror: " + error); } </script> </head> <body> <form id="form1" runat="server"> <asp:scriptmanager id="scriptmanager1" runat="server"> </asp:scriptmanager> <div id="wrapper"> <div id="content_area"> <h3> </h3> <h3 class="headingtitle">create new brand(step 1 of 2):</h3> <table cellspacing="15" class="brandstable"> <br/> <h3 class="headingtitle">create new item(step 2 of 2):</h3> <table cellspacing="15" class="brandstable"> <tr> <td style="width: 160px; height: 37px;"> <strong>item type:</strong></td> <td style="height: 37px"> <asp:radiobutton id="itemtype1" runat="server" text="guitar" autopostback="false" groupname="itemtype"/> <asp:radiobutton id="itemtype2" runat="server" text="bass" autopostback="false" groupname="itemtype"/> </td> </tr> <tr> <td style="width: 160px; height: 37px;"> <strong>item image1:</strong></td> <td style="height: 37px"> <br /> <asp:ajaxfileupload id="itemfileupload1" runat="server" onuploadcomplete="itemuploadimage1_click" onclientuploadcomplete="uploadcomplete" maximumnumberoffiles="1"/> <asp:hiddenfield id="validatepicture1" value="" runat="server" /> </td> </tr> <tr> <td style="width: 160px; height: 37px;"> <strong>item image2:</strong></td> <td style="height: 37px"> <br /> <asp:ajaxfileupload id="itemfileupload2" runat="server" onuploadcomplete="itemuploadimage2_click" onclientuploadcomplete="uploadcomplete2" maximumnumberoffiles="1"/> <asp:hiddenfield id="validatepicture2" value="" runat="server" /> </td> </tr> </table> <asp:label id="lblresult2" runat="server" text=""> </asp:label> <br /> <asp:button id="button1" runat="server" cssclass="submitbutton" text="save item" onclick="button1_click"/> </div> </div> </form> </body> </html>
and here code-behind. way, has object reference null in method getpath1 line of code: string retval = session["picturepath1"].tostring();:
using system; using system.collections; using system.collections.generic; using system.io; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using ajaxcontroltoolkit; public partial class pages_createbrands : system.web.ui.page { protected void page_load(object sender, eventargs e) { } private void cleartextfields2() { itembrand.text = ""; itemmodel.text = ""; itemprice.text = ""; itemdescription.text = ""; itemnecktype.text = ""; itembody.text = ""; itemfretboard.text = ""; itemfret.text = ""; itemneckpickup.text = ""; itembridgepickup.text = ""; itembridge.text = ""; itemhardwarecolor.text = ""; } [system.web.services.webmethod] public string getpath1() { system.web.sessionstate.httpsessionstate session = system.web.httpcontext.current.session; string retval = session["picturepath1"].tostring(); session["picturepath1"] = null; return retval; } [system.web.services.webmethod] public string getpath2() { system.web.sessionstate.httpsessionstate session = system.web.httpcontext.current.session; string retval = session["picturepath2"].tostring(); session["picturepath2"] = null; return retval; } protected void itemuploadimage1_click(object sender, ajaxfileuploadeventargs e) { if (itemtype1.checked) { string filename = e.filename; session["picturepath1"] = filename; itemfileupload1.saveas(server.mappath("~/images/brands/string instrument items/guitar/") + filename); } else if(itemtype2.checked) { string filename = e.filename; session["picturepath1"] = filename; itemfileupload1.saveas(server.mappath("~/images/brands/string instrument items/bass/") + filename); } } protected void itemuploadimage2_click(object sender, ajaxfileuploadeventargs e) { if (itemtype1.checked) { string filename = e.filename; session["picturepath2"] = filename; itemfileupload2.saveas(server.mappath("~/images/brands/string instrument items/guitar/") + filename); } else if (itemtype2.checked) { string filename = e.filename; session["picturepath2"] = filename; itemfileupload2.saveas(server.mappath("~/images/brands/string instrument items/bass/") + filename); } } protected void button1_click(object sender, eventargs e) { if (itemtype1.checked) { int item_type = connectionclassbrands.getidbytype(itemtype1.text); int item_brandid = connectionclassbrands.getidbybrand(itembrand.text); string item_model = itemmodel.text; double item_price = convert.todouble(itemprice.text); string item_image1 = session["picturepath1"].tostring(); string item_image2 = session["picturepath2"].tostring(); string item_description = itemdescription.text; string item_necktype = itemnecktype.text; string item_body = itembody.text; string item_fretboard = itemfretboard.text; string item_fret = itemfret.text; string item_bridge = itembridge.text; string item_neckpickup = itemneckpickup.text; string item_bridgepickup = itembridgepickup.text; string item_hardwarecolor = itemhardwarecolor.text; connectionclassguitaritems.addstringinstrumentitems(item_type, item_brandid, item_model, item_price, item_image1, item_image2, item_description, item_necktype, item_body, item_fretboard, item_fret, item_bridge, item_neckpickup, item_bridgepickup, item_hardwarecolor); lblresult2.text = "upload successful!" + item_image1 + " , " + item_image2; } else if (itemtype2.checked) { try { int item_type = connectionclassbrands.getidbytype(itemtype2.text); int item_brandid = connectionclassbrands.getidbybrand(itembrand.text); string item_model = itemmodel.text; double item_price = convert.todouble(itemprice.text); string item_image1 = session["picturepath1"].tostring(); string item_image2 = session["picturepath2"].tostring(); string item_description = itemdescription.text; string item_necktype = itemnecktype.text; string item_body = itembody.text; string item_fretboard = itemfretboard.text; string item_fret = itemfret.text; string item_bridge = itembridge.text; string item_neckpickup = itemneckpickup.text; string item_bridgepickup = itembridgepickup.text; string item_hardwarecolor = itemhardwarecolor.text; connectionclassguitaritems.addstringinstrumentitems(item_type, item_brandid, item_model, item_price, item_image1, item_image2, item_description, item_necktype, item_body, item_fretboard, item_fret, item_bridge, item_neckpickup, item_bridgepickup, item_hardwarecolor); lblresult2.text = "upload successful!"; cleartextfields2(); } catch (exception ex) { lblresult2.text = ex.message; } } else { response.redirect("~/pages/overviewguitardata.aspx"); } } }
Comments
Post a Comment