2964349
2964349

Reputation: 165

Having error with user registation in ASP.Net using C#

I am new to ASP and I am trying to insert the values in to the database. I get the following errors. "DEFAULT or NULL are not allowed as explicit identity values."

And if I try to send a empty value rather than NULL it gives a error saying "Cannot insert explicit value for identity column in table 'user_login' when IDENTITY_INSERT is set to OFF."

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class register : System.Web.UI.Page
{
public SqlConnection con;
public SqlDataAdapter data;
public DataSet dset;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void register_Click(object sender, EventArgs e)
{
    string fn = fullname.Text;
    string em = email.Text;
    string pass1 = password.Text;
    string pass2 = password2.Text;

    if (pass1 == pass2)
    {

        con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=ewadb;Integrated Security=SSPI");
        data = new SqlDataAdapter("INSERT INTO user_login (id, fullname, email, password, registered_date) VALUES ('','" + fn + "','" + em + "','" + pass1 + "','')", con);


        dset = new DataSet();
        data.Fill(dset);


    }
    else { 

    }
}
}

Do any one know how to fix this error.

Upvotes: 0

Views: 171

Answers (1)

Sumit Gupta
Sumit Gupta

Reputation: 2192

When you have a column set as Auto-increment you should not pass it's value. i.e. change your insert statement to skip "id" field, and do not pass it's value as well.

Upvotes: 4

Related Questions