javascript - Ajax sometimes working sometimes it doesn't -


i have problem ajax code have in asp.net website. ajax supposed retrieve filename of uploaded image through ajaxfileupload asp.net server. unfortunately outcome is, give me object reference not set instance of object in asp.net code-behind because doesn't return value image uploaded using ajaxfileupload. , after bunch of tries, work. , after couple of minutes, not work again. cycle code work, not , work again. don't know if problem ajax code or maybe internet connection. kindly me guys on figuring 1 out.

here code aspx(i have included of code main focus on ajax script , ajaxfileupload has id of itemfileupload1 , itemfileupload2):

<%@ 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("validatepicture");//your                hiddenfield id         txt.value = "1";          $.ajax({             type: "post",             url: "pages/createbrands.aspx",             contenttype: "application/json; charset=utf-8",             datatype: "json",             success: function (result1) {                 onsuccess1(result1.d);             },             error: function (xhr, status, error) {                 onfailure1(error);             }         });     }     function onsuccess1(result1) {         var pp1 = document.getelementbyid("picturepath1");         pp1.value = result1;     }     function onfailure1(error) {         alert(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: "pages/createbrands.aspx",             contenttype: "application/json; charset=utf-8",             datatype: "json",             success: function (result2) {                 onsuccess1(result2.d);             },             error: function (xhr, status, error) {                 onfailure1(error);             }         });     }     function onsuccess1(result2) {         var pp2 = document.getelementbyid("picturepath2");         pp2.value = result2;     }     function onfailure1(error) {         alert(error);     } </script>  <link rel="stylesheet" href="~/styles/jquery.bxslider.css"/> <link rel="stylesheet" href="~/styles/stylesheet.css"/> <link rel="stylesheet" href="~/styles/font-awesome.min.css"/> <link rel="shortcut icon" type="image/png" href="~/images/rocksign.png"/>  <style type="text/css">     .auto-style1 {         width: 160px;         height: 37px;     }     .auto-style2 {         height: 37px;     } </style>    </head>  <body>  <form id="form1" runat="server">     <asp:scriptmanager id="scriptmanager1" runat="server">           </asp:scriptmanager>      <div id="wrapper">          <header id="main_header">             <div id="callout">                 <h2>&#9742; 111222333</h2>                 <p>michigan state kawasaki iceland</p>             </div>             <h1>music store</h1>         </header>          <div class="clearfix"></div>          <nav id="nav_menu">             <ul id="nav">                 <li><a href="~/pages/home.aspx" runat="server">home</a></li>                 <li><a href="#">instruments</a>                     <ul class="sub-menu">                         <li><a href="~/pages/guitarbrands.aspx"       runat="server">electric guitars</a></li>                         <li><a href="~/pages/bassbrands.aspx"       runat="server">bass guitars</a></li>                         <li><a href="#">drums</a></li>                     </ul>                 </li>                 <li><a href="#">amplifiers</a></li>                 <li><a href="#">accessories</a></li>                 <li><a href="#">featured artists</a></li>                 <li><a href="#"><img src="../images/cog.png" /></a>                     <ul class="cog_submenu">                         <li><a href="~/pages/createbrands.aspx"      runat="server">create new brand</a></li>                         <li><a href="#">use existing brand</a></li>                         <li><a href="~/pages/overviewguitardata.aspx"      runat="server">guitar data overview</a></li>                         <li><a href="~/pages/overviewbassdata.aspx"      runat="server">bass data overview</a></li>                     </ul>                </li>        </ul>         </nav>          <div id="content_area">             <h3>&nbsp;</h3>             <h3 class="headingtitle">create new brand(step 1 of 2):</h3>              <table cellspacing="15" class="brandstable">                  <tr>                     <td style="width: 160px; height: 37px;">                      <strong>brand type:</strong></td>                      <td style="height: 37px">                          <asp:checkbox id="brandtypeguitar" runat="server"      text="guitar" autopostback="true" groupname="brandtype"/>                         <asp:checkbox id="brandtypebass" runat="server"      text="bass" autopostback="true" groupname="brandtype"/>                      </td>                 </tr>                  <tr>                     <td style="width: 160px; height: 37px;">                      <strong>brand name:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="brandname" runat="server"      backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px"      width="300px"></asp:textbox>                      </td>                 </tr>                  <tr>                     <td class="auto-style1">                      <strong>brand image:</strong></td>                      <td class="auto-style2">                     <br/>                     <asp:fileupload id="brandfileupload" runat="server" />                     <asp:button id="branduploadimage" runat="server"      text="upload image" onclick="branduploadimage_click" />                     </td>                 </tr>             </table>              <asp:label id="lblresult" runat="server" text=""></asp:label>             <br />             <asp:button id="btnsave" runat="server" cssclass="submitbutton"      text="save brand" onclick="btnsave_click" />             <br/>             <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"      oncheckedchanged="itemtype1_checkedchanged"/>                         <asp:radiobutton id="itemtype2" runat="server"      text="bass" autopostback="false" groupname="itemtype"      oncheckedchanged="itemtype1_checkedchanged"/>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item brand:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itembrand" runat="server"      backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px"      width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item model:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itemmodel" runat="server"      backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px"      width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item price:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itemprice" runat="server"       backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px"       width="300px"></asp:textbox>                      </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"/>                      </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"/>                      </td>                 </tr>                  <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item description:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itemdescription" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item neck type:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itemnecktype" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item body:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itembody" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item fretboard:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itemfretboard" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item fret:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itemfret" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item bridge:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itembridge" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item neck pickup:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itemneckpickup" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item bridge pickup:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itembridgepickup" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </td>                 </tr>                 <tr>                     <td style="width: 160px; height: 37px;">                      <strong>item hardware color:</strong></td>                      <td style="height: 37px">                      <asp:textbox id="itemhardwarecolor" runat="server" backcolor="#ffff66" bordercolor="black" borderwidth="1px" height="20px" width="300px"></asp:textbox>                      </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 class="clearfix"></div>          <footer>             <p>&copy;all rights reserved</p>         </footer>       </div>  </form> 

