Save data using asp net web services and jquery ajax

  Рет қаралды 76,319

kudvenkat

kudvenkat

Күн бұрын

Link for all dot net and sql server video tutorial playlists
www.youtube.co...
Link for slides, code samples and text version of the video
csharp-video-tu...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our KZbin channel. Hope you can help.
/ @aarvikitchen5572
In this video we will discuss how to save data using asp.net web services and jquery ajax.
When Add Employee button is clicked we want to post form data to the asp.net web service and the web service should save the data to the database table.
Step 1 : Create Insert Stored Procedure
Step 2 : Modify EmployeeService.asmx.cs as shown below
namespace Demo
{
[WebService(Namespace = "tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class EmployeeService : System.Web.Services.WebService
{
[WebMethod]
public void AddEmployee(Employee emp)
{
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spInsertEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@Name",
Value = emp.Name
});
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@Gender",
Value = emp.Gender
});
cmd.Parameters.Add(new SqlParameter()
{
ParameterName = "@Salary",
Value = emp.Salary
});
con.Open();
cmd.ExecuteNonQuery();
}
}
[WebMethod]
public void GetAllEmployees()
{
List<Employee> listEmployees = new List<Employee>();
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select * from tblEmployee", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.ID = Convert.ToInt32(rdr["Id"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.Salary = Convert.ToInt32(rdr["Salary"]);
listEmployees.Add(employee);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(listEmployees));
}
}
}
Step 3 : Modify HtmlPage1.html as shown below.
$(document).ready(function () {
$('#btnAddEmployee').click(function () {
var employee = {};
employee.Name = $('#txtName').val();
employee.Gender = $('#txtGender').val();
employee.Salary = $('#txtSalary').val();
$.ajax({
url: 'EmployeeService.asmx/AddEmployee',
method: 'post',
data: '{emp: ' + JSON.stringify(employee) + '}',
contentType: "application/json; charset=utf-8",
success: function () {
getAllEmployees();
},
error: function (err) {
alert(err);
}
});
});
});

