1 Replies - 17752 Views - Last Post: 04 January 2009 - 11:53 AM Rate Topic: -----

#1 sawman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-February 07

Object reference not set to an instance of an object.

Post icon  Posted 04 January 2009 - 11:19 AM

The following code generates the System.NullReferenceException .

	private void bnSubmit_Click(object sender, System.EventArgs e)
		{
			if (Page.IsValid)
			{
				SqlConnection conekt = new SqlConnection(DB_CONN_STRING);

				Content content = new Content(conekt);

				try
				{
					content.Insert(Convert.ToInt32(tbfamilyID.Text), tbfName.Text, tbmName.Text, tblName.Text, tbMaiden.Text, tbDOB.Text, tbSOB.Text, tbCoOB.Text, tbCiOB.Text, tbDOD.Text, tbSOD.Text, tbCoOD.Text, tbCiOD.Text, tbCaOD.Text, tbMiscDetails.Text);

					tbfName.Text = "";
					tbmName.Text = "";
					tblName.Text = "";
					tbMaiden.Text = "";
					tbDOB.Text = "";
					tbSOB.Text = "";
					tbCoOB.Text = "";
					tbCiOB.Text = "";
					tbDOD.Text = "";
					tbSOD.Text = "";
					tbCoOD.Text = "";
					tbCiOD.Text = "";
					tbCaOD.Text = "";
					tbMiscDetails.Text = "";
				} // End try.
				catch (SqlException err)
				{
					lbError.Text = "Sorry, the following error occured: " + err.Message;
				} // End catch.
			} // End if.

		} //---------------------------------------- End private void bnSubmit_Click().


This code is in the .cs file for a C# .aspx file. When trying to submit data to SQL Server 2005 on the bnSubmit_Click event the System.NullReferenceException is generated.

The source error: points to the content.Insert method as seen in the above code. I know this is generated because I'm trying to use a reference variable who's value is Nothing/null. However, there are cases where the data from at least one of the TextBox's is going to be empty and I don't need any data sent to that particular record in the table.

This is the Stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
Genealogy.Content.Insert(Int32 familyID, String firstName, String middleName, String lastName, String maidenName, String dateOfBirth, String stateOfBirth, String countyOfBirth, String cityOfBirth, String dateOfDeath, String stateOfDeath, String countyOfDeath, String cityOfDeath, String causeOfDeath, String miscDetails) +1186
Genealogy.AddIndividuals.bnSubmit_Click(Object sender, EventArgs e) in c:\staging\Genealogy\addIndividuals.aspx.cs:94
System.Web.UI.WebControls.Button.onclick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921


Any ideas on how I can make this work?

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Object reference not set to an instance of an object.

Posted 04 January 2009 - 11:53 AM

just as a programming "best practice", if you are passing more than 3 parameters to a method, then you should be creating a class.

How about creating a class that contains those 15 fields, and then pass the class? You could then put a breakpoint at the insert statement and see which of the fields is null.

Blank data should be fine. Null data is not.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1