janek jan
janek jan

Reputation: 1

An explicit value for the identity column in table 'Users' can only be specified when a column list is used and IDENTITY_INSERT is ON

I've been trying to make my SQL Server database work with my aspx project. I have created a table for Users who reads data from my webform text fields. However, for whatever reason it tries to assign my Users table id column to the 1st textbox in my web project. I cannot seem to think of a way to fix it. Could you help me?

CREATE TABLE [dbo].[Users]
(
    [Id] INT NOT NULL PRIMARY KEY, 
    [Username] NVARCHAR(MAX) NULL, 
    [Password] NVARCHAR(MAX) NULL, 
    [Email] NVARCHAR(MAX) NULL, 
    [Name] NVARCHAR(MAX) NULL
)

and here I assign the values from webform:

SqlCommand cmd = new SqlCommand("insert into Users values('" + tbUname.Text + "','" + tbPass.Text + "','" + tbEmail.Text + "','" + tbName.Text + "','u')", con);

The webform markup:

        <div class="center-page">
            <label class="col-xs-11">Username</label>
            <div class="col-xs-11">
                <asp:TextBox ID="tbUname" runat="server" CssClass="form-control" placeholder="Username"></asp:TextBox>

            </div>
            <label class="col-xs-11">Password</label>
            <div class="col-xs-11">
                <asp:TextBox ID="tbPass" runat="server" CssClass="form-control" placeholder="Password"></asp:TextBox>
            </div>
            <label class="col-xs-11">Confirm Password</label>
            <div class="col-xs-11">
                <asp:TextBox ID="tbCPass" runat="server" CssClass="form-control" placeholder="Confirm Password"></asp:TextBox>
            </div>
            <label class="col-xs-11">Name</label>
            <div class="col-xs-11">
                <asp:TextBox ID="tbName" runat="server" CssClass="form-control" placeholder="Name"></asp:TextBox>
            </div>

            <label class="col-xs-11">E-Mail</label>
            <div class="col-xs-11">
                <asp:TextBox ID="tbEmail" runat="server" CssClass="form-control" placeholder="Email"></asp:TextBox>
            </div>
            <br />
            <br />
            <br />
            <div class="col-xs-11 space-vert">
                <asp:Button ID="btSignup" runat="server" class="btn btn-success" Text="Sign Up" OnClick="btSignup_Click" />
            </div>
        </div>

Upvotes: 0

Views: 9886

Answers (3)

akmaurya7
akmaurya7

Reputation: 5

To solve the issue: Replace this query...

SqlCommand cmd = new SqlCommand("insert into Users values('" + tbUname.Text + "','" + tbPass.Text + "','" + tbEmail.Text + "','" + tbName.Text + "','u')", con);

with this one down here

SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[Users] ( [Username], [Password], [Email],[Name])VALUES('" + tbUname.Text + "','" + tbPass.Text + "','" + tbEmail.Text + "','" + tbName.Text + "')", con);

Upvotes: 0

Jeffry Evan
Jeffry Evan

Reputation: 327

is your ID fiels is identity field with auto increment? i think its not because i don't see you use

IDENTITY(1,1)

in your create table, then you need to use it in your insert statement

INSERT INTO dbo.Users(Id,Username, Password, Email, Name)
VALUES (@val1, @val2, @val3, @val4,@val5);

Upvotes: 1

marc_s
marc_s

Reputation: 754488

You just simply need to explicitly specify the columns you want to insert into in your INSERT command - something like this:

INSERT INTO dbo.Users(Username, Password, Email, Name)
VALUES (@val1, @val2, @val3, @val4);

Upvotes: 2

Related Questions