Saturday, March 24, 2012

System.NullReferenceException

I'm learning C# and I seem to be making all sorts of mistakes. Could anyone help me troubleshoot my error? I tried to document the logic as best as I could.

// PassID: Call GetEmail Stored Procedure: Grab Emails from every contact that is equal to the value of x.
// x represents the value of each checkbox the user checked on SendEmail.aspx

private void PassID(string x)
{
// Set connection string to database
//string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["FasR6ConnectionString"].ToString();

string ConnectionString = "Data Source=NOTEBOOK;Initial Catalog=FasR6;Integrated Security=True";


// Create new connection and pass in the value of the connection string as a parameter
// Use GetEmail to call the stored procedure which will return the emails to send out.
SqlConnection ConnectToDatabase = new SqlConnection(ConnectionString);
string GetEmail = "spGetEmail";

//Create SqlCommand object and pass in the stored procedure and connection as parameters
// Define the command type as a stored procedure
SqlCommand objCommand = new SqlCommand(GetEmail, ConnectToDatabase);
objCommand.CommandType = CommandType.StoredProcedure;

// Define a SqlParamenter of type Int to hold the value of SinID.
SqlParameter objParameter = new SqlParameter("@dotnet.itags.org.ID", SqlDbType.Int);
objCommand.Parameters.Add(objParameter);
objParameter.Direction = ParameterDirection.Input;
objParameter.Value = x; // x is the value of each checkbox that is checked

//create a new data adapter and pass in sql command via my stored procedure.
SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand);
DataSet objDataSet = new DataSet("dtEmail"); //Create a data table named dtEmail to hold what the stored procedure returns.
objAdapter.Fill(objDataSet); //Fill the dataset


if (objDataSet.Tables["dtEmail"].Rows.Count != 0) // <=============== System.NullReferenceException was unhandled by user code (Details Below)

{

ContactIdentifier = null;
string tempString = objDataSet.Tables["dtEmail"].Rows[0]["ContractorID"].ToString();
string tempString2 = Convert.ToString(ContactIdentifiers.Find(tempString));


if (tempString == null)
{
ContactIdentifier = ContactIdentifiers.Add();
ContactIdentifier.EmailAddress = (objDataSet.Tables["dtEmail"].Rows[0]["Email1"].ToString());
}
}

*******************************************************************************************************************************

//System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Source="App_Web_tfcl-8tl"
StackTrace:
at Partnering_SendEmail.PassID(String x) in ****************************:line 74
at Partnering_SendEmail.btnSubmit_Click(Object sender, EventArgse) in ******************************SendEmail.aspx.cs:line 33
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The below line is wrong:

DataSet objDataSet = new DataSet("dtEmail"); //Create a data table named dtEmail to hold what the stored procedure returns.

It doesn't create a data table called "dtEmail" but it creates a dataset named "dtEmail". So you will not have a data table in the dataset with this name. If you want to name the datatable inside a dataset use:

objAdapter.Fill(objDataSet, "dtEmail"); //Fill the dataset


woohooo it works!!!!!!!

Thanks for all of the help!

0 comments:

Post a Comment