Morteza
Morteza

Reputation: 444

ExecuteNonQuery: Connection property has not been initialized

I have this code and when I run it gives this error ExecuteNonQuery: Connection property has not been initialized. And I have sql database. its name is Cost. I have this code and when I run it gives this error ExecuteNonQuery: Connection property has not been initialized. And I have sql database. its name is Cost. My code is:

    namespace Accountingss
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public SqlConnection conn;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Connect(string cmdtxt, Hashtable parameters)
        {
            conn = new SqlConnection();
            string connString = @"Data      Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Cost.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            conn.ConnectionString = connString;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = cmdtxt;
            cmd.Parameters.Clear();
            var ieParams = parameters.GetEnumerator();
            while (ieParams.MoveNext())
            {
                cmd.Parameters.AddWithValue(ieParams.Key.ToString(), ieParams.Value.ToString());
                //cmd.Parameters.Add(new SqlParameter(ieParams.Key.ToString(), ieParams.Value.ToString()));
            }

            conn.Open();
            cmd.ExecuteNonQuery();

            //SqlDataAdapter costdataAdpater = new SqlDataAdapter();
            //DataTable costdataTable = new DataTable(); 
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string insert = "INSERT INTO Cost (Type, Amount) VALUES (@type,         @amount)";// +type.Text + ',' + a.Text + ")";
            var addpTA = new Hashtable();
            addpTA.Add("@type", txtType.Text);
            addpTA.Add("@amount", txtAmount.Text);
            Connect(insert, addpTA);
        }
    }
}

Upvotes: 1

Views: 4783

Answers (4)

MSajjadi
MSajjadi

Reputation: 3839

You didn't connected the command to the connection.

cmd.Connection = conn;

And after executing the command you should close it.

conn.Close();

Upvotes: 3

Steve
Steve

Reputation: 216303

Simply connect the SqlConnection to your SqlCommand before executing

cmd.Connection = conn;

The Connection object is the tool that delivers our commands to the underlying database engine.
We need to plumb it to our commands if we want to reach the database.

A good shortcut is to create the command directly from the connection using this method

   SqlCommand cmd = conn.CreateCommand();

Upvotes: 1

Umesh
Umesh

Reputation: 2732

You have to assign connection to sql command like below. It seems that you have forgot to do so.

cmd.Connection = conn;

Upvotes: 3

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50742

You should pass connection to command

SqlCommand cmd = new SqlCommand(conn);

or

cmd.Connection = conn;

Upvotes: 1

Related Questions