c# - Update SQL Database from client side in ASP.NET - Stack Overflow

I am kinda new in web development, looking for secured way to update SQL Database from the client side,

I am kinda new in web development, looking for secured way to update SQL Database from the client side, or in other description updating the database without refreshing the webpage like (like facebook button). I searched a lot and found that it can be done by using a web service and call it by javascript or using javascript direct or ajax, but which is the best and secured way and there is any other way to do it ? thanks..

I am kinda new in web development, looking for secured way to update SQL Database from the client side, or in other description updating the database without refreshing the webpage like (like facebook button). I searched a lot and found that it can be done by using a web service and call it by javascript or using javascript direct or ajax, but which is the best and secured way and there is any other way to do it ? thanks..

Share Improve this question asked May 23, 2012 at 23:09 Mohamed UsamaMohamed Usama 992 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

you can use ajax for updating database from client side.. Like if you click a button in web page, get the click event of that page through JavaScript or jQuery then through ajax you can perform database update. See the code below:

For catching event(like my button id is button1):

$('#<%=button1.ClientID%>').click(function{ 
      $.ajax({
               type: "POST",
               url: "default.aspx/UpdateDatabase",
               data: "{'textboxvalue':'" + $('<%=textbox1.ClientID%>').val() + "'}'
               contentType: "application/json;charset=utf-8",
               datatype: "json",
               success: UpdateDone
             });
});

In above code you have passed one value from a textbox1 to function UpdateDatabse in Default.aspx page(Please defined this function as [WebMethod]). then do your update in this function and return some string or bool value so that you can judge that update is done then value in success is name of function that will run if your function runs successfully so define that function in JavaScript or jQuery like

    function UpdateDone(response)
    {
        if(response.d == 'done')
         { alert('Update is done');  }
        else
         { alert('Sorry Update not done'); }
    }

Above code will not do any postback you see that your value is updated in database. Please note that the function you make in C# page please mark it as WebMethod and it will be a static method, then only your JavaScript can find that function.

Hope this will resolve your problem.

The term ajax you use is correct but already a bit old. The new kids on the block are called SPA's where SPA stands for Single Page Application

It does what you want to achieve to the extreme: no more page refreshes. So it seems a good way to start

Here is The ASP.NET Single Page Application homepage

My advice is to research and invest time in one of the (many) javascript frameworks that will help you achieve this goal much faster. Hand coding javascript and make it work cross browser is too much work. The ASP.NET team choose upshot.js to solve your problem and it seems a fine choice.

Screenshot take from here

I found doing AJAX with JSON with ASP.NET MVC 3 to be easiest method of doing AJAX requests. Then you can have a specific action method that handles the request and makes the updates the database via Entity Framework(EF).

Essentially only passing the data that needs to be updated in the JSON. From there the MVC Action receives the JSON, and uses EF to lookup the DB record, apply/save changes. It can even respond with a success message which your AJAX can use to update some field that verifies the data was saved for the user(you could even do something where you have a "Saving..." message appear between the first ajax request and the response.)

This will allow you to send the request without refreshing your page. All your DB access code will be server side in the Action method.

This example shows how you might do a json request. You would modify this by adding additional code to the Create method to interact with entity framework(or your database tool of choice) to update a record based on the Person person parameter that was passed in(notice MVC did a really nice thing of converting the json data into a nice Person class!)

http://juristr./blog/2011/08/posting-json-data-to-aspnet-mvc-3-web/

If the data the user will enter in the webform is sensitive, you would need to encrypt it before sending the json request. I would personally just setup the website to use SSL. Anything you cook up on your own probably won't be as secure as SSL.

The code you add to the Create method might look like this:

//Find the person that they are attempting to edit
Person currentPerson = db.Persons.Find(person.PersonKey);
//update the database fields based on the submitted data(I would probably actually use AutoMapper for this
currentPerson.Name = person.Name;
currentPerson.DateOfBith = person.DateOfBirth;
//etc.
db.SaveChanges();

//pose a JSON response object indicating success, you would want to bine this with a try catch in the above to reply regarding failures as well

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745346569a4623576.html

相关推荐

  • c# - Update SQL Database from client side in ASP.NET - Stack Overflow

    I am kinda new in web development, looking for secured way to update SQL Database from the client side,

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信