Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C# 2008 object instance problem. Please help?
So I am having a bit of a problem here with a class assignment I'm doing. Yes we have to use visual studio 2008. It's a payroll system that stores the employees name, pay rate, start date, and end date into a database. Now everything executes fine until I start inputting the information. It says "NullReferenceException was unhandled by user code" with the following description: "Object reference not set to an instance of an object".
Data layer code:
// This function saves the personnel data
public static bool SavePersonnel(string Database, string FirstName, string LastName,
string PayRate, string StartDate, string EndDate)
{
bool recordSaved;
try
{
// Add your comments here
OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Database);
conn.Open();
OleDbCommand command = conn.CreateCommand();
string strSQL;
// Add your comments here
strSQL = "Insert into tblPersonnel " +
"(FirstName, LastName, PayRate, StartDate, EndDate) values ('" +
FirstName + "', '" + LastName + "', " + PayRate + ", '" + StartDate +
"', '" + EndDate + "')";
// Add your comments here
command.CommandType = CommandType.Text;
command.CommandText = strSQL;
// Add your comments here
command.ExecuteNonQuery();
//closes connection
conn.Close();
recordSaved = true;
}
catch (Exception ex)
{
recordSaved = false;
}
return recordSaved;
}
Here is the frmPersonnelVerified verifying code
// saving the personnel information
if (clsDataLayer.SavePersonnel(Server.MapPath("PayrollSystem_DB.mdb"),
Session["txtFirstName"].ToString(),
Session["txtLastName"].ToString(),
Session["txtPayRate"].ToString(),
Session["txtStartDate"].ToString(),
Session["txtEndDate"].ToString()))
{
txtVerifiedInfo.Text = txtVerifiedInfo.Text +
"\nThe information was successfully saved!";
}
else
{
txtVerifiedInfo.Text = txtVerifiedInfo.Text +
"\nThe information was NOT saved.";
}
Now when I click "continue" my webpage shows this line of code highlighted:
if (clsDataLayer.SavePersonnel(Server.MapPath("PayrollSystem_DB.mdb"),
Any tips? If you need more information I'll give more.
typo:
If you can't see past the "Server.MapPath", this is what it looks like:
Server.MapPath("PayRollSystem_DB.mdb")
@Mike: I don't expect you to trace it, I just thought it was obvious. I apologize, the line that is highlighted says line 33 in my program "if(clsDataLayer.SavePersonnel(Server.MapPath("PayRollSystem.DB.mdb")" at the bottom of my question.
@Rachet: According to quick watch...it says that all the "txt####" do not exist in the current context...great....
2 Answers
- Anonymous7 years agoFavorite Answer
"Object reference not set" happens when a value is unexpectedly null. Of course you didn't tell us the line number (I guess you expect us to trace it out?). I'm not going to mentally compile and trace through your program for you. Set break points. Step through your code. Find the line it's failing on. Find the variable on that line that is null. Then find out why it's null. If you still cant figure it out tell us at least what line it fails on.
- RatchetrLv 77 years ago
Almost certainly, at least one of your Session["txtXXXX"] values is null. The exception will happen because of the ToString cal. (null.ToString() generates that exception).
Set a breakpoint on that line by clicking somewhere in that code, and hitting F9. There should be a red dot on left side of screen.
Run under debugger by hitting F5. Enter your data.
When you get the the breakpoint, Visual Studio should pop back up. If not, switch to it.
Highlight this part of your code: Session["txtFirstName"]. Right Click the highlighted text, and select QuickWatch. What does it show for the value of Session["txtFirstName"]? Is it null? If so, there is your problem. If not, repeat for all the other Session variables until you find that one that is null. Then figure out why it is null.
ETA: Are you putting these txtXXX values in Session state at some point? Or are they text controls on your current page? If you are using Session state, then you need to figure out why that isn't happening. If they are text controls on your page, access the contents as txtXXXX.Text.