0

when textbox getting the data through datatable object in static method at that time it shows the null value (i.e. showing error : object reference not set to an instance of the object). But when I am doing the debug mode at that time it shows the data in the datatable, so from that I am confusing that how in textbox it getting the null value.

Here is my code

[WebMethod]
    public static void GetCnorGSTNo(string Param1)
    {
        Page page = (Page)HttpContext.Current.Handler;
        TextBox cnorGST = (TextBox)page.FindControl("txtbx_cnortin");

        DataAccess clsObjDataAccess = new DataAccess();
        string qrySelCnorGtinNo = "select TinNo1 from CnorMaster where CnorName = '" + Param1 + "'";
        DataTable dtqrySelCnorGtinNo = new DataTable();
        dtqrySelCnorGtinNo = clsObjDataAccess.GetDataTable(qrySelCnorGtinNo);
        if (dtqrySelCnorGtinNo.Rows.Count > 0)
        {
            cnorGST.Text = dtqrySelCnorGtinNo.Rows[0]["TinNo1"].ToString();
        }
    }

When I am doing in debug mode

"dtqrySelCnorGtinNo.Rows[0]["TinNo1"].ToString();"

this line shows the data

but when this data is giving to the textbox i.e. cnorGST.Text at that time it showing an error i.e. object reference not set to an instance of the object.

Please help me to solve this issues.

Thank You.

karan
  • 482
  • 1
  • 8
  • 30
  • 1
    A webmethod runs outside of the context of a page's lifecycle, so the controls don't exist on the server. You cannot access them. https://stackoverflow.com/questions/31514188/how-to-access-page-controls-inside-a-static-web-method – Tim Schmelter Jul 04 '17 at 10:35
  • is there any thing that I can access the control. Because whatever the value is coming in the datatable that value I want to show the textbox. Then how can I do this? – karan Jul 04 '17 at 10:37
  • 1
    You can access the database in the webmethod, just return the value you want to show in the textbox. Then assign this text via javascript from clientside. – Tim Schmelter Jul 04 '17 at 10:38

1 Answers1

1

A webmethod runs outside of the context of a page's lifecycle, so the controls don't exist on the server. You cannot access them.

But you are calling this webemethod from a clientside method. There you can assign the text. So get the value from the database in the webmethod and return it:

[WebMethod]
public static string GetCnorGSTNo(string Param1)
{
    DataAccess clsObjDataAccess = new DataAccess();
    string qrySelCnorGtinNo = "select TinNo1 from CnorMaster where CnorName = '" + Param1 + "'";
    return clsObjDataAccess.GetDataTable(qrySelCnorGtinNo).AsEnumerable()
       .ElementAtOrDefault(0)?.Field<string>("TinNo1");
}

The javascript method that calls this webmethod assigns the returned text to txtbx_cnortin.Value.

Sir, can you give me this parameterized queries demo. I'm using sqlserver2005

Sure

[WebMethod]
public static string GetCnorGSTNo(string cnorName)
{
    using (var con = new SqlConnection("connection-string here"))
    using (var cmd = new SqlCommand("select TinNo1 from CnorMaster where CnorName = @CnorName", con))
    {
        cmd.Parameters.Add("@CnorName", SqlDbType.NVarChar).Value = cnorName;
        con.Open();
        object val = cmd.ExecuteScalar();
        return val == DBNull.Value ? "" : (string)val;
    }
}

Since i don't know the db-type of the TinNo1-column you might have to change the (string)val.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @karan: note that you should use parameterized queries instead of string concatenation. Otherwise you are (very) prone to sql-injection attacks. – Tim Schmelter Jul 04 '17 at 10:48
  • Sir, can you give me this parameterized queries demo. – karan Jul 04 '17 at 10:51
  • sqlserver2005, 2008r2 – karan Jul 04 '17 at 10:57
  • Sir, you mean to say that I want to do parameterized queries like store procedure.....right? – karan Jul 04 '17 at 10:57
  • how can I select the multiple field in parameterized query. for ex if I want to do the query like "select TinNo1, TinNo2 from CnorMaster where CnorName = @CnorName" then how can I return the value in json? @Tim Schmelter – karan Jul 13 '17 at 07:39