views:

298

answers:

2

i have few textbox on the form and when the user submit i want to capture the data and insert into db

here is what my code looks like

beforeSubmit: function(data) 
{ // called just before the form is submitted

    var item = $("[id$='item']");
    var category = $("[id$='category']");
    var record = $("[id$='record']");

    var json = "{'ItemName':'" + escape(item.val()) +
        "','CategoryID':'" + category.val() + "','RecordID':'" + record.val() + "'}";

    //this page is where data is to be retrieved and processed
    var ajaxPage = "DataProcessor.aspx?Save=1";

    var options = 
    {
        type: "POST",
        url: ajaxPage, 
        data: json,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,
        success: function(response) 
        {
            alert("success: " + response);
        },
        error: function(msg) 
        { 
            alert("failed: " + msg); 
        }
    };

    //execute the ajax call and get a response
    var returnText = $.ajax(options).responseText;
    if (returnText == 1) {
        record.html(returnText);
        $("#divMsg").html("<font color=blue>Record saved successfully.</font>");
    }
    else
    {
        record.html(returnText);
        $("#divMsg").html("<font color=red>Record not saved successfully.</font>");
    }  

    // $("#data").html("<font color=blue>Data sent to the server :</font> <br />" + $.param(data));

},

here is what is the Data sent to the server: if i uncomment this line:

 // $("#data").html("<font color=blue>Data sent to the server :</font> <br />" + $.param(data));

_VIEWSTATE=%2FwEPDwULLTE4ODM1ODM4NDFkZOFEQfA7cHuTisEwOQmIaj1nYR23&_EVENTVALIDATION=%2FwEWDwLuksaHBgLniKOABAKV8o75BgLlosbxAgKUjpHvCALf9YLVCgLCtfnhAQKyqcC9BQL357nNAQLW9%2FeuDQKvpuq2CALyveCRDwKgoPWXDAKhwImNCwKiwImN &day_fi=12&month_fi=12&year_fi=1234&lastFour_fi=777&countryPrefix_fi=1&areaCode_fi=555-555&phoneNumber_fi=5555&email_fi=nisardotnet%40gmail.com&username=nisarkhan&password=123456&retypePassword=123456

A: 

You should be able to pull it all out of the Request.Form.

Jeremy B.
Jeremy, can you show me how? - thanks.
Abu Hamzah
+1  A: 

Nisardotnet - are you working in C#? You've got way too much going on here. You can cut down your code by half using web methods - also, get rid of viewstate - you don't need it (remove the form from the page)

If your working in C# let me know and I can help. Rob

*APPEND*

Ok - I built a simple "grab input values and update DB" thing - it's on my site here.

Give me a shout if you've got any questions. Rob

*APPEND*

Ok, so in your class file you could have

internal static void updateDB(String values)
{
    // do something with 'values'
}

Then in your aspx page you can call it like so...

[WebMethod]
public static void updateDB(String values)
{
    yourClass.updateDB(values);
}

That should work.

LiverpoolsNumber9
Hi Rob,Yes I am working on C# VS 2008 asp.net - Thank you.
Abu Hamzah
Rob, i have tested your solution and it wokks but when i try to put the webmethod in the class then it fails. have you tried put the updateDb in class and see?here is the error message:HTTP Error 403 - Forbidden. var url = "App_Code/WebMethods.cs/updateDB";or var url = "WebMethods.cs/updateDB";
Abu Hamzah
Abu - you cannot call the web method directly from the class file. It is, literally, forbidden! You can only make a call from jQuery.ajax to an aspx page (or asmx, but it's very buggy). So, you can put the method in a class file, but then you'll need another web method on your aspx page (or it's code behind) that you call from jQuery.ajax. Does that make sense? - i've put some more explanation above
LiverpoolsNumber9
interesting,... thanks very much
Abu Hamzah
You're welcome. Hope it works for you. Once I figured this out, things got a lot easier and all my apps are now using it!Take care
LiverpoolsNumber9