Пікірлер: 25
@mohammedbadran7840
@mohammedbadran7840 8 жыл бұрын
I learned so much from your videos thnx a lot
@Csharp-video-tutorialsBlogspot
@Csharp-video-tutorialsBlogspot 8 жыл бұрын
+Mohammed Knight Thanks a bunch for taking your valuable time to give feedback. Means a lot. Glad you found the videos useful. Dot Net & SQL Server videos for aspiring software developers kzbin.infoplaylists?view=1&sort=dd If you need videos for offline viewing, you can order them using the link below www.pragimtech.com/Order.aspx Code Samples, Text Version of the videos & PPTS on my blog csharp-video-tutorials.blogspot.com Tips to effectively use our channel kzbin.info/www/bejne/r2ibYYCtnb5qZtU Want to receive email alerts, when new videos are uploaded, please subscribe to our channel using the link below kzbin.info Please click the THUMBS UP button below the video, if you think you liked them Thank you for sharing these links with your friends Best Venkat
@mohammedbadran7840
@mohammedbadran7840 8 жыл бұрын
+kudvenkat thnx a lot from Egypt
@tolaskaplan
@tolaskaplan 6 жыл бұрын
I have a question - how are you passing a json string in the ajax and recieving an employee object in the web service?
@ahmedshahabaz8370
@ahmedshahabaz8370 9 жыл бұрын
This example didn't work for me. I used visual studio 2013 and Microsoft SQL server 2012. I uncommented [System.Web.Script.Services.ScriptService] this. Firstly I had to pass Id as a parameter to the stored procedure. So my stored procedure had 4 parameters. Secondly passing the parameter from html page to asmx method didn't work. So I had to change the parameter type of the AddEmployee(Employee emp ) method to AddEmployee(string name,string gender , int salary). I passed the three parameter from the html page.I counted the number of rows in tblEmployee table and incremented it by one and assigned it to the emp.Id this was passed as the @Id parameter to the stored procedure. Please suggest me if I could have done this in another way. Where I could have kept the Employee type parameter of the AddEmployee method.
@tung.nguyen.29
@tung.nguyen.29 9 жыл бұрын
Nice tutorial, good job guy!
@vimalupadhyay1883
@vimalupadhyay1883 6 жыл бұрын
Hello Venkat Sir, i am making one application by seeing your videos, I implementing Insert/Add functionality same as in this video, but sometimes my application insert data, and sometimes its not, but in both cases it gives error [object,object] ,even it successfully insert data it go to error function, not success function and give error [object object] can you provide some guidance on what i need to do? i already viewed this video several times and check everything as per it, but still getting this issue
@pastuh
@pastuh 6 жыл бұрын
If you get this error, that means you get Object. Need to use .each function, to get all elements from object
@mohammedtawane
@mohammedtawane 6 жыл бұрын
I really enjoyed your clear video, Although i was looking different. Can you pls guide me how to insert file in to database using jquery ajax through webservice? I appreciate you
@vshah2703
@vshah2703 Жыл бұрын
sir in web service where did you get the value name that is passed to parameter @Name, I am unable to pass
@gauravdubey4002
@gauravdubey4002 9 жыл бұрын
Hi kudvenkat,can u include the upload file in the same form and along side saving that filename to database and file to the folder.
@rahulrathaur8039
@rahulrathaur8039 5 жыл бұрын
Hello sir. How Can I Send Image from Ajax to Webservice
@Rocky19700101
@Rocky19700101 5 жыл бұрын
Thank you for your excellent video, I got successful on search and get all data from web service, but always fails on saving data to database, Just don't know what cause the problem?
@eksrikanth06
@eksrikanth06 9 жыл бұрын
can you pls share an example of saving Image(from picturebox) to Sqlserver table with Image(datatype)
@pxanimate
@pxanimate 9 жыл бұрын
Thank you so much . Great Tutorial ... I have a question How can i create the Employee class object ...?
@artemboss1999
@artemboss1999 9 жыл бұрын
a good video tutorial thanks
@SwiftLearn
@SwiftLearn 4 жыл бұрын
Get employees tutorial please.
@jclinares6874
@jclinares6874 8 жыл бұрын
aunque no entendi lo que hablaste, me ayudo a comprender mucho (y)
@donfeto7636
@donfeto7636 5 жыл бұрын
i would rather write the data like that data:Json.stringify({emp:employee}) we have to stringify anything that isn't string cuz the http accept only string to be passed in url any complex type have to stringifyed first
@FabianoAugustoperfil
@FabianoAugustoperfil 7 жыл бұрын
very good class
@codewizard5327
@codewizard5327 6 жыл бұрын
It kinda works, but in real world application, you might want to add a small piece of html at the end of tbody instead of success:GetAllEmployees()
@bojanasamardzioska3040
@bojanasamardzioska3040 7 ай бұрын
💯
@pablosalas8586
@pablosalas8586 8 жыл бұрын
and with update?? SP: public void UpdateEmployee(Employee emp) { string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spUpdateEmployee", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Id", Value = emp.ID }); cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Name", Value = emp.Name }); cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Gender", Value = emp.Gender }); cmd.Parameters.Add(new SqlParameter() { ParameterName = "@Salary", Value = emp.Salary }); con.Open(); cmd.ExecuteNonQuery(); } } ********************************************* $('#btnUpdateEmployee').click(function () { var employee = {}; employee.ID = $('#txtId').val(); employee.Name = $('#txtName').val(); employee.Gender = $('#txtGender').val(); employee.Salary = $('#txtSalary').val(); $.ajax({ url: 'EmployeeService.asmx/UpdateEmployee', method: 'POST', data: '{emp: ' + JSON.stringify(employee) +'}', contentType: "application/json; charset=utf-8", success: function () { getAllEmployees(); }, error: function (err) {alert(err); } }); }); doesn't work!. :(
@Shahzaibkhan-xr7lr
@Shahzaibkhan-xr7lr 3 жыл бұрын
For those who are getting error [object object] uncomment this library [System.Web.Script.Services.ScriptService] in your asmx file
Check if username exists in database with ajax
19:17
kudvenkat
Рет қаралды 86 М.
Calling asp net web services using jquery ajax
15:44
kudvenkat
Рет қаралды 97 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
How to suggest available username
17:27
kudvenkat
Рет қаралды 26 М.
jQuery Ajax Tutorial #1 - Using AJAX & API's (jQuery Tutorial #7)
7:32
LearnCode.academy
Рет қаралды 711 М.
Full CRUD Operations Using Modal Popup in ASP.NET Core MVC | CRUD Application with ASP.NET Core
1:26:16
𝐂𝐨𝐝𝐞𝐖𝐢𝐭𝐡𝐆𝐨𝐩𝐢
Рет қаралды 24 М.
Brutally honest advice for new .NET Web Developers
7:19
Ed Andersen
Рет қаралды 284 М.
How to insert data to database using Model in ASP.NET CORE
19:25
Csharp Space
Рет қаралды 20 М.
08: How to pass JSON data to server side using jQuery AJAX ?
10:12
mahesh panhale
Рет қаралды 38 М.
jquery ajax call to web method in asp.net c# to save details
15:38
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН