Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can anyone help me why i am getting this error?thanks in advance..



Cannot insert explicit value for identity column in table 'OS_Mailinglist1' when IDENTITY_INSERT is set to OFF.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'OS_Mailinglist1' when IDENTITY_INSERT is set to OFF.

Source Error:


Line 310:
Line 311:
Line 312: cmd.ExecuteNonQuery();
Line 313: ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Successsfully Registered your Email ID and Area of interest.');", true);
Line 314:

Posted
Updated 28-Sep-16 21:13pm

 
Share this answer
 
Comments
nityasri 10-Dec-12 6:35am    
@kiran thank u
this is because you are passing some value in a column which is set as identity (auto-increment).

hope it helps.
otherwise, please send the query u r passing to command object.
 
Share this answer
 
Comments
nityasri 10-Dec-12 6:35am    
@ankit..thanks a lot
AnkitGoel.com 10-Dec-12 6:37am    
please mark as answer if helped you.
nityasri 10-Dec-12 6:50am    
This is were i am getting the error...

protected void btnSubscribe_Click(object sender, EventArgs e)
{
string arrvalues = null;
for (int i = 0; i < CBL1.Items.Count ; i++)
{
if (CBL1.Items[i].Selected)
{
arrvalues = arrvalues + CBL1.Items[i].Value + ",";
}
}

if (arrvalues==null)
{

}
else
{
if (arrvalues.EndsWith(","))
{
arrvalues = arrvalues.Remove(arrvalues.Length - 1, 1);
}
My_Util Util = new My_Util();
DataSet ds;
ds = Util.CheckusernameMail(txtEmail.Text.Trim());
if (ds.Tables[0].Rows.Count > 0)
{
//My_Util Util = new My_Util();
Util.SetConnection();
Util.sql_con.Open();
SqlCommand cmd;
// cmd = new SqlCommand("insert into offsale.OS_Mailinglist1(UserName,Area_of_interest,Mail_Pwd)values('" + txtEmail.Text.ToString() + "' ,'" + arrvalues + "','" + pwd.ToString() + "')", Util.sql_con);

cmd = new SqlCommand("update offsale.OS_Mailinglist1 set Area_Of_Interest='" + arrvalues + "' where UserName='" + txtEmail.Text.ToString() + "'", Util.sql_con);
cmd.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Area of Interest Updated Successfully');", true);

Util.sql_con.Close();

foreach (ListItem item in CBL1.Items)
{
if (item.Selected == true)
{
item.Selected = false;
}
}

txtEmail.Text = "";
Label1.Visible = false;
passwrd.Visible = false;
btnSend.Enabled = true;
btnSubscribe.Enabled = false;

}
else
{
Util.SetConnection();
Util.sql_con.Open();
cmd = new SqlCommand("insert into offsale.OS_Mailinglist1(UserName,Area_Of_Interest,Mail_Pwd)values('" + txtEmail.Text.ToString() + "' ,'" + arrvalues + "','" + ViewState["pwd"].ToString () + "')", Util.sql_con);
// cmd = new SqlCommand("insert into offsale.OS_Mailinglist1(UserName,Area_of_interest,Mail_Pwd)values('" + txtEmail.Text.ToString() + "' ,'" + arrvalues + "','" + passwrd.Text.ToString () + "')", Util.sql_con);



cmd.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Successsfully Registered your Email ID and Area of interest.');", true);

//ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Register your Password to MailingList')", true);
// return;
Util.sql_con.Close();

foreach (ListItem item in CBL1.Items)
{
if (item.Selected == true)
{
item.Selected = false;
}
}

txtEmail.Text = "";
Label1.Visible =false ;
passwrd.Visible =false ;
btnSend.Enabled = true;
Label2.Visible = true;
Label2.Text = "Please select your Area of Interest & Subscribe";
btnSubscribe.Enabled = false;


}
}
}
AnkitGoel.com 10-Dec-12 6:54am    
r u still getting errors? if yes, i need your table create script
Do not put values in identity column.
most of time identity column we consider as id. so make sure if your id is identity column then in insert query consider id.
for example
i am putting a query of stored procedure as follows,
ALTER procedure [dbo].[sp_registration](
@pkid int=null,
@userName nvarchar(100),
@password nvarchar(100),
@email nvarchar(100),
@mode nvarchar(10)
)
as
begin
if @mode='signUp'
begin
insert into registration(userName, email, password, status, createdOn) values(@userName, @email, @password, 1, GETDATE());
end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900