SQL Server Error: Cannot insert explicit value for identity column in table 'Polls' when IDENTITY_INSERT is set to OFF

Last Reply 3 months ago By pandeyism

Posted 3 months ago

here is my c# Code

I am getting error

"Cannot insert explicit value for identity column in table Employee when IDENTITY_INSERT is set to"

public partial class Form1 : Form
{
    int inEmpID = 0;
    public Form1()
    {
        InitializeComponent();

    }
    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=MasterDetail;MultipleActiveResultSets=True;");
    private void Form1_Load(object sender, EventArgs e)
    {
        PositionComboBoxFill();
        FillEmployeeDataGridView();
    }

    private void FillEmployeeDataGridView()
    {
          
    }

    void PositionComboBoxFill()
    {
        {
            con.Open();
            SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM Position", con);
            DataTable dtbl = new DataTable();
            sqlDa.Fill(dtbl);
            DataRow topItem = dtbl.NewRow();
            topItem[0] = 0;
            topItem[1] = "-Select-";
            dtbl.Rows.InsertAt(topItem, 0);
            cmdPosition.ValueMember = dgvcmbPosition.ValueMember = "PositionID";
            cmdPosition.DisplayMember = dgvcmbPosition.DisplayMember = "Position";
            cmdPosition.DataSource = dtbl;
            dgvcmbPosition.DataSource = dtbl.Copy();
            con.Close();
        }
    }

    private void Save_Click(object sender, EventArgs e)
    {
            int EmpID = 0;
                con.Open();
                SqlCommand sqlCmd = new SqlCommand("EmployeeAddOrEdit",con );
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.AddWithValue("@EmpID", inEmpID);
                sqlCmd.Parameters.AddWithValue("@EmpCode", textBox1.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@EmpName", txtEmpName.Text.Trim());
                sqlCmd.Parameters.AddWithValue("@PositionID", Convert.ToInt32(cmdPosition.SelectedValue));
                sqlCmd.Parameters.AddWithValue("@DOB", dbdate.Value);
                sqlCmd.Parameters.AddWithValue("@Gender", cmdGender.Text);                    
                EmpID = Convert.ToInt32(sqlCmd.ExecuteScalar());   
                SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-5PJ76B9;Integrated Security=SSPI;Initial Catalog=MasterDetail;MultipleActiveResultSets=True;");
                con.Open();
                foreach (DataGridViewRow dgvRow in dataGridView1.Rows)
                {
                    if (dgvRow.IsNewRow) break;
                    else
                    {
                        SqlCommand sqlCmd = new SqlCommand("EmpCompanyAddOrEdit", con);
                        sqlCmd.CommandType = CommandType.StoredProcedure;
                        sqlCmd.Parameters.AddWithValue("@EmpCmpID", Convert.ToInt32(dgvRow.Cells["dgvtxtEmpCompID"].Value == DBNull.Value ? "0" : dgvRow.Cells["dgvtxtEmpCompID"].Value));
                        sqlCmd.Parameters.AddWithValue("@EmpID", inEmpID);
                        sqlCmd.Parameters.AddWithValue("@CompanyName", dgvRow.Cells["dgvtxtCompanyName"].Value == DBNull.Value ? "" : dgvRow.Cells["dgvtxtCompanyName"].Value);
                        sqlCmd.Parameters.AddWithValue("@PositionID", Convert.ToInt32(dgvRow.Cells["dgvcmbPosition"].Value == DBNull.Value ? "0" : dgvRow.Cells["dgvcmbPosition"].Value));
                        sqlCmd.Parameters.AddWithValue("@ExpYear", Convert.ToInt32(dgvRow.Cells["dgvtxtYear"].Value == DBNull.Value ? "0" : dgvRow.Cells["dgvtxtYear"].Value));
                        sqlCmd.ExecuteNonQuery();
                       
            }
            FillEmployeeDataGridView();
            MessageBox.Show("Submitted Successfully");
        }
    }
}

please guide

You are viewing reply posted by: pandeyism 3 months ago.