-7
public partial class SignUp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void BtSignup_Click(object sender, EventArgs e)
    {
        String CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("insert into Users values('"+TbUname.Text+"', '"+TbPass.Text+"','"+TbEmail.Text+"', '"+TbName.Text+"')",con);
            con.Open();
            cmd.ExecuteNonQuery(); //error in this line
        }
    }

}
Kyle B
  • 2,328
  • 1
  • 23
  • 39

2 Answers2

1

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();
}
Kyle B
  • 2,328
  • 1
  • 23
  • 39
  • Cannot insert the value NULL into column 'Id', table 'E:\VS_PROJECTS\SAMPLEWEBSITE\APP_DATA\MYDATABASE.MDF.dbo.Users'; column does not allow nulls. INSERT fails. The statement has been terminated. (This error shows according to your code ) – Mohammed Saquib May 15 '18 at 11:38
  • @MohammedSaquibSiddique, for "Cannot insert the value NULL", see: https://stackoverflow.com/questions/10013313/why-is-sql-server-throwing-this-error-cannot-insert-the-value-null-into-column – Kyle B May 15 '18 at 11:42
0

insert into Users (dbcol1, dbcol2, dbcol3, dbcol4)values('"+TbUname.Text+"', '"+TbPass.Text+"','"+TbEmail.Text+"', '"+TbName.Text+"')

here dbcol is means it your table coloumn name....

try this..

and also in your database table id is not allowing null values. i think your coloumn(id) is primary key. you can`t pass null either you change or else you should pass value in (id)coloumn..

Naveed
  • 46
  • 4