Ravi
Ravi

Reputation: 31417

insert data into the table using jquery and asp.net

Default3.aspx

This is my first page. in which i have two textbox and one submit button, onclick of submit button calling Default4.aspx page, where i have written the data insertion code.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">
    </script>

    <script type="text/javascript">
$(function() {
$("#add").click(function() {

var login = $("#loginid").val();
var pass = $("#pass").val();
var dataString = 'login='+ login +'&pass='+pass;

$("#flash").show();
$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" align="absmiddle"> <span class="loading">Loading Comment...</span>');

$.ajax({
type: "POST",
url: "Default4.aspx",
data: dataString,
cache: false,
success: function(html){
$("#display").after(html);
//document.getElementById('content').value='';
//document.getElementById('content').focus();
$("#flash").hide();
}
});
 return false;
});
});
    </script>

</head>
<body>
    <form name="frm_add" id="frm_add">
    <input type="text" name="loginid" id="loginid" />
    <input type="text" name="pass" id="pass" />
    <input type="submit" id="add" value="Submit" />
    <div id="flash"></div>
<div id="display"></div>
    </form>
</body>
</html>

Default4.aspx

<%@ Import Namespace="System.Data.SqlClient" %>

<!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>Untitled Page</title>
</head>
<body>
    <%
    Dim login, pass As String
        login = Request.QueryString("login")
        pass = Request.QueryString("pass")
        Dim cs As String = ConfigurationManager.ConnectionStrings("eExamSolutionConnection").ConnectionString
        Dim cn As New SqlConnection(cs)
        Dim cmd As New SqlCommand
        ' MsgBox(dob)
        cmd.CommandText = "INSERT INTO ADMIN_CREDENTIAL VALUES ('" & login & "','" & pass & "')"
        cmd.Connection = cn
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        cmd.Dispose()
        cn.Dispose()
        Response.Write("Added")
     %>
</body>
</html>

But, blank row inserted into my table, whenever i'm submitting the value. Need help !!

Upvotes: 0

Views: 3940

Answers (2)

The Jonas Persson
The Jonas Persson

Reputation: 1746

Try changing your posted data

data: {login: $("#loginid").val(), pass: $("#pass").val()},

Then, on your Default4.aspx, grab the data from the form data, not the querystring. Request.Form("login") and Request.Form("pass").

Upvotes: 1

Afshin Gh
Afshin Gh

Reputation: 8198

Use Fiddler and check if everything is alright.

Change the TYPE of JQuery Ajax to GET and see if it works or not.

$.ajax({
type: "GET",
url: "Default4.aspx",
data: dataString,
cache: false,
success: function(html){
$("#display").after(html);
//document.getElementById('content').value='';
//document.getElementById('content').focus();
$("#flash").hide();

Upvotes: 3

Related Questions