ASP.Net Error: ExecuteNonQuery: Connection property has not been initialized

Last Reply 4 months ago By dharmendr

Posted 4 months ago

I amtrying to upload image directly in database but everytime i got error related to connection.

Error-

System.InvalidOperationException: ExecuteNonQuery: Connection property has not been initialized. at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Jobopedia.Q_SP_Afterlogin.btnLogin_Click(Object sender, EventArgs e) in C:\Users\veenita\Documents\Visual Studio 2017\Projects\Jobopedia\Jobopedia\Q_SP_Afterlogin.aspx.cs:line 109

            try
            {
                // Extract Image File Name.
                 string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

                //Set the Image File Path.
                string filePath = "~/Images/" + fileName;

                //Save the Image File in Folder.
                FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));

                string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    //string sql = "INSERT INTO Q_SP_Afterlogin (Bussiness_name,city,email,name,toll_free,website_link,payment_mode,Image) VALUES (@Bname,@DropDownList2,@mail,@name,@toll,@link,@CheckBoxList1,@Image)";
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO Q_SP_Afterlogin (Bussiness_name,city,email,name,toll_free,website_link,payment_mode,Image) VALUES (@Bname,@DropDownList2,@mail,@name,@toll,@link,@CheckBoxList1,@Image)", conn))
                    {
                        cmd.Parameters.AddWithValue("@Bname", DropDownList1.SelectedValue.ToString());
                        cmd.Parameters.AddWithValue("@DropDownList2", DropDownList2.SelectedValue.ToString());
                        cmd.Parameters.AddWithValue("@mail", mail.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@toll", toll.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@link", link.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@name", name.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("@CheckBoxList1", CheckBoxList1.SelectedValue.ToString());
                        cmd.Parameters.AddWithValue("@Image", filePath);
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                try
                {
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        lab.Text = "Business information is added successfully.";
                        lab.Visible = true;
                    }
                    else
                    {
                        lab.Text = "Error while adding..!!";
                    }
                }
                catch(Exception ex)
                {
                    lab.Text = "information already added";
                    Response.Write(ex);

                }                

 

You are viewing reply posted by: dharmendr 4 months ago.
Posted 4 months ago

Replace with below code.

int i = 0;
try
{
    // Extract Image File Name.
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

    //Set the Image File Path.
    string filePath = "~/Images/" + fileName;

    //Save the Image File in Folder.
    FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));

    string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(constr))
    {
        //string sql = "INSERT INTO Q_SP_Afterlogin (Bussiness_name,city,email,name,toll_free,website_link,payment_mode,Image) VALUES (@Bname,@DropDownList2,@mail,@name,@toll,@link,@CheckBoxList1,@Image)";
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Q_SP_Afterlogin (Bussiness_name,city,email,name,toll_free,website_link,payment_mode,Image) VALUES (@Bname,@DropDownList2,@mail,@name,@toll,@link,@CheckBoxList1,@Image)", conn))
        {
            cmd.Parameters.AddWithValue("@Bname", DropDownList1.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@DropDownList2", DropDownList2.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@mail", mail.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@toll", toll.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@link", link.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@name", name.Text.Trim().ToString());
            cmd.Parameters.AddWithValue("@CheckBoxList1", CheckBoxList1.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@Image", filePath);
            conn.Open();
            i = cmd.ExecuteNonQuery();
            conn.Close();
        }
    }
    if (i > 0)
    {
        lab.Text = "Business information is added successfully.";
        lab.Visible = true;
    }
    else
    {
        lab.Text = "Error while adding..!!";
    }
}
catch (Exception ex)
{
    Response.Write(ex);
}