这是页面加载,我在数据表中创建两列,并使用下拉列表连接数据库:
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();
}
代码端这是一个临时数据网格:
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;
}
}
错误:
发生了'System.NullReferenceException'类型的异常 Security Managment System.dll但未在用户代码中处理
附加信息:对象引用未设置为的实例 对象
答案 0 :(得分:0)
将断点应用于方法流,并逐步检查您的值设置为null。如果代码中有任何null,则会显示此错误消息。在Post back方法上应用断点。检查数据表是否正确填充。或者您分配的值不是空的。