-1

This is page load where I make two columns in data table and also connect database with dropdown-list:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)  
    {    
        DataTable dt = new DataTable();  
        dt.Columns.AddRange(new DataColumn[2] { new   DataColumn("product_name"), new DataColumn("Quantity") });  
        ViewState["sales"] = dt;  
        GridView1.DataBind();  

        txt_productname.DataSource = sic.get_product_name_for_textbox();  
        txt_productname.DataTextField = "product_name";  
        txt_productname.DataValueField = "product_id";  
        txt_productname.DataBind();  

        txt_customername.DataSource =   sic.get_customer_name_for_textbox();  
        txt_customername.DataTextField = "customer_name";  
        txt_customername.DataValueField = "customer_id";  
        txt_customername.DataBind();  
    }  

Code side this is a temporary data grid:

protected void btn_insert_Click(object sender, EventArgs e)
    if (string.IsNullOrWhiteSpace(txt_productname.Text) ||string.IsNullOrWhiteSpace(txt_quantity.Text))
    {
        Response.Write("<script LANGUAGE='JavaScript'>alert('NotAllowNull')     </script>");  
    }
    else
    {
        DataTable dt = null;
        if (Session["DataTable"] != null)
            dt = (DataTable)Session["DataTable"];
        else
        {
        }

        dt.Rows.Add(txt_productname.SelectedValue.ToString());
        dt.Rows.Add(txt_quantity.Text);

        GridView1.DataSource = dt;
        GridView1.DataBind();
        Session["DataTable"] = dt;
    }
}

Error:

An exception of type 'System.NullReferenceException' occurred in Security Managment System.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • "this error"? **WHAT** error? – Marc B Oct 14 '16 at 18:29
  • An exception of type 'System.NullReferenceException' occurred in Security Managment System.dll but was not handled in user code Additional information: Object reference not set to an instance of an object. – Armaan Arain Oct 14 '16 at 18:29
  • 1
    have you tried stepping through your code and finding where it fails? – MaCron Oct 14 '16 at 18:43
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – C-Pound Guru Oct 15 '16 at 02:34

1 Answers1

0

Apply breakpoints to the flow of methods and check step by step where your values are set to be null. If there is any null in your code then this error message appears. Apply breakpoint on Post back method. Check data table is properly filled or not. Or you're assigning values aren't null.

Zuhair Ali
  • 19
  • 6