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