[Solved] Error when clicking on button to save value in database


The two likely problems that I can see without you adding the error code.

  1. is that you are missing the column names in your insert statement.
  2. is that a user is putting an apostrophe in one of your text boxes. This is a SQL Injection vulnerability.

Try something similar to this instead:

using (SqlConnection con = new SqlConnection(CS))
{
    // add the columns and do not concatenate strings in SQL statements
    SqlCommand cmd = new SqlCommand(@"insert into Users 
         (username, password, email, name) 
           values 
         (@username, @password, @email, @name)", con);

    // add values by using sql parameters
    cmd.Parameters.AddWithValue("@username", TbUname.Text);
    cmd.Parameters.AddWithValue("@password", TbPass.Text);
    cmd.Parameters.AddWithValue("@email", TbEmail.Text);
    cmd.Parameters.AddWithValue("@name", TbName.Text);

    con.Open();

    cmd.ExecuteNonQuery();
}

2

solved Error when clicking on button to save value in database