views:

290

answers:

3

Hi, I want to insert text field values to database using javascript jquery ajax call to C# server method. problem is that ajax call run successfully but C# is not updating the database. what could be the reason. please reply soon.asp code is giving below:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dbTest.aspx.cs" Inherits="dbTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head runat="server">
    <title></title>
     <script src="jquery.js" type="text/javascript"></script>
     <script src="json2.js" type="text/javascript"></script>

     <script type="text/javascript">

         function SaveOnClick() {
             $.ajax({
                 type: "POST",
                 url: "dbTest.aspx/SaveData",
                 data: "{'CustomerName': '" + $('#txtCustomerName').val() + "', 'CustomerPhoneNumber': '" + $('#txtCustomerPhoneNumber').val() + "', 'ColorID': '" + $('#txtColorID').val() + "'}",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 success: function(result, txtstatus) {
                     alert(txtstatus);
                 },
                 error: function(result) {
                     alert("jQuery Error:" + result.statusText);
                 }
             });
         }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="Customers" runat="server">
      </asp:DropDownList>
    </div>
    <div id="CustomerDetails">
    </div>
    <table border="0">
<tr>
 <td align="right"><strong>Customer Name:</strong></td>
 <td align="left">
  <asp:TextBox id="txtCustomerName" maxlength="50" runat="server" />
</td>
</tr>
<tr>
 <td align="right"><strong>Customer Phone Number:</strong></td>
 <td align="left">
  <asp:TextBox id="txtCustomerPhoneNumber" runat="server" />
</td>
</tr>
<tr>
 <td align="right"><strong>Color ID:</strong></td>
 <td align="left">
  <asp:TextBox id="txtColorId" runat="server" />
</td>
</tr>
<tr>
 <td>&nbsp;</td>
</tr>
</table>

<input type="button" onclick="SaveOnClick()" value="click me"/>

<asp:Panel id="pnlConfirm" runat="server">
 <h2>Thanks for submitting your information to us!</h2>

</asp:Panel>

    </form>
</body>
</html>

Below is C# file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Services;
using System.Web.UI.WebControls;
using System.Data.SqlClient;


public partial class dbTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        pnlConfirm.Visible = false;
    }
    [WebMethod]
    public static void SaveData(string CustomerName, string CustomerPhoneNumber, string ColorID)
    {
        string Server = "al2c06";
        string Username = "app_Testing";
        string Password = "jsjsdj";
        string Database = "AmbreenTest";

        string ConnectionString = "Data Source=" + Server + ";";
        ConnectionString += "User ID=" + Username + ";";
        ConnectionString += "Password=" + Password + ";";
        ConnectionString += "Initial Catalog=" + Database;
        string query = "INSERT INTO Customer_Order(customerName, customerPhoneNumber, colorID)";
        query += "VALUES (";
        query += "'" + CustomerName+ "'";
        query += ",";
        query += "'" + CustomerPhoneNumber + "'";
        query += ",";
        query += "'" + ColorID+ "'";
        query += ")";

        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
            }
        }
        //lblSQL.Text = query;
        //pnlConfirm.Visible = true;
    }
}
A: 

You didn't get the posted values. That is the problem.

[WebMethod]
    public static void SaveData(string CustomerName, string CustomerPhoneNumber, string ColorID)
    {
        string Server = "al2c06";
        string Username = "app_AmbreenTesting";
        string Password = "testing";
        string Database = "AmbreenTesting";

        string ConnectionString = "Data Source=" + Server + ";";
        ConnectionString += "User ID=" + Username + ";";
        ConnectionString += "Password=" + Password + ";";
        ConnectionString += "Initial Catalog=" + Database;
        string query = "INSERT INTO Customer_Order(customerName, customerPhoneNumber, colorID)";
        query += "VALUES (";
        query += "'" + Request.Form["CustomerName"].ToString()+ "'";
        query += ",";
        query += "'" + Request.Form["CustomerPhoneNumber"].ToString() + "'";
        query += ",";
        query += "'" + Request.Form["ColorID"].ToString()+ "'";
        query += ")";

        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
        //lblSQL.Text = query;
        //pnlConfirm.Visible = true;
    }

HTH

Raja
The posted values are the parameters to his method, but as I comment, I am not sure how it is hooked up to be run, if it is at all.Anyways, this code will fail, because you are in a static Context, so you can't access ´Request´.
driis
Oh ya you are right...I usually have a web method and get the information from Request.Form. By the way what is the error??
Raja
@Raja:I tried ur method and made my web method non-static to use Request.Form but then jquery error occur
amby
A: 

I think what you need is:

using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
Andrew Myhre
That's certainly wrong.
driis
Ah yes, my bad. Thanks for the downvote
Andrew Myhre
A: 

anyone please answer what i m doing wrong here

amby
Answering your own question without actually answering it, for a start. Delete this answer and add it as a comment to the original question before it's downvoted.
Dolph