简体   繁体   中英

Cannot insert explicit value for identity column in table 'candidatedetails' when IDENTITY_INSERT is set to OFF

I am geting this error. How should I solve this?

This is my create query

CREATE TABLE [dbo].[candidatedetails](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [cname] [varchar](50) NULL,
    [pname] [varchar](50) NULL,
    [cno] [varchar](50) NULL,
    [pno] [varchar](50) NULL,
    [address] [varchar](max) NULL,
    [year] [varchar](50) NULL,
    [class] [varchar](50) NULL,
    [branch] [varchar](50) NULL,
    [totalfees] [numeric](18, 0) NULL,
    [paidfees] [numeric](18, 0) NULL,
    [pendingfees] [numeric](18, 0) NULL,
    [idno] [int] NULL,
 CONSTRAINT [PK_candidatedetails] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)

This is the code

SqlConnection con = new SqlConnection(constring);
            con.Open();
            if(con.State==ConnectionState.Open)
            {
                string q = "INSERT INTO candidatedetails(id,cname,pname,cno,pno,address,year,class,branch,totalfees,paidfees,pendingfees,idno) VALUES ('','"+cname+"','"+pname+"','"+cno+"','"+pno+"','"+address+"','"+year+"','"+class1+"','"+branch+"','"+totalfees+"','"+paidfees+"','"+pendingfees+"','"+idno+"')";
                SqlCommand com = new SqlCommand(q, con);
                com.ExecuteNonQuery();
                MessageBox.Show("Candidate is registered");
            }

I am getting this error on line "com.executenonquery()"

Your id column is declared as IDENTITY so it auto-generates a value on insertion. You don't have to include it on your insert column list nor supply a value for it.

string q = "INSERT INTO candidatedetails(cname,pname,cno,pno,address,year,class,branch,totalfees,paidfees,pendingfees,idno) VALUES ('"+cname+"','"+pname+"','"+cno+"','"+pno+"','"+address+"','"+year+"','"+class1+"','"+branch+"','"+totalfees+"','"+paidfees+"','"+pendingfees+"','"+idno+"')";

Also, beware of generating your SQL dynamically like this, it's prone to errors (if one of your values has an unescaped single quote for example) and to SQL injection if the values come from user input or another unsafe source.

You should either create a Stored Procedure that receives these parameters, or build the sql with sp_executesql .

just Remove id form sql query: Try this:

 SqlConnection con = new SqlConnection(constring);
                con.Open();
                if(con.State==ConnectionState.Open)
                {
                    string q = "INSERT INTO candidatedetails(cname,pname,cno,pno,address,year,class,branch,totalfees,paidfees,pendingfees,idno) VALUES ('"+cname+"','"+pname+"','"+cno+"','"+pno+"','"+address+"','"+year+"','"+class1+"','"+branch+"','"+totalfees+"','"+paidfees+"','"+pendingfees+"','"+idno+"')";
                    SqlCommand com = new SqlCommand(q, con);
                    com.ExecuteNonQuery();
                    MessageBox.Show("Candidate is registered");
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM