I’ve converted a textbox to accept integers. But when l enter the a numerical value into the textbox and click ‘Find’ button, an error occurs (NullReferenceException), can any1 help me to solve this problem? If possible pls tell me the reason of the error.
private void Find_Click(object sender, EventArgs e)
{
int convertedBranchID;
convertedBranchID = Convert.ToInt32(branchID.Text);
convertedBranchID = int.Parse(branchID.Text);
string selectDayOfWeek = dayOfWeek.Items[dayOfWeek.SelectedIndex].ToString();
//dayOfWeek is the name of a combobox
//'NullReferenceException was unhandled' occurs here....
DataRow[] findBranchID = RetailCamDataSet1.Tables["smBranchWorkingDayInfo"].Select("BranchID='" + searchBranchID + "'");
branchIDResult = findBranchID.Length;
if (dayOfWeek.SelectedItem.ToString() == "Sunday")
{
}
}
RetailCamDataSet1 or the Table you are indexing into are null. If RetailCamDataSet1 is null and you call the Tables property, you are trying to reference a null.
Same with the Select. For example, if there isn’t a table named smBranchWorkingDayInfo (typo? not filled?) then indexing into that will return null so calling Select against a null object will lead to a null reference exception.
Set a break point on the line before and examine the RetailCamDataSet1object (make sure it’s not null). You can also try adding some debug code above it that iterates over the tables and see if that table is present.