evolet10000
Active member
- Joined
- Nov 22, 2012
- Messages
- 40
- Programming Experience
- Beginner
[SUB]im trying to make a query that insert all listview datas into database with background worker
here the problem
i get a cross thread error @ listviewRIS_ITEMS.Items, i know that it can be bypass using delegates? but is there another way? i mean like? can i store "listviewRIS_ITEMS" in a variable?
so that my full code will execute smoothly? just need some advice experts
heres the full code.
[/SUB]
here the problem
VB.NET:
[/SUB]
For Each SAIitem As ListViewItem In ListViewRIS_ITEMS.Items
cmd = New SqlClient.SqlCommand("INSERT INTO SAI_ITEMS(SAI_NO, STOCK_NO, STOCK_CATEGORY, STOCK_DESCRIPTION, STOCK_UNIT, QUANTITY, STOCK_BALANCE_QTY) VALUES('" & SAI_NO_RESULT & "', '" & SAIitem.SubItems(0).Text & _
"', '" & SAIitem.SubItems(1).Text & "', '" & SAIitem.SubItems(2).Text & "', '" & SAIitem.SubItems(3).Text & "', '" & SAIitem.SubItems(4).Text & "', '" & SAIitem.SubItems(5).Text & "')", con)
cmd.Connection = con
cmd.ExecuteNonQuery()
Next[SUB]
i get a cross thread error @ listviewRIS_ITEMS.Items, i know that it can be bypass using delegates? but is there another way? i mean like? can i store "listviewRIS_ITEMS" in a variable?
so that my full code will execute smoothly? just need some advice experts
heres the full code.
VB.NET:
[/SUB]
Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
BW_ADDRIS_SAI.RunWorkerAsync()
frmLoading.ShowDialog()
End Sub
Private Sub BW_ADDRIS_SAI_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BW_ADDRIS_SAI.DoWork
Try
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
'SAI QUERY
Dim SAI As String = "SAI"
Dim SAIYEARNOW = Year(Now)
Dim SAIMONTHNOW = Month(Now)
Dim SAICOUNT As Integer = SAIgetcount()
Dim SAIDATENOW As Date = Now()
Dim SAI_NO_RESULT As String
SAI_NO_RESULT = GENERATE_SAI_NO(SAI, SAIYEARNOW, SAIMONTHNOW, SAICOUNT)
'RIS QUERY
Dim RIS As String = "RIS"
Dim RISYEARNOW = Year(Now)
Dim RISMONTHNOW = Month(Now)
Dim RISCOUNT As Integer = RISgetcount()
Dim RISDATENOW As Date = Now()
Dim RIS_NO_RESULT As String
RIS_NO_RESULT = GENERATE_RIS_NO(RIS, RISYEARNOW, RISMONTHNOW, RISCOUNT)
'==============(START)INSERT INTO SAI=============='
cmd = New SqlClient.SqlCommand("INSERT INTO SAI(SAI_NO, OFFICE, DIVISION, PURPOSE, INQUIRED_BY_EMP_NO, INQUIRED_DATE) VALUES('" & SAI_NO_RESULT & "', '" & OFFICE _
& "', '" & DIVISION & "', '" & TextBoxUSER_ADDRIS_PURPOSE.Text & "', '" & EMP_NO & "', '" & SAIDATENOW & "')", con)
cmd.Connection = con
cmd.ExecuteNonQuery()
'==============(END)INSERT INTO SAI=============='
'==============(START)INSERT INTO SAI ITEMS=============='
For Each SAIitem As ListViewItem In ListViewRIS_ITEMS.Items
cmd = New SqlClient.SqlCommand("INSERT INTO SAI_ITEMS(SAI_NO, STOCK_NO, STOCK_CATEGORY, STOCK_DESCRIPTION, STOCK_UNIT, QUANTITY, STOCK_BALANCE_QTY) VALUES('" & SAI_NO_RESULT & "', '" & SAIitem.SubItems(0).Text & _
"', '" & SAIitem.SubItems(1).Text & "', '" & SAIitem.SubItems(2).Text & "', '" & SAIitem.SubItems(3).Text & "', '" & SAIitem.SubItems(4).Text & "', '" & SAIitem.SubItems(5).Text & "')", con)
cmd.Connection = con
cmd.ExecuteNonQuery()
Next
'==============(END)INSERT INTO SAI ITEMS=============='
'==============(START)INSERT INTO RIS=============='
cmd = New SqlClient.SqlCommand("INSERT INTO RIS(RIS_NO, RIS_DATE, OFFICE, DIVISION, SAI_NO, SAI_DATE, PURPOSE, REQUISITIONER_EMP_NO, REQUESTED_DATE, OFFICE_HEAD_EMP_NO) VALUES('" & RIS_NO_RESULT & "', '" & RISDATENOW & _
"', '" & OFFICE & "', '" & DIVISION & "', '" & SAI_NO_RESULT & "', '" & SAIDATENOW & "', '" & TextBoxUSER_ADDRIS_PURPOSE.Text & "', '" & EMP_NO & "', '" & RISDATENOW & "', '" & TextBoxADD_RIS_APR_EMP.Text & "')", con)
cmd.Connection = con
cmd.ExecuteNonQuery()
'==============(END)INSERT INTO RIS=============='
'==============(START)INSERT INTO RIS ITEMS=============='
For Each RISitem As ListViewItem In ListViewRIS_ITEMS.Items
cmd = New SqlClient.SqlCommand("INSERT INTO RIS_ITEMS(RIS_NO, SAI_NO, STOCK_NO, STOCK_CATEGORY, STOCK_DESCRIPTION, STOCK_UNIT, QUANTITY) VALUES('" & RIS_NO_RESULT & "', '" & SAI_NO_RESULT & "', '" & RISitem.SubItems(0).Text & _
"', '" & RISitem.SubItems(1).Text & "', '" & RISitem.SubItems(2).Text & "', '" & RISitem.SubItems(3).Text & "', '" & RISitem.SubItems(4).Text & "')", con)
cmd.Connection = con
cmd.ExecuteNonQuery()
Next
'==============(END)INSERT INTO RIS ITEMS=============='
'==================TEMPORARY BALANCE===================='
'For Each RISitemB_QTY As ListViewItem In ListViewRIS_ITEMS.Items
'Dim temp_bal As Integer = TextBoxBALANCE_QTY.Text
'Dim req_qty As Integer = RISitemB_QTY.SubItems(4).Text
'Dim new_temp_bal As Integer = temp_bal - req_qty
'cmd = New SqlClient.SqlCommand("UPDATE STOCK SET STOCK_TEMP_BALANCE_QTY = '" & new_temp_bal & "' WHERE STOCK_NO = '" & RISitemB_QTY.SubItems(0).Text & "'", con)
'cmd.Connection = con
'cmd.ExecuteNonQuery()
'Next
'==================END TEMPORARY BALANCE===================='
con.Close()
myFlag = True
RISNORESULT = RIS_NO_RESULT
SAINORESULT = SAI_NO_RESULT
Catch ex As Exception
myBWFlag = True
myErr = ex.Message
End Try
End Sub
Private Sub BW_ADDRIS_SAI_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BW_ADDRIS_SAI.RunWorkerCompleted
frmLoading.Close()
If myFlag = True Then
frmDialog.LabelDialogDotText.Text = "MESSAGE: ADD RIS AND SAI"
frmDialog.LabelDialogDotText.ForeColor = Color.Red
frmDialog.TextBox1.Text = "Successful"
frmDialog.ShowDialog()
frmUSER_FRAME.SHOW_USER_SAI()
MsgBox("RIS AND SAI HAS BEEN ADDED")
TabControlMainContent.SelectedIndex = 1
ElseIf myBWFlag = True Then
frmDialog.LabelDialogDotText.Text = "WARNING: Network Error"
frmDialog.LabelDialogDotText.ForeColor = Color.Red
frmDialog.TextBox1.Text = myErr
frmDialog.ShowDialog()
End If
End Sub Public Function GENERATE_SAI_NO(ByVal SAI As String, ByVal SAIYEARNOW As String, ByVal SAIMONTHNOW As String, ByVal SAICOUNT As Integer) As String
Dim SAI_NO As String
SAI_NO = SAI & "-" & SAIYEARNOW & "-" & "0" & SAIMONTHNOW & "-" & "0" & SAICOUNT
Return SAI_NO
End Function
Public Function SAIgetcount() As Integer
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
cmd = New SqlClient.SqlCommand("SELECT COUNT(*) FROM SAI", con)
Return cmd.ExecuteScalar
con.Close()
End Function
Public Function GENERATE_RIS_NO(ByVal RIS As String, ByVal RISYEARNOW As String, ByVal RISMONTHNOW As String, ByVal RISCOUNT As Integer) As String
Dim RIS_NO As String
RIS_NO = RIS & "-" & RISYEARNOW & "-" & "0" & RISMONTHNOW & "-" & "0" & RISCOUNT
Return RIS_NO
End Function
Public Function RISgetcount() As Integer
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
cmd = New SqlClient.SqlCommand("SELECT COUNT(*) FROM RIS", con)
Return cmd.ExecuteScalar
con.Close()
End Function[SUB]