Please understand the following scenario.... i hope it will help......
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataInsertion.aspx.cs" Inherits="DataInsertion" %>
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>Data insertion title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js">script>
<script type="text/javascript">
$(document).ready(function () {
GetID();
$('#btnSave').click(function () {
var s = validateFields();
if (s) {
$.ajax({
type: "POST",
url: "DataInsertion.aspx/SaveData",
data: '{firstName:"' + $('#tbFName').val() + '",lastName:"' + $('#tbLName').val() + '"}',
contentType: "application/json; charset=utf-8",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
function OnSuccess(response) {
if (response.d) {
alert("Successfully inserted !");
GetID();
}
}
}
});
});
function validateFields() {
var tbFName = $('#tbFName').val();
var tbLName = $('#tbLName').val();
if (tbFName == "") {
alert('Enter first name');
return false;
}
else if (tbLName == "") {
alert('Enter last name');
return false;
}
else {
return true;
}
}
function GetID() {
$('#tbFName').val('');
$('#tbLName').val('');
$.ajax({
type: "POST",
url: "DataInsertion.aspx/GetID",
// data: '{id: "' + $("#tbId").val() + '" ,firstName:"' + $('#tbFName').val() + '" ,lastName:"' + $('#tbLName').val() + '}',
contentType: "application/json; charset=utf-8",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
function OnSuccess(response) {
$('#tbId').val(response.d);
$('#tbId').attr('readOnly', true);
}
}
script>
head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
ID:
td>
<td>
<asp:TextBox ID="tbId" runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
FirstName:
td>
<td>
<asp:TextBox ID="tbFName" runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
LastName:
td>
<td>
<asp:TextBox ID="tbLName" runat="server">asp:TextBox>
td>
tr>
table>
<input type="button" value="Save Data" id="btnSave" />
div>
form>
body>
html>
The Code behind is given below
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;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class DataInsertion : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static bool SaveData(string firstName, string lastName)
{
String connectionString = ConfigurationManager.ConnectionStrings["TESTDB"].ConnectionString;
bool result = false;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = String.Format("insert into Hemanth_DB.dbo.PersonName(FirstName,LastName) values ('{0}','{1}')", firstName, lastName);
cmd.CommandType = CommandType.Text;
connection.Open();
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
result = true;
}
else
{
result = false;
}
}
return result;
}
[WebMethod]
public static int GetID()
{
int rowId;
String connectionString = ConfigurationManager.ConnectionStrings["TESTDB"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "select MAX(id)+1 from Hemanth_DB.dbo.PersonName";
cmd.CommandType = CommandType.Text;
connection.Open();
rowId = (int)cmd.ExecuteScalar();
}
return rowId;
}
[WebMethod]
public static bool Upsert()
{
return true;
}
}
Please mark as answer, if found good...........
Regards,
Vikash Pathak
Kaliswarang, if this helps please login to Mark As Answer. |
Reply | Alert Moderator