<script type="text/javascript">
$(document).ready(function () {
// Send an AJAX request
$.getJSON("api/products/",
function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, val) {
// Format the text to display.
var str = val.Name + ': $' + val.Price;
// Add a list item for the product.
$('<li/>', { html: str })
.appendTo($('#products'));
});
});
});
</script>
function save(){
var name = document.getElementById('<%= txt_name.ClientID %>').value;
var country = document.getElementById('<%= txt_country.ClientID %>').value;
var city =document.getElementById('<%= txt_city.ClientID %>').value;
var mob = document.getElementById('<%= txt_mob.ClientID %>').value;
//alert(""+name+"---"+country+"----"+city+"----"+mob);
$.ajax({
type: "POST",
url: "default.aspx/save",
data: "{name: '"+ name +"' , country :'"+ country +"' , city:'"+city+"' mob : '"+mob+"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
cache: false,
success: function(result){
alert('saved');
}
,error(er)
{
//Faild to go to the server
alert(er.responseText)
}
});
}
my prob is how i save data to database as m not getting that......
Amit Bhardwa...
Member
61 Points
65 Posts
how to insert data to database using javascript with asp.net
Jul 11, 2012 06:41 AM|LINK
I am new to javascript and i want to insert data to my database using javascript.
plz help.
amit jamwal
karang
Contributor
2441 Points
919 Posts
Re: how to insert data to database using javascript with asp.net
Jul 11, 2012 06:47 AM|LINK
Hi
There is a method in jQuery ajax()
http://api.jquery.com/jQuery.ajax/
With this method you can call the server side code in the code behind. Add [WebMethod] attribute to the method which you want to call using jQuery.
Hope this helps.
Karan Gupta
http://gyansangrah.com
Please click "Mark as Answer" if this helped you.
vijayst
All-Star
16558 Points
3216 Posts
Microsoft
Re: how to insert data to database using javascript with asp.net
Jul 11, 2012 06:56 AM|LINK
You can use asp.net web api - http://www.asp.net/web-api
The tutorial - http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api explains how to use javascript to fetch data from web api. Code from tutorial shown below:
<script type="text/javascript"> $(document).ready(function () { // Send an AJAX request $.getJSON("api/products/", function (data) { // On success, 'data' contains a list of products. $.each(data, function (key, val) { // Format the text to display. var str = val.Name + ': $' + val.Price; // Add a list item for the product. $('<li/>', { html: str }) .appendTo($('#products')); }); }); }); </script>http://liteblog.codeplex.com
Amit Bhardwa...
Member
61 Points
65 Posts
Re: how to insert data to database using javascript with asp.net
Jul 11, 2012 07:05 AM|LINK
thnx
in my question i want to make connection with the database i.e. sql server to insert or retreive the data.
Also i am using asp.net 2.0
thnx
ami
fayaz_3e
Star
9332 Points
1744 Posts
Re: how to insert data to database using javascript with asp.net
Jul 11, 2012 07:22 AM|LINK
As mentioned above, use jQuery AJAX to accomplish this.
Create a public static method in your code behind file and have your DB insert/Retrieve logic in it.
Mark the public static method with [WebMethod] attribute
Use jQuery Ajax to invoke the the newly created web method.
This is called as accessing page methods from javascript. Search for it, will get tons of sites...
Amit Bhardwa...
Member
61 Points
65 Posts
Re: how to insert data to database using javascript with asp.net
Jul 11, 2012 07:28 AM|LINK
thnx
i did the same but got this error for the connection string obj
": CS0120: An object reference is required for the nonstatic field, method, or property '_Default.save(string, string, string, string)':
i tried but still waiting for success.
ami
fayaz_3e
Star
9332 Points
1744 Posts
Re: how to insert data to database using javascript with asp.net
Jul 11, 2012 07:40 AM|LINK
You should not invoke this method in C#. Invoke this in javascript/jQuery...
Somthing like below, change the script as per your method and param names...
$.ajax({ type: "POST", url: "Default.aspx/Save", data: "{'ID' : '" + $('input:textbox[id*=txtID]').val() + "', 'name' : '" + $('input:textbox[id*=txtName]').val() + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { alert('saved'); } });Amit Bhardwa...
Member
61 Points
65 Posts
Re: how to insert data to database using javascript with asp.net
Jul 11, 2012 08:54 AM|LINK
thnx for reply
m doing like this
function save(){ var name = document.getElementById('<%= txt_name.ClientID %>').value; var country = document.getElementById('<%= txt_country.ClientID %>').value; var city =document.getElementById('<%= txt_city.ClientID %>').value; var mob = document.getElementById('<%= txt_mob.ClientID %>').value; //alert(""+name+"---"+country+"----"+city+"----"+mob); $.ajax({ type: "POST", url: "default.aspx/save", data: "{name: '"+ name +"' , country :'"+ country +"' , city:'"+city+"' mob : '"+mob+"'}", contentType: "application/json; charset=utf-8", dataType: "json", async: false, cache: false, success: function(result){ alert('saved'); } ,error(er) { //Faild to go to the server alert(er.responseText) } }); }my prob is how i save data to database as m not getting that......
amit jamwal
fayaz_3e
Star
9332 Points
1744 Posts
Re: how to insert data to database using javascript with asp.net
Jul 11, 2012 09:18 AM|LINK
Ok. Now in your web method the method which you have in code behind file, there write sql query and do a execute nonquery...
http://www.schisani.com/ShowPost.aspx?postID=19
Amit Bhardwa...
Member
61 Points
65 Posts
Re: how to insert data to database using javascript with asp.net
Jul 12, 2012 05:17 AM|LINK
i tried but not found any result...
can u post some example for me..
coz in my webmethod if i use static keyword than it gives me error
Error 1 An object reference is required for the nonstatic field, method, or property '_Default.con'
and without static function is not called.
m confused. :(
amit jamwal