locked
ExecuteNonQuery: Connection property has not been initialized. RRS feed

  • Question

  •  Hi friends,

    am getting this error 

    i tried in many ways but still i can't rectify can some one help me out of this

    string cds = "Data Source=****; Initial Catalog=Sindhu; User id=sa;Password="******";
                using (SqlConnection con = new SqlConnection(cds))
                {

                    SqlCommand cmd = new SqlCommand("INSERT INTO Registration (FirstName,LastName,UserName,Password,ConfirmPassword,Gender,Address,Email) values ('" + txtFirstName.Text + "','" + txtLastName.Text + "','" + txtUserName.Text + "','" + txtPassword.Text + "','" + txtRePwd.Text + "," + rdoGender.SelectedItem.Value + "','" + txtAddress.Text + "','" + txtEmailID.Text + "'), con");
                    con.Open();
                    cmd.ExecuteNonQuery();
                    Response.Redirect("Login.aspx");
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    con.Close();


    Friday, February 5, 2016 9:58 AM

Answers

  • You have several syntax errors in your code. I fixed the syntax errors but did not change any of your logic:

                string cds = "Data Source=****;Initial Catalog=Sindhu; User id=sa;Password=******";
                using (SqlConnection con = new SqlConnection(cds))
                    {
                    SqlCommand cmd = new SqlCommand("INSERT INTO Registration (FirstName,LastName,UserName,Password,ConfirmPassword,Gender,Address,Email) values ('" + txtFirstName.Text + "','" + txtLastName.Text + "','" + txtUserName.Text + "','" + txtPassword.Text + "','" + txtRePwd.Text + "," + rdoGender.SelectedItem.Value + "','" + txtAddress.Text + "','" + txtEmailID.Text + "'");
                    cmd.Connection = con;
    
                    con.Open();
                    cmd.ExecuteNonQuery();
                    Response.Redirect("Login.aspx");
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    con.Close();
    
                    }


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, February 5, 2016 3:37 PM