here code-behind(i included every code in code-behind main focus on mehtod name itemuploadimage1_click, itemuploadimage2_click , button1_click. error in button1_click , point out string item_image1 null):

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 cleartextfields() {     brandname.text = ""; }  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 = ""; }  protected void branduploadimage_click(object sender, eventargs e) {      try     {         string filename = path.getfilename(brandfileupload.filename);         brandfileupload.saveas(server.mappath("~/images/brands/") +         filename);         lblresult2.text = "image " + filename + " uploaded!";     }     catch (exception)     {         lblresult.text = "upload failed!";     }  }   protected void itemtype1_checkedchanged(object sender, eventargs e)  {     session["itemtype"] = (sender radiobutton).text;  }   protected void itemuploadimage1_click(object sender, ajaxfileuploadeventargs   e) {     if (session["itemtype"] != null) {         if (session["itemtype"].tostring() == "guitar")         {             string filename = e.filename;             session["picturepath1"] = filename;             itemfileupload1.saveas(server.mappath("~/images/brands/string  instrument items/guitar/") + filename);         }         else if (session["itemtype"].tostring() == "bass")         {             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 (session["itemtype"] != null) {         if (session["itemtype"].tostring() == "guitar")         {             string filename = e.filename;             session["picturepath2"] = filename;             itemfileupload2.saveas(server.mappath("~/images/brands/string instrument items/guitar/") + filename);         }         else if (session["itemtype"].tostring() == "bass")         {             string filename = e.filename;             session["picturepath2"] = filename;             itemfileupload2.saveas(server.mappath("~/images/brands/string instrument items/bass/") + filename);         }     } }   protected void btnsave_click(object sender, eventargs e) {      if (brandtypeguitar.checked && !brandtypebass.checked)     {         try         {             string brand_image = connectionclassbrands.brandfilename;             string brand_name = brandname.text;              connectionclassbrands.addbrandsforonetype(brandtypeguitar.text, brand_name, brand_image);             lblresult.text = "upload successful!";          }         catch (exception)         {             lblresult.text = "upload failed!";         }     }     else if (brandtypebass.checked && !brandtypeguitar.checked)     {         try         {             string brand_image = connectionclassbrands.brandfilename;             string brand_name = brandname.text;              connectionclassbrands.addbrandsforonetype(brandtypebass.text, brand_name, brand_image);             lblresult.text = "upload successful!";          }         catch (exception)         {             lblresult.text = "upload failed!";         }     }     else if (brandtypeguitar.checked && brandtypebass.checked)     {         try         {             string brand_image = connectionclassbrands.brandfilename;             string brand_name = brandname.text;              connectionclassbrands.addbrandsfortwotypes(brandtypeguitar.text, brandtypebass.text, brand_name, brand_image);             lblresult.text = "upload successful!";          }         catch (exception)         {             lblresult.text = "upload failed!";         }     }     else     {         lblresult.text = "upload failed!";     }        cleartextfields();  }  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;         }       }    }      } 

here stack trace error:

object reference not set instance of object.  description: unhandled exception occurred during execution of  current web request. please review stack trace more information  error , originated in code.   exception details: system.nullreferenceexception: object reference not set  instance of object.  source error:    line 168:                string item_model = itemmodel.text; line 169:                double item_price =  convert.todouble(itemprice.text); line 170:                string item_image1 =  session["picturepath1"].tostring(); line 171:                string item_image2 =  session["picturepath2"].tostring(); line 172:                string item_description = itemdescription.text;  source file: c:\users\user1\documents\visual studio  2015\websites\musicstore\pages\createbrands.aspx.cs    line: 170   stack trace:    [nullreferenceexception: object reference not set instance of  object.] pages_createbrands.button1_click(object sender, eventargs e) in  c:\users\user1\documents\visual studio  2015\websites\musicstore\pages\createbrands.aspx.cs:170 system.web.ui.webcontrols.button.onclick(eventargs e) +9696694 system.web.ui.webcontrols.button.raisepostbackevent(string eventargument)  +204   system.web.ui.webcontrols.button.system.web.ui.ipostbackeventhandler .raisepostbackevent(string eventargument) +12 system.web.ui.page.raisepostbackevent(ipostbackeventhandler sourcecontrol,  string eventargument) +15 system.web.ui.page.raisepostbackevent(namevaluecollection postdata) +35 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint,  boolean includestagesafterasyncpoint) +1639 

there issues in code :

 1. ajax url :      url: "pages/createbrands.aspx" // worng     correct url : "createbrands.aspx/pages"      //also ensure pages method must there in createbrands.aspx.cs page.      2. files name coming null      string filename = e.filename;      //check if e.filename null or not before setting session variable.      //if null can try using normal asp:fileupload      , through jquery can make ajax call of fileupload id.       $('#itemfileupload1').click(function() {..your ajax call inside..});      3. using       session["picturepath1"] //which fine       //but in aspx page accessing       document.getelementbyid("picturepath1");       //this wrong cannot access session variable .you can      directly use session variable in aspx page like:      "<%=session["picturepath1"]%>". 

try making these changes , check if works or not thanks!!


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -