Hi,

I am new to the field of SQL and C#. In my form I have few textboxes whose values are to be inserted into SQL table on Submit button click. I have created Insert.cs App_code class which handles the Database operations. And code behind will insert the textbox values into DB using a method in Insert.cs class. But code behind gives me an "Object reference not set to an instance of an object" exception. I don't know where I am going wrong here. Please do help me in this. Below is the sample code of Insert.cs and Code Behind.

Insert.cs

namespace InsertDataAccess
{
  public class Insert
  {
    public Insert()
    {
      //
      // TODO: Add constructor logic here
      //
    }
    public int insertINTOautoprequal(string code, string time, string first, string last, string email, string phoneno, string timetocall)
    {
      SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ToString());
      conn.Open();
      string query = "Insert INTO LoanOffer.autoprequal(offercode, timeofday, firstname, lastname, emailID, phone, besttimetocall) Values(@offercode, @time, @first, @last, @email, @phoneno, @timetocall);";
      SqlCommand cmd = new SqlCommand(query, conn);
      try
      {
        cmd.Parameters.AddWithValue("@offercode", code);
        cmd.Parameters.AddWithValue("@time", time);
        cmd.Parameters.AddWithValue("@first", first);
        cmd.Parameters.AddWithValue("@last", last);
        cmd.Parameters.AddWithValue("@email", email);
        cmd.Parameters.AddWithValue("@phoneno", phoneno);
        cmd.Parameters.AddWithValue("@timetocall", timetocall);
        return cmd.ExecuteNonQuery();
      }
      catch
      {
        throw;
      }
      finally
      {
        conn.Close();
      }
    }
  }
}

contactform_new.aspx.cs

public partial class contactform_new : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
 
  }
  protected void btnSubmit_Click(object sender, EventArgs e)
  {
    Insert ins = new Insert();
    DateTime dtime;
    dtime = DateTime.Now;
    string ocode = offercode.Text;
    string firstname = firstnamepreapp.Text;
    string lastname = lastnamepreapp.Text;
    string email = emailpreapp.Text;
    string phoneno = phonepreapp.Text;
    string timetocall = besttimepreapp.SelectedItem.Value;
    string time = dtime.ToString();
     
    //Insert the data into autoprequal table
    ins.insertINTOautoprequal(ocode, time, firstname, lastname, email, phoneno, timetocall);   ---->Shows the error on this line.
  }
}

Thanks.

Edited 5 Years Ago by user220: n/a

What does the stacktrace look like?

Here is the stack trace for the exception:

[NullReferenceException: Object reference not set to an instance of an object.]
InsertDataAccess.Insert.insertINTOautoprequal(String code, String time, String first, String last, String email, String phoneno, String timetocall) in c:\inetpub\wwwroot\App_Code\DAL\Insert.cs:23
loans_preapproval_contact_new.btnSubmit_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\loans\contactform_new.aspx.cs:34
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +153
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3690

Edited 5 Years Ago by user220: n/a

Is your ConnectionString defined within the web.config file? Also do the line numbers posted here match your line numbers in your file? It seem rather odd that when you are setting a string value it would complain about null.

Edited 5 Years Ago by cale.macdonald: n/a

Is your ConnectionString defined within the web.config file?

Yes, here is my web.config file

<?xml version="1.0"?>

<configuration>

  <system.web>
    <customErrors mode="Off" />
	<compilation debug="true" />
  </system.web>

  <system.webServer>
    <defaultDocument>
      <files>
        <add value="index.aspx" />
      </files>
    </defaultDocument>
  </system.webServer>

  <connectionStrings>
    <add name="connstring" connectionString="Data Source=DWH;Initial Catalog=Test;Integrated Security=True"/>
  </connectionStrings>

</configuration>

Do the line numbers posted here match your line numbers in your file? It seem rather odd that when you are setting a string value it would complain about null.

This article has been dead for over six months. Start a new discussion instead.