TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sna khan
NA
19
10.4k
IS IT POSSIBLE TO INSERT DATA USING AJAX IN ASP.NET
May 5 2014 2:15 PM
IF Yes i am trying that code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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></title>
<script type="text/javascript">
function loadXMLDoc() {
alert("Your browser broke!");
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState == 4) {
var ajaxDisplay = document.getElementById('myDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
var password = document.getElementById('TextBox1').value;
var email = document.getElementById('TextBox2').value;
var queryString = "?password=" + password;
queryString += "&email=" + email ;
ajaxRequest.open("POST", "Default.aspx/SaveUser", true);
ajaxRequest.send();
alert("Your bGETG!");
}
</script>
<style type="text/css">
.style1
{
width: 50%;
}
.style2
{
width: 272px;
}
.style3
{
width: 272px;
height: 41px;
}
.style4
{
height: 41px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td class="style2">
<asp:Label ID="Label2" runat="server" Text="fname"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label3" runat="server" Text="lastname"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
<asp:Label ID="Label4" runat="server" Text="password"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
<asp:Label ID="Label5" runat="server" Text="email"></asp:Label>
</td>
<td class="style4">
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</form>
</body>
</html>
and cod in
Default.aspx is
public static void SaveUser(yu user)
{
string constr = ConfigurationManager.ConnectionStrings["ads"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Example1 VALUES(@Password, @email)"))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Password", user.Password);
cmd.Parameters.AddWithValue("@email", user.email);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Reply
Answers (
3
)
Pop up box in web form
insert data using ajax