Click here to Skip to main content
15,900,646 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
this s one part of vb program which showing err0r
Cannot insert explicit value for identity column in table 'CUSTOMER_TEST' 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 'CUSTOMER_TEST' when IDENTITY_INSERT is set to OFF.
=====
C#
Protected Sub btnInsert_Click(sender As Object, e As System.EventArgs) Handles btnInsert.Click
        Dim Connection As String
        Dim Command As String
        Dim Cust_id As Integer
        Dim Billing_Type As String
        Dim dt As New DataTable
        Dim Cust_Name As String = itxtCustName.Text
        Dim Address As String = itxtAddress.Text
        Dim City As String = itxtCity.Text
        Dim Country As String = itxtCountry.Text
        If rb_BillingType.SelectedIndex = 0 Then
            Billing_Type = "C"
        ElseIf rb_BillingType.SelectedIndex = 1 Then
            Billing_Type = "D"
        Else
            Billing_Type = "B"
        End If
        'If String.IsNullOrEmpty(ILookup1.Text) Then
        '    Try
        '        Connection = ConfigurationManager.AppSettings("ConnectionString").ToString()
        '        Command = "select max(cust_id) as id from CUSTOMER_TEST"
        '        Dim da1 As New SqlDataAdapter(Command, Connection)
        '        da1.Fill(dt)
        '        Cust_id = "0000" & dt.Rows(0).Item("id") + 1
        '    Catch ex As Exception
        '        MsgBox(ex.Message)
        '    End Try
        'End If
        Connection = ConfigurationManager.AppSettings("ConnectionString").ToString()
        Command = "insert into dbo.CUSTOMER_TEST (CUST_ID,CUST_NAME,BILLING_TYPE,ADDRESS,CITY,COUNTRY) values ('" & Cust_id & "','" & Cust_Name & "','" & Billing_Type & "','" & Address & "','" & City & "','" & Country & "')"
        Dim da As New SqlDataAdapter(Command, Connection)
        'dt.Select(Command)
        da.Fill(dt)
    End Sub
Posted
Updated 22-Sep-15 19:50pm
v2

1 solution

since CUST_ID is identity column you can remove it from insert statement
VB
Command = "insert into dbo.CUSTOMER_TEST (CUST_NAME,BILLING_TYPE,ADDRESS,CITY,COUNTRY) values ('" & Cust_Name & "','" & Billing_Type & "','" & Address & "','" & City & "','" & Country & "')"
 
Share this answer
 
Comments
Member 12004868 23-Sep-15 1:56am    
thank you ..

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