Skip to content Skip to sidebar Skip to footer

Update Sql Database From Client Side In Asp.net

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

Solution 1:

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.

Solution 2:

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.

enter image description here

Screenshot take from here

Solution 3:

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.com/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 editPersoncurrentPerson= 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();

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

Post a Comment for "Update Sql Database From Client Side In Asp.net"