ajax nothing written to the database RRS feed

  • Question

  • User181930479 posted

    im trying to send data from sidenav located in my default.aspx page to another page called addstudy.aspx . im getting the alert that the study was inserted in the database , but in fact nothing is inserted , instead , in my console im getting , 500 internal server error , any help please? my code is below

    Default.aspx ( on button click)

    <script type="text/javascript">
           function insert() {
               var xmlhttp = new XMLHttpRequest();
               var anchors = document.getElementById('mySidenav').getElementsByTagName('a');
               for (var i = 0; i < anchors.length; i++) {
               xmlhttp.open("GET", "addnewstudy.aspx?nm=" + anchors[i].text + "&gd=" + anchors[i].href + "&opr=insert", false);
               alert("New Study inserted in database");

    then in my addnewstudy.aspx

      SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=LLDB;Integrated Security=True;Pooling=False");
        string name;
        string gender;
        string opr;
        protected void Page_Load(object sender, EventArgs e)
            opr = Request.QueryString["opr"].ToString();
            if (opr == "insert")
                name = Request.QueryString["nm"].ToString();
                gender = Request.QueryString["gd"].ToString();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into tbl_myresearches (NodeName,NodeLink) values('" + name.ToString() + "','" + gender.ToString() + "')";
    Thursday, December 7, 2017 7:01 AM

All replies

  • User-1716253493 posted

    How about if you open the url dierctly in address bar?


    or you can evaluate what the anchor text and href value, may content some unwanted characters

    Thursday, December 7, 2017 7:31 AM
  • User181930479 posted

    if i open the url directly in the browser , and fill in the nm and the gender , values that i fill  are written in the database....

    i am stuck i cant figure out what is happening...

    Thursday, December 7, 2017 7:43 AM
  • User475983607 posted

    The posted code looks ok. There is probably an issue elsewhere in the code base. 

    Keep in mind that synchronous AJAX request has been deprecated in some browsers which might have something to do with the issue.



    Plus the code is not handling the response.

    Consider changing the design a bit to use a Web Method rather than a calling the page and running through the page life cycle.  Rather than sending a bunch of requests, I would send an array of objects. 

    Thursday, December 7, 2017 2:22 PM
  • User61956409 posted

    Hi NAF,<o:p></o:p>

    Your code seem ok, and I do a test on my side with your code, which works for me. To troubleshoot the issue, you can try to specify testing values for querystrings and set breakpoint inside Page_Load event of addnewstudy.aspx, and then you can debug your code to check if it works as expected. <o:p>

    var xmlhttp = new XMLHttpRequest();
    var nm = "testnm";
    var gd = "testgd";
    xmlhttp.open("GET", "addnewstudy.aspx?nm=" + nm + "&gd=" + gd + "&opr=insert", false);

    With Regards, <o:p></o:p>

    Fei Han<o:p></o:p>

    Thursday, December 21, 2017 5:41 AM