0

I'm calling a webService.asmx using Jqueryand inside that service i'm retrieving a usercontrolcontrol's values to save them in the database but the user control has thrown a NullReferenceException

here is Ajaxcall

function SaveEdit()
    {
        $.ajax({
            type: "POST",
            url: "Services/RatiosSettingsService.asmx/UpdateRatios",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) { }
        });
    }

and this is WebServicecode

[WebMethod]
    public void UpdateRatios()
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Crs2"].ConnectionString))
        {
            ADO ado = new ADO();
            List<SqlParameter> parameters = new List<SqlParameter>();
            ucOtherRatios2 obj = new ucOtherRatios2();

            Dictionary<string, int> hs = obj.GetHsChks();

            foreach (KeyValuePair<string, int> item in hs)
            {
                SqlParameter para = new SqlParameter(item.Key, item.Value);
                parameters.Add(para);
            }

            con.Open();
            ado.CUDSp("UpdateRatios", "spUpdateClientRatios",parameters,con);
            con.Close();
        }
    }

and here is where the exception happened inside usercontrol method that retrieve the controls values

public Dictionary<string, int> GetHsChks()
    {
        Dictionary<string, int> chks = new Dictionary<string, int>();

        chks.Add("@siAnalysisOtherRatiosHistorical1", Convert.ToInt32(chkOthWcHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical2", Convert.ToInt32(chkOthWiHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical3", Convert.ToInt32(chkOthTlgHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical4", Convert.ToInt32(chkOthEiHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical5", Convert.ToInt32(chkOthEcHs.Checked));

        chks.Add("@siAnalysisOtherRatiosHistorical6", Convert.ToInt32(chkOthEicHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical7", Convert.ToInt32(chkOthEsHs.Checked));
        chks.Add("@siAnalysisOtherRatiosHistorical8", Convert.ToInt32(chkOthEtHs.Checked));

        return chks;
    }

it says that checkbox is null

2 Answers2

0

You can't access page's controls from a webmethod. Since web methods don't carry the page state. It isn't a full postback. Instead just the session cookie travels with the request. You have to do a full page postback to get or set the control values. Or you have to send the values of the controls through AJAX post method.

You can get more details on below link:

How to access page controls inside a static web method?

Aman Sahni
  • 212
  • 1
  • 8
  • I have changed my code .. I'm calling the user-controle method from none webMethod i'v do it using a postback button-click and still giving me that null Exception – Ahmad A. Al-Dwairi Aug 07 '17 at 10:29
  • You cannot access the controls in case of postback call because when you are doing AJAX postback, it is not full page postback. So you need to send data in form of JSON from AJAX postback method to your .net method. – Aman Sahni Aug 07 '17 at 16:08
0

I see one thing in jQuery what would be good to change. Maby is better to use $.getJSON() function inside jQuery if you only getting JSON data. There you can use .done() to get data and .fail() for debbug.

Next thing is setup POST or GET variables to pass data to your JSON file to get proper data.

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78