We help IT Professionals succeed at work.
Get Started

ExecuteNonQuery: Connection property has not been initialized.

3,934 Views
Last Modified: 2013-12-17
OK - I have looked at several similar cases, but I just don't know enough to find the problem.  Can anyone tell me why the following code behind file is throwing the error at the bottom of the page?

-----------------  Here is the code behind file ----------------
using System;
using System.Text.RegularExpressions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using AspDotNetStorefrontCore;
using System.Data.Sql;
using System.Data.SqlClient;


namespace AspDotNetStorefront
{

public partial class createaccount : SkinBase
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
      int addResult = 0;
      addResult = AddLead(txtCampaignID.Text, txtEmail.Text);
      switch (addResult) {
            case 0:
                  Message.Text = "Success";
                  goto case -1;
            case -1:
                  Message.Text = "Failure - record already exists";
                  goto default;
            default:
                  Message.Text = "Please complete ALL required fields and try again.";
                  break;
      }
      txtCampaignID.Text = "";
      txtEmail.Text = "";
}

using (SqlConnection con = new SqlConnection(DB.GetDBConn()))
                {
                    con.Open();
      dbCommand.CommandText = "spAddLead";
      dbCommand.CommandType = CommandType.StoredProcedure;
      dbCommand.Parameters.Add(new SqlParameter("@FName", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@FName"].Value = txtFName;
      dbCommand.Parameters.Add(new SqlParameter("@LName", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@LName"].Value = txtLName;
      dbCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@Email"].Value = txtEmail;
      dbCommand.Parameters.Add(new SqlParameter("@Phone", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@Phone"].Value = txtPhone;
      dbCommand.Parameters.Add(new SqlParameter("@Addr1", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@Addr1"].Value = txtAddr1;
      dbCommand.Parameters.Add(new SqlParameter("@Addr2", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@Addr2"].Value = txtAddr2;
      dbCommand.Parameters.Add(new SqlParameter("@City", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@City"].Value = txtCity;
      dbCommand.Parameters.Add(new SqlParameter("@State", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@State"].Value = txtState;
      dbCommand.Parameters.Add(new SqlParameter("@Zip", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@Zip"].Value = txtZip;
      dbCommand.Parameters.Add(new SqlParameter("@RequestType", SqlDbType.VarChar, 50));
      dbCommand.Parameters["@RequestType"].Value = QuoteReq;
      dbCommand.Parameters.Add(new SqlParameter("@CampaignID", SqlDbType.Int));
      dbCommand.Parameters["@CampaignID"].Value = txtCampaignID;
      dbCommand.Parameters.Add(new SqlParameter("@Result", SqlDbType.Int));
      dbCommand.Parameters["@Result"].Direction = ParameterDirection.ReturnValue;
      int commandResult = 1;
      try {
            con.Open();
            dbCommand.ExecuteNonQuery();
            commandResult = (int)dbCommand.Parameters["@Result"].Value;
      }
      catch (SqlException ex) {
            commandResult = ex.Number;
      }
      finally {
      con.Close();
      }
      return commandResult;
}
}
}
--------------- End Code Behind ----------------------

--------------  Begin Error Msg ----------------------
 ExecuteNonQuery: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized.

Source Error:

Line 71:       int commandResult = 1;
Line 72:       try {
Line 73:             using (SqlConnection con = new SqlConnection(DB.GetDBConn()))
Line 74:                 {
Line 75:                     con.Open();


Source File: \\fs1-n02\stor2wc1dfw1\400154\www.vinylreplacementwindowsquote.com\web\content\_Raleigh.aspx.cs    Line: 73

Stack Trace:

[InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized.]
   System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +4876133
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +105
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
   AspDotNetStorefront.createaccount.AddLead(String txtCampaignID, String txtEmail) in \\fs1-n02\stor2wc1dfw1\400154\www.vinylreplacementwindowsquote.com\web\content\_Raleigh.aspx.cs:73
   AspDotNetStorefront.createaccount.Button1_Click(Object sender, EventArgs e) in \\fs1-n02\stor2wc1dfw1\400154\www.vinylreplacementwindowsquote.com\web\content\_Raleigh.aspx.cs:26
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 11 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE