Insert data using jquery
Demo.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#<%=btnsub.ClientID %>').click(function () {
$.ajax({
type: 'POST',
contentType: "application/json;
charset=utf-8",
url: 'insertjquery.aspx/Insertmethod',
data: "{'Name':'" +
document.getElementById('txtname').value + "', 'Email':'" + document.getElementById('txtemail').value + "','mobile':'"
+ document.getElementById('txtmobile').value
+ "','company':'" +
document.getElementById('txtcompany').value +
"','salary':'" +
document.getElementById('txtsalary').value + "'}",
async: false,
success: function (response) {
$('#<%=txtname.ClientID
%>').val('');
$('#<%=txtemail.ClientID%>').val('');
$('#<%=txtmobile.ClientID
%>').val('');
$('#<%=txtcompany.ClientID%>').val('');
$('#<%=txtsalary.ClientID%>').val('');
alert("Record Has been Saved in Database");
},
error: function () {
console.log('there is some error');
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>Insert</legend>
<table>
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="txtname" runat="server" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="txtemail" runat="server" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Mobile:
</td>
<td>
<asp:TextBox ID="txtmobile" runat="server" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Company:
</td>
<td>
<asp:TextBox ID="txtcompany" runat="server" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Salary:
</td>
<td>
<asp:TextBox ID="txtsalary" runat="server" ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnsub" runat="server" Text="submit" ClientIDMode="Static" />
</td>
<td>
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
Demo.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.Web.Services;
public partial class insertjquery
: System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string Insertmethod(string
Name, string Email, string
mobile, string company, int salary)
{
Bal_generat obg = new Bal_generat();
return obg.insert("Insert",
Name, Email, mobile, company, salary);
}
}
Bal_genera.cs
public string insert(string mode,string
name,string email,string
mobile,string company,int
salary)
{
SqlParameter[] para = new
SqlParameter[6];
para[0] = obget.Getpara("@mode",
mode);
para[1] = obget.Getpara("@name",
name);
para[2] = obget.Getpara("@eid",
email);
para[3] = obget.Getpara("@company",
company);
para[4] = obget.Getpara("@salary",
salary);
para[5] = obget.Getpara("@mobile",
mobile);
bdt = odal.register("intrio",
para);
return bdt.Rows[0][0].ToString ();
}
dal.cs
public SqlCommand GetCommand()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout = 0;
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
return cmd;
}
public DataTable register(string
spname, SqlParameter[] para)
{
DataTable dt = new
DataTable();
SqlCommand cmd = GetCommand();
cmd.CommandText = spname.ToString();
cmd.Parameters.AddRange(para);
SqlDataAdapter da = new
SqlDataAdapter(cmd);
try
{
da.Fill(dt);
return dt;
}
catch (Exception e)
{
throw e;
}
finally
{
con.Close();
cmd.Dispose();
}
}
public SqlParameter Getpara(string
paraName,string paravalue)
{
SqlParameter para = new
SqlParameter();
para.SqlDbType = SqlDbType.VarChar;
para.SqlValue = paravalue;
para.ParameterName = paraName;
return para;
}
public SqlParameter Getpara(string
paraName, int paravalue)
{
SqlParameter para = new
SqlParameter();
para.SqlDbType = SqlDbType.Int;
para.SqlValue = paravalue;
para.ParameterName = paraName;
return para;
}
public SqlParameter Getpara(string
paraname, float paravalue)
{
SqlParameter para = new
SqlParameter();
para.ParameterName = paraname.ToString();
para.Value = paravalue.ToString();
para.SqlDbType = SqlDbType.Float;
return para;
}
public SqlParameter Getpara(string
paraname, decimal paravalue)
{
SqlParameter para = new
SqlParameter();
para.ParameterName = paraname.ToString();
para.Value = paravalue.ToString();
para.SqlDbType = SqlDbType.Decimal;
return para;
}
public SqlParameter Getpara(string
paraname, DateTime paravalue)
{
SqlParameter para = new
SqlParameter();
para.ParameterName = paraname.ToString();
para.Value = paravalue.ToString();
para.SqlDbType = SqlDbType.DateTime;
return para;
}
}
Comments
Post a Comment