-2

我试图通过登录系统来插入数据。我的查询没有任何错误,但运行时抛出的异常为“对象引用未设置为对象的实例”。检查我的代码,请纠正我。

protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (TextBox6.Text == " ")
            {
                string alertmessage = "";
                alertmessage = "Username should not be blank";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox6.Focus();
            }
            else if (TextBox7.Text == " ")
            {
                string alertmessage = "";
                alertmessage = "Username should not be blank";
                this.CreateMessageAlert(this, alertmessage, "alertKey");
                TextBox7.Focus();
            }
            else
            {
                string sq = "SELECT COUNT(*) FROM tbl_KKSUser WHERE Uname=@un and Password=@pas";
                SqlCommand sd = new SqlCommand(sq, con);
                SqlParameter unameparam;
                unameparam = new SqlParameter("@un", SqlDbType.VarChar, 25);
                unameparam.Value = TextBox6.Text;
                sd.Parameters.Add(unameparam);

                string original = TextBox7.Text.Trim();
                string withhash = original;
                b1 = Encoding.BigEndianUnicode.GetBytes(withhash);
                encrypted = Convert.ToBase64String(b1);
                SqlParameter passparam;
                passparam = new SqlParameter("@pas", SqlDbType.VarChar, 8000);
                passparam.Value = Convert.ToString(encrypted);
                sd.Parameters.Add(passparam);

                con.Open();
                {
                    int iresults;
                    iresults = Convert.ToInt32(sd.ExecuteScalar().ToString());
                    if (iresults > 0)
                    {
                        string q = "insert into tbl_KKSMaterialRaise(MaterialCode,Source,Category,Population,StockInStores,Specification,PrearedBy,CheckedBy,ApprovedBy,CreatedDate) values(@mc,@sc,@cat,@pop,@sis,@spec,@pb,@cb,@ab,@cd)";
                        SqlCommand dm = new SqlCommand(q, con);
                        dm.Parameters.AddWithValue("@mc", Mcodeddl.SelectedItem.Text);
                        dm.Parameters.AddWithValue("@sc", TextBox1.Text.Trim());
                        dm.Parameters.AddWithValue("@cat", TextBox2.Text.Trim());
                        dm.Parameters.AddWithValue("@pop", TextBox3.Text.Trim());
                        dm.Parameters.AddWithValue("@sis", TextBox4.Text.Trim());
                        dm.Parameters.AddWithValue("@spec", TextBox5.Text.Trim());
                        dm.Parameters.AddWithValue("@pb", PBddl.SelectedItem.Text);
                        dm.Parameters.AddWithValue("@cb", CBddl.SelectedItem.Text);//In this line i have got error
                        dm.Parameters.AddWithValue("@ab", ABddl.SelectedItem.Text);
                        dm.Parameters.AddWithValue("@cd", DateTime.Today);
                        dm.ExecuteNonQuery();
                        string alertmessage = "";
                        alertmessage = "Component Details Saved";
                        this.CreateMessageAlert(this, alertmessage, "alertKey");
                    }
                    else
                    {
                        Response.Write("<script>alert('Invalid Username/Password')</script>");
                    }
                }
                con.Close();

            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
4

2 回答 2

2

看起来您的下拉列表之一很可能没有选择任何选项,即空引用来自以下行之一:

dm.Parameters.AddWithValue("@mc", Mcodeddl.SelectedItem.Text);

在检索 .Text 属性之前,请尝试检查所有这些是否已选择项目。

如果不是这样,那么知道哪一行导致了异常会很有用——你通常可以从异常堆栈跟踪中得到它。

于 2012-06-26T06:47:12.773 回答
0

这意味着您尚未初始化或分配变量。调试器应该具体告诉你哪个变量。仔细看看它。然后你只需要检查它是否已经初始化(= new Class())或分配(= instance)。

你可能想看看这个问题

于 2012-06-26T06:46:54.117 回答