Mjvcj
Mjvcj

Reputation: 23

How to insert other values into database in Visual Studio when there is an ID auto incremented

Userid is auto incremented value and giving default as its value doesn't work

string inse = @"insert into registration (userid, firstname, lastname, day, 
                                          month, year, address, city, pincode, 
                                          state, country, phoneno, emailaddress) 
                values  (DEFAULT, @firstname, @lastname, @day, 
                         @month, @year, @address, @city, @pincode,
                         @state, @country, @phoneno, @emailaddress)";

SqlCommand cmd = new SqlCommand(inse, con);
cmd.Parameters.AddWithValue("@firstname", txtfname.Text);
cmd.Parameters.AddWithValue("@lastname", txtlname.Text);
.. 

etc

Upvotes: 2

Views: 120

Answers (1)

Dejan
Dejan

Reputation: 113

Just don't add it at all in the query, if it's auto-incremented then the database will handle the ID.

Query that should work:

string inse = @"insert into registration(firstname,lastname,day,month,year,
               address,city,pincode,state,country,phoneno,emailaddress) 
               values  
               (@firstname,@lastname,@day,@month,@year,@address,@city,@pincode,
               @state,@country,@phoneno,@emailaddress)";

Upvotes: 2

Related Questions