el_mstFunctionalnilai
EL_MstFunctionalNilai
Что это?Будьте осторожны с заглавными буквами.
Я уже догадывался о SqlDbType Пожалуйста, проверьте вашу базу данных, чтобы узнать фактические типы.
Код, соответствующий вашему шаблону с 2 попаданиями в базу данных.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim queryResult As Integer
'Pass the connection string directly to the constructor of the connection
Using conn2 As New SqlConnection(ConfigurationManager.ConnectionStrings("BackboneConnectionString").ConnectionString())
'Pass the command text and the connection directly to the command constructor
'You do not need as alias for Count
Using cmd As New SqlCommand("select count(*) from EL_MstFunctionalNilai where nik = @nik and idfuncmtr = @IDFuncMtr", conn2)
conn2.Open()
'This value does not appear to change in the loop so I moved it outside the loop
Dim urutan As Int32 = GetNumberFunctional()
'Add the parameters outside of the loop and only change the value inside the loop
'The value of the text box can't change inside the loop so it can be assigned outside the loop
cmd.Parameters.Add("@nick", SqlDbType.Int).Value = CInt(txtnik.Text)
cmd.Parameters.Add("@IDFuncMtr", SqlDbType.VarChar)
Using cmd2 As New SqlCommand("Insert into EL_MstFunctionalNilai values(@IDFunc, @nik, @IDFuncParent, @IDFuncChild, @IDFuncMtr, '', '', '0')", conn2)
cmd2.Parameters.Add("@IDFunc", SqlDbType.Int).Value = urutan
cmd2.Parameters.Add("@nik", SqlDbType.Int).Value = CInt(txtnik.Text)
cmd2.Parameters.Add("@IDFuncMtr", SqlDbType.VarChar) 'mtr
cmd2.Parameters.Add("@IDFuncParent", SqlDbType.VarChar) 'parent
cmd2.Parameters.Add("@IDFuncChild", SqlDbType.VarChar) 'child
For Each item As ListItem In CheckBoxList2.Items
If item.Selected Then
Dim str As String = item.Value.ToString
Dim strArr() As String = str.Split("_"c)
Dim IDFunctionalParent1 As String = strArr(0)
Dim IDFunctionalChild1 As String = strArr(1)
Dim IDFunctionalMtr1 As String = strArr(2)
cmd.Parameters("@IDFuncMtr").Value = IDFunctionalMtr1
queryResult = CInt(cmd.ExecuteScalar())
If queryResult = 0 Then
cmd2.Parameters("@IDFuncMtr").Value = IDFunctionalMtr1 'mtr
cmd2.Parameters("@IDFuncParent").Value = IDFunctionalParent1 'parent
cmd2.Parameters("@IDFuncChild").Value = IDFunctionalChild1 'child
cmd.ExecuteNonQuery()
End If
End If
Next
End Using
End Using
End Using
End Sub
Я думаю, было бы проще использовать один запрос, который проверяет наличие записи и вставляет ее, если она не существует.
Protected Sub Method2()
Dim urutan As Int32 = GetNumberFunctional()
Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("BackboneConnectionString").ConnectionString())
Using cmd As New SqlCommand("If Exists (Select 1 From EL_MstFunctionalNilai where nik = @nik and idfuncmtr = @IDFuncMtr) Select 0 Else Select 1 Insert into EL_MstFunctionalNilai values(@IDFunc, @nik, @IDFuncParent, @IDFuncChild, @IDFuncMtr, '', '', '0');", cn)
cmd.Parameters.Add("@nik", SqlDbType.Int).Value = CInt(txtnik.Text)
cmd.Parameters.Add("@IDFuncMtr", SqlDbType.VarChar)
cmd.Parameters.Add("@IDFunc", SqlDbType.VarChar).Value = urutan
cmd.Parameters.Add("@IDFuncParent", SqlDbType.VarChar)
cmd.Parameters.Add("@IDFuncChild", SqlDbType.VarChar)
cn.Open()
For Each item As ListItem In CheckBoxList2.Items
If item.Selected Then
Dim strArr() As String = item.Value.ToString.Split("_"c)
cmd.Parameters("@IDFuncMtr").Value = strArr(2)
cmd.Parameters("@IDFuncParent").Value = strArr(0)
cmd.Parameters("@IDFuncChild").Value = strArr(1)
cmd.ExecuteScalar()
End If
Next
End Using
End Using
End Sub