How to
save data in Sql Server using $("#form1").serialize() with Ajax and Jquery in Asp.Net
Note: here we will discuss about how to save in Sql
Server without [WebMethod] means here we are using $("#form1").serialize
concept to passing data to server and after passing we get the passed data with query string
-- Sql Server.....
use
test
create table Student
(
Id int primary key,
Name varchar(50),
Age int,
PhoneNo varchar(50)
)
Source Code for
Default.aspx…………………………….
<%@ Page Language="C#"
AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication3.WebForm1"
%>
<!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="Scripts/jquery-1.4.1.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function ()
{
$("[type='button']").not("#Button1").bind("click",
function ()
{
//
$(".a").bind("click", function () {
$("#txtid").val($($(this).closest("tr").find("td")[0]).text().trim());
$("#txtname").val($($(this).closest("tr").find("td")[1]).text().trim());
$("#txtage").val($($(this).closest("tr").find("td")[2]).text().trim());
$("#txtphoneno").val($($(this).closest("tr").find("td")[3]).text().trim());
});
$("#Button1").click(function ()
{
debugger;
var data = $("#form1").serialize();
$.ajax({
data: data,
url: "Webform1.aspx",
success:
function (response) {
var row = "<tr><td>"
+ $("#txtid").val() + "</td><td>" + $("#txtname").val() + "</td><td>" + $("#txtage").val() + "</td><td>" + $("#txtphoneno").val() + "</td><td> <input type='button' class='a'
value='select' /></td></tr>";
$("#GridView1").find("tbody").append(row);
},
error: function (response)
{
alert(response);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div name="abc">
<table width="50%">
<tr><td colspan="2"><b>How to save
data in Sql Server using $("#form1").serialize() with Ajax and Jquery in Asp.Net</b></td></tr>
<tr><td>Id</td><td> <input id="txtid" name="Text1" type="text" /></td></tr>
<tr><td>Name</td><td> <input id="txtname"
name="Text2"
type="text"
/></td></tr>
<tr><td>Age</td><td><input id="txtage" name="Text3" type="text" /></td></tr>
<tr><td>PhoneNo</td><td><input id="txtphoneno" name="Text4" type="text" /></td></tr>
<tr><td><input id="Button1"
type="button"
value="Save" /></td></tr>
<tr><td colspan="2">
<center>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
ID
</HeaderTemplate>
<ItemTemplate>
<%#Eval("id")
%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<%#Eval("Name")
%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Age
</HeaderTemplate>
<ItemTemplate>
<%#Eval("Age")
%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Phone
No
</HeaderTemplate>
<ItemTemplate>
<%#Eval("phoneno")
%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Button
</HeaderTemplate>
<ItemTemplate>
<input type="button"
class="a"
value="select"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</center>
</td></tr>
</table>
</div>
</form>
</body>
</html>
Source Code for
Default.aspx.cs…………………………….
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication3
{
public partial class WebForm1 :
System.Web.UI.Page
{
SqlConnection con = new SqlConnection("data source=CETPA1;database=test;integrated
security=true;");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs
e)
{
if (this.Request.QueryString["Text1"] != null)
{
try
{
int id = Convert.ToInt32(this.Request.QueryString["Text1"].ToString());
string name = this.Request.QueryString["Text2"].ToString();
int age = Convert.ToInt32(this.Request.QueryString["Text3"].ToString());
string phno = this.Request.QueryString["Text4"].ToString();
cmd = new SqlCommand("insert into Student values(" + id + ",'" + name + "',"
+ age + ",'" + phno + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
showalldata();
}
catch (Exception
ex)
{
throw new Exception("Error
in Insertion.");
}
}
else
{
}
showalldata();
}
private void
showalldata()
{
cmd = new SqlCommand("select * from Student", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource
= ds;
GridView1.DataBind();
}
}
}
Result
0 comments:
Post a Comment