javascript - Insert data into database using ajax -
i using ajax insert data in database
my default .aspx file below
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ajax demo</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#<%= btn_insert.clientid %>").click(function () { //var name = $('#<%=txt_name.clientid%>').val(); //var email=$('#<%=txt_email.clientid%>').val() var email = document.getelementbyid('txt_email').value; var name = document.getelementbyid('txt_name').value; //alert("{'name':'" + name + "', 'email':'" + email + "'}"); $.ajax({ type: 'post', contenttype: "application/x-www-form-urlencoded; charset=utf-8", data: "name=" + name + "&email=" + email, //data: $('#form1') async: true, success: function (response, data) { $('#txt_name').val(''); $('#txt_email').val(''); alert("record has been saved in database"); alert(response.data.name); }, error: function () { console.log('there error'); } }); return false; }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <p> name : <asp:textbox id="txt_name" runat="server" clientidmode="static"></asp:textbox> </p> <p> e-mail : <asp:textbox id="txt_email" runat="server" clientidmode="static"></asp:textbox> </p> <p> <asp:button id="btn_insert" runat="server" text="insert" onclick="btn_insert_click"/> </p> </div> </form> </body> </html>
and .cs file below
using system; using system.collections.generic; using system.linq; using system.web; using system.web.services; using system.web.ui; using system.web.ui.webcontrols; using system.data.sqlclient; using system.configuration; using system.data; using system.io; public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) { string name = request["name"]; string email = request["email"]; insertmethod(name, email); } sqlcommand cmd; public boolean insertmethod(string name, string email) { sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["connectionstring"].connectionstring); cmd = new sqlcommand("insert dbo.demo_ajax (name,email)values (@name,@email)", conn); cmd.commandtype =commandtype.text; cmd.parameters.addwithvalue("@name",name); cmd.parameters.addwithvalue("@email", email); try { conn.open(); //cmd.executenonquery(); int affected = cmd.executenonquery(); if (affected == 1) { //response.write("bhargav"); return true; } else { return false; } } catch (exception ex) { //response.write(ex.message); return false; } { conn.close(); } } protected void btn_insert_click(object sender, eventargs e) { } }
when run insert 1 blank data in database.
i don't know how solve it.
please 1 me.
use webmethod working.
add namespace
using system.web.services;
change method like
[webmethod] public static boolean insertmethod(string name, string email)
and remove
<asp:button id="btn_insert" runat="server" text="insert" />
and remove click event .cs page working.
Comments
Post a Comment