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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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> </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;
}
}