这里是我的代码:的NullReferenceException是未处理 - 如何解决它
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=MANINOTEBOOK\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=Casesheet");
con.Open();
SqlCommand cmd = new SqlCommand("select PatientID from FTR where PatientID='" + textBox1.Text + "'", con);
textBox2.Text = cmd.ExecuteScalar().ToString();
if (textBox2.Text == textBox1.Text)
{
Consultation cs = new Consultation(textBox1.Text);
cs.Show();
}
else
{
MessageBox.Show("Data not found");
}
}
当我编译此代码我得到一个错误“的NullReferenceException是未处理”。我不知道如何解决它。我需要检查在“执行标量”命令中生成的值是否为空。请帮助我解决这个问题。
VS has'F9''F10'' F11' –
当你编译**代码或者**运行代码时,你会得到一个错误吗?由于可能在您运行**代码时,请指出导致错误的代码行。你的堆栈跟踪,或你的调试器将帮助你。 –
我怀疑你编译代码时会出错。我怀疑你运行代码时会出错。 –