C#错误“对象引用未设置为对象的实例" [英] C# Error 'Object Reference Not Set To An Instance Of An Object'
本文介绍了C#错误“对象引用未设置为对象的实例"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经阅读了有关此问题的其他问题,但看不到我的代码在何处或为什么引发此错误.如果有人可以看到我要去哪里,我的代码在下面.
I've read other questions about this but don't see where or why my code is throwing this error. The code I have is below, if anyone can see where i'm going wrong.
private void btnSignIn_Click(object sender, EventArgs e)
{
sqlConnectionNW.ConnectionString = "Data Source=" + server + ";Initial Catalog=Northwind;Integrated Security=True";
try
{
sqlConnectionNW.Open();
String enteredDate = cmbDay.SelectedItem.ToString() + "/" + cmbMonth.SelectedItem.ToString() + "/" + cmbYear.SelectedItem.ToString();
if ((txtEmployeeID.TextLength != 0) && (enteredDate.Length != 0))
{
int ID = Convert.ToInt32(txtEmployeeID.Text);
employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";
String birthDate;
birthDate = dsEmployees.Employees.FindByEmployeeID(ID).BirthDate.ToShortDateString();
if (employeesBindingSource.Count > 0) //GETS TO HERE AND THEN GOES TO CATCH EX
{
sqlConnectionNW.Close();
if (enteredDate.ToString() == birthDate.ToString())
{
frmMenu frmMainMenu = new frmMenu();
frmMainMenu.server = server;
frmMainMenu.employeeID = txtEmployeeID.Text;
MessageBox.Show("Welcome to the Northwind Ordering System");
this.Hide();
frmMainMenu.Show();
}
else
{
MessageBox.Show("The birth date you have entered is incorrect");
}
}
else
{
MessageBox.Show("The Employee ID you have entered does not exist");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
sqlConnectionNW.Close();
}
推荐答案
设法解决此问题.我改变了这个:
Manage to solve this problem. I changed this:
if ((txtEmployeeID.TextLength != 0) && (enteredDate.Length != 0))
{
int ID = Convert.ToInt32(txtEmployeeID.Text);
employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";
String birthDate;
birthDate = dsEmployees.Employees.FindByEmployeeID(ID).BirthDate.ToShortDateString(); // FROM HERE
if (employeesBindingSource.Count != 0)
{
sqlConnectionNW.Close();
对此:
如果((txtEmployeeID.TextLength!= 0)&&(enteredDate.Length!= 0)) {
if ((txtEmployeeID.TextLength != 0) && (enteredDate.Length != 0)) {
int ID = Convert.ToInt32(txtEmployeeID.Text);
employeesBindingSource.Filter = "EmployeeID ='" + txtEmployeeID.Text + "'";
String birthDate;
if (employeesBindingSource.Count != 0)
{
birthDate = dsEmployees.Employees.FindByEmployeeID(ID).BirthDate.ToShortDateString(); //TO HERE
sqlConnectionNW.Close();
这只是在if语句后放置"birthDate = ..."的情况.
It was just a case of putting the "birthDate = ..." after the if statement.
这篇关于C#错误“对象引用未设置为对象的实例"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文