I am using following jquery code in an aspx page:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
$(document).ready(function () {
$("#<%= Button1.ClientID %>").click(function () {
var UserName = $('#<%=TextBox1.ClientID%>').val();
var Phone = $('#<%=TextBox3.ClientID%>').val();
var Email = $('#<%=TextBox2.ClientID%>').val();
var Message = $('#<%=TextBox4.ClientID%>').val();
and i am also using the following code in csharp file:
public static void insertdetails(string name,string emailid,string phone,string message)
{
SqlConnection Con = new SqlConnection(@"Server=HP-PC;Integrated Security=True;" + "Database=DB1");
Console.WriteLine("connected to db");
string query = "INSERT INTO [SubmissionFormTable]([Name],[Email],[Phone],[Message])" +
"VALUES (@Name,@Email,@Phone,@Message)";
Hero1234567
Member
2 Points
25 Posts
error while inserting values into a table using jquery("NetworkError: 405 Method Not Allowed )
Dec 08, 2012 06:13 AM|LINK
I am using following jquery code in an aspx page:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
$(document).ready(function () {
$("#<%= Button1.ClientID %>").click(function () {
var UserName = $('#<%=TextBox1.ClientID%>').val();
var Phone = $('#<%=TextBox3.ClientID%>').val();
var Email = $('#<%=TextBox2.ClientID%>').val();
var Message = $('#<%=TextBox4.ClientID%>').val();
var json = "{'" + UserName + "','" + Phone + "','" + Email + "''" + Message + "'}";
alert(UserName);
$.ajax({
type: "POST",
//C:\Users\hp\Documents\Visual Studio 2010\WebSites\
//C:/Users/hp/Documents/Visual Studio 2010/WebSites/jQuerySubmitForm/
url: "/jQuerySubmitForm/JqueryFunction/insertdetails",
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function () { alert("success"); },
error: OnError,
failure: function () {
alert(response);
}
});
function OnError(request, status, error) {
alert(request.statusText);
return false;
}
// var name = $('#<%=TextBox1.ClientID%>').val();
// var email = $('#<%=TextBox2.ClientID%>').val();
// var phone = $('#<%=TextBox3.ClientID%>').val();
// var message = $('#<%=TextBox4.ClientID%>').val();
// var data = JSON.stringify({ name: name, email: email, phone: phone, message: message });
// alert("comes here");
// alert(data);
// alert(url);
// $.ajax({
// type: "POST",
// url: "JqueryFunction.aspx/insertdetails",
// data: data,
// contentType: "application/json",
// datatype: "json",
// processdata: true,
// success: function () { alert("success"); },
// error: function () { alert("error"); }
// });
});
});
</script>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
width: 455px;
text-align: center;
}
</style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
<table class="style1">
<tr>
<td class="style2" style="text-align: center">
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server" Text="Email ID"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="251px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server" Text="Phone"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="Message"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" Width="248px"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="Button1" runat="server" Text="Submit" />
</td>
</tr>
</table>
</p>
</asp:Content>
and i am also using the following code in csharp file:
public static void insertdetails(string name,string emailid,string phone,string message)
{
SqlConnection Con = new SqlConnection(@"Server=HP-PC;Integrated Security=True;" + "Database=DB1");
Console.WriteLine("connected to db");
string query = "INSERT INTO [SubmissionFormTable]([Name],[Email],[Phone],[Message])" +
"VALUES (@Name,@Email,@Phone,@Message)";
SqlCommand cmd = new SqlCommand(query, Con);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Email", emailid);
cmd.Parameters.AddWithValue("@Phone", phone);
cmd.Parameters.AddWithValue("@Message", message);
try
{
Con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
Con.Close();
}
That will be great if any one helps me clear out the error...
SohailShaikh
Contributor
6119 Points
1167 Posts
Re: error while inserting values into a table using jquery("NetworkError: 405 Method Not Allowed ...
Dec 08, 2012 06:51 AM|LINK
Please change the value in json format with the help of
var json = "{'" + UserName + "','" + Phone + "','" + Email + "''" + Message + "'}";Sohail Shaikh
Hero1234567
Member
2 Points
25 Posts
Re: error while inserting values into a table using jquery("NetworkError: 405 Method Not Allowed ...
Dec 08, 2012 06:58 AM|LINK
Sohail, made the appropriate changes suggested but no luck yet....
SohailShaikh
Contributor
6119 Points
1167 Posts
Re: error while inserting values into a table using jquery("NetworkError: 405 Method Not Allowed ...
Dec 08, 2012 07:06 AM|LINK
Are you calling web method or page method through Jquery
you can download furhter help full program from my skydrive
https://skydrive.live.com/#cid=2F22272220E37707&id=2F22272220E37707%21103
following are the program name in this i m getting data from database and also insert from database through jquery call web service
CheckUserNameAndEmailAvailable_Webservice
JsonListHandel
LoginWithJquery
Sohail Shaikh
asteranup
All-Star
30184 Points
4906 Posts
Re: error while inserting values into a table using jquery("NetworkError: 405 Method Not Allowed ...
Dec 10, 2012 10:02 AM|LINK
Hi,
Put a debugger intoerror function of the ajax call and check the inner server error
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog