Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 48319 |  Welcome, Guest!   Register  Login
 Home > Forums > jQuery > Get value from textbox to database ...
Kaliswarang

Get value from textbox to database

Replies: 5 | Posted by: Kaliswarang on 9/15/2012 | Category: jQuery Forums | Views: 1042 | Status: [Member] | Points: 10  


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


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Jimmyhelu
Jimmyhelu  
Posted on: 9/16/2012 6:00:13 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Hariinakoti
Hariinakoti  
Posted on: 9/17/2012 5:04:31 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Vikash
Vikash  
Posted on: 9/17/2012 6:59:11 AM
Level: Starter | Status: [Member] | Points: 25

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 

Hariinakoti
Hariinakoti  
Posted on: 9/17/2012 7:00:05 AM
Level: Starter | Status: [Member] | Points: 25

Good Work Vikash


Thanks & Regards
Hari

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

Jayakumars
Jayakumars  
Posted on: 9/17/2012 11:38:31 AM
Level: Bronze | Status: [Member] | Points: 25

hi

ofcourse use this


Mark as Answer if its helpful to you

Regards
Email Id: kumaraspcode2009@gmail.com

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

Reply - Please login to reply


Click here to login & reply

Found interesting? Add this to:


 Latest Posts

Write New Post | More ...

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 10:18:55 AM