我如何清除这个错误..帮助我 [英] How do I clear this bug ..help me

查看:57
本文介绍了我如何清除这个错误..帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是vb程序的一部分,显示err0r

当IDENTITY_INSERT设置为OFF时,无法在表'CUSTOMER_TEST'中为identity列插入显式值。



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.Data.SqlClient.SqlException:当IDENTITY_INSERT设置为OFF时,无法在表'CUSTOMER_TEST'中为identity列插入显式值。

=====

受保护的子btnInsert_Click(发件人为对象,e As System .EventArgs)处理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 字符串 = itxtCustName.Text
Dim Address As String = itxtAddress.Text
Dim City As < span class =code-sdkkeyword> String = itxtCity.Text
Dim Country As String = itxtCountry.Text
如果rb_BillingType.SelectedIndex = 0 那么
Billing_Type = C
ElseIf rb_BillingType.SelectedIndex = 1 然后
Billing_Type = D
否则
Billing_Type = B
结束如果
' If String.IsNullOrEmpty(ILookup1.Text)Then
'
尝试
' Connection = ConfigurationManager.AppSettings(ConnectionString)。ToString()
'
Command = 从CUSTOMER_TEST中选择max(cust_id)作为id
' 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 If
Connection = ConfigurationManager.AppSettings(ConnectionString)。ToString()
Command =insert into dbo.CUSTOMER_TEST(CUST_ID,CUST_NAME,BILLING_TYPE,ADDRESS,CITY,COUNTRY)值( '
& Cust_id& ' ,' & Cust_Name& ; ' ,' & Billing_Type& ' ,' &地址& ' ,' &城市与城市 ' ,' &国家和地区 '
Dim da As New SqlDataAdapter(Command,Connection)
'
dt.Select(命令)
da.Fill(dt)
End Sub

解决方案

由于CUST_ID是标识列,您可以从插入语句中删除它

 Command =  插入dbo.CUSTOMER_TEST(CUST_NAME,BILLING_TYPE,ADDRESS,CITY,COUNTRY)值('& Cust_Name&  ','& Billing_Type&  ', '&地址&  ','& City& < span class =code-string> ','& Country&  ') 


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.
=====

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

解决方案

since CUST_ID is identity column you can remove it from insert statement

Command = "insert into dbo.CUSTOMER_TEST (CUST_NAME,BILLING_TYPE,ADDRESS,CITY,COUNTRY) values ('" & Cust_Name & "','" & Billing_Type & "','" & Address & "','" & City & "','" & Country & "')"


这篇关于我如何清除这个错误..帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