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.
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.