Get value from textbox to database

Posted by Kaliswarang under jQuery on 9/15/2012 | Points: 10 | Views : 5633 | Status : [Member] | Replies : 5
Hi Experts,

I am new to Jquery and Ajax
I need to get the code for below synario

I have one text box in UI .

Using query and ajax the value should be saved in database.Please help me on this




Responses

Posted by: Jimmyhelu on: 9/16/2012 [Member] Starter | Points: 25

Up
0
Down
I have you put in breakpoints? First, check to make sure that the connection string is correct from the file. Second, make sure the connection string is getting read properly(make sure "conn" is not blank or null).

Also, you have the parameter and values in the wrong order. It should be the parameter and then the value...


Kaliswarang, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
hi,
see this link.
http://forums.asp.net/t/1629529.aspx/1

Thanks & Regards
Hari

Kaliswarang, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vikash on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
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. | Alert Moderator

Posted by: Hariinakoti on: 9/17/2012 [Member] Starter | Points: 25

Up
0
Down
Good Work Vikash


Thanks & Regards
Hari

Kaliswarang, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/17/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

ofcourse use this


Mark as Answer if its helpful to you

Kaliswarang, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response