0

I'm still a beginner in JavaScript and ajax. I have this problem in my code where I try to upload an image from an ajaxfileupload but the error for JavaScript will always popup, saying that it has an internal server error. What possible mistakes I made in my code?

Here is the 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>&nbsp;</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 is the code-behind. By the way, it also has an object reference null in the method GetPath1 with this 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 + " and " + 
            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");
        }



    }


  }
halfer
  • 19,824
  • 17
  • 99
  • 186
Boom Click
  • 51
  • 2
  • 10
  • Internal Server error could be easily found by debug server codes. Have you debug it? – Farzin Kanzi Aug 20 '17 at 14:17
  • Possible duplicate of: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it. You use something that is null or does not exist. As @FarzinKanzi says you should be able to find out which line of code is throwing the error by debugging. – VDWWD Aug 20 '17 at 14:22
  • Did you try debugging the server side code? – Chetan Aug 20 '17 at 15:43
  • @halfer - yea I apologize, im just kinda frustrated. Been figuring this one out for weeks now. And many are saying that I should debug when in fact I have already tried debugging. Its like going to a technical support and they'll say "have you tried turning on and off the cpu?"..which is a wild guess on fixing your problem. Anyways, I'll figure it out myself or try other solutions. – Boom Click Aug 21 '17 at 02:53
  • OK, thanks. Note that unless you state that you have debugged, and make a note of what you found, people will reasonably assume you don't know what debugging is. Most code bug questions posted to Stack Overflow have not even had a cursory debugging, and experienced readers are overwhelmed by what appears to be requests for free work. Anything you can do to distinguish your question from those others is therefore very useful for everyone `:-)`. – halfer Aug 21 '17 at 10:07
  • wow! i was able to solve it. The problem originated from the if else statement that returns true if itemType1 is checked or itemType2 is checked. – Boom Click Aug 21 '17 at 12:44

0 Answers0