วิธีแก้ไข code vb error close:open database ของผม Error ไม่รู้จะเเก้ไขอย่างไร
Code (VB.NET)
Private Sub Add_Data()
Dim sqlSearch As String
Dim sqlString As String
Dim nResult As Integer
nResult = 0
'Try
cnn.Open()
command = New SqlCommand("Select GetDate()", cnn)
dtServerDateTime2 = command.ExecuteScalar
cnn.Close()
cnn.Open()
sqlSearch = "Select *from vewTotal_Result_Qty_LineName Where Production_Order='" & cobOrder.Text & "'"
command = New SqlCommand(sqlSearch, cnn)
Dim sqlReader As SqlDataReader = command.ExecuteReader()
If sqlReader.Read = True Then
nResult = CInt(sqlReader("Sum_Qty").ToString) + 1
cnn.Close()
cnn.Open()
sqlString = "Update Result_Quantity SET Result_Qty=" & nResult & " Where Production_Order='" & cobOrder.Text & "'"
command_Update = New SqlCommand(sqlString, cnn)
command_Update.ExecuteNonQuery()
cnn.Close()
Else
cnn.Close()
cnn.Open()
nResult = 1
sqlString = "INSERT INTO Result_Quantity (Line_Name,Production_Order,Item_Code,Item_Desc, " & _
" Result_Date,Result_Qty,Flag_Status,IP_Address) " & _
" Values('" & Me.cobLineName.Text & "','" & Me.cobOrder.Text & "','" & Me.lblItemCode.Text & "','" & Me.lblItemDesc.Text & "'," & _
"'" + Format(dtServerDateTime2, "yyyy-MM-dd H:mm:ss") + "'," & nResult & ",'" & "1" & "','" & Me.txtIP_Address.Text & "') "
command_Insert = New SqlCommand(sqlString, cnn)
command_Insert.ExecuteNonQuery()
cnn.Close()
End If
lblQty.Text = nResult
Me.Refresh()
cnn.Open()
sqlSearch = "Select *from Expected_Datetime Where Production_Order='" & cobOrder.Text & "'"
command = New SqlCommand(sqlSearch, cnn)
Dim sqlReader2 As SqlDataReader = command.ExecuteReader()
If sqlReader2.Read = True Then
If CInt(sqlReader2("Plan_Qty").ToString) = CInt(lblQty.Text) Then
Call Save_Result()
Me.btAdd.Enabled = False
Me.btRemove.Enabled = False
'Me.btStop.Enabled = False
Me.btStart.Enabled = False
'cnn.Close()
'cnn.Open()
Else
'cnn.Close()
'cnn.Open()
If CInt(lblQty.Text) + 1 > CInt(sqlReader2("Plan_Qty").ToString) Then
MsgBox("Not Input Result Over Plan Qty.", vbCritical + vbOKOnly)
'lblQty.Text = i - 1
Me.btAdd.Enabled = False
cnn.Close()
End If
cnn.Close()
End If
cnn.Close()
End If
cnn.Close()
End Sub
ส่วนที่บันทึกข้อความ
Code (VB.NET)
Private Sub Save_Result()
Dim sqlInsert As String
Dim sqlUpdate As String
'---------------------
'Naowarat 10/01/2018
'cnn.Open()
command = New SqlCommand("Select GetDate()", cnn)
dtServerDateTime2 = command.ExecuteScalar
cnn.Close()
If Me.lblItemCode.Text <> "" Then
If MsgBox("Do you want to Finish Production Order : " & cobOrder.Text & "", vbInformation + vbOKCancel) = vbOK Then
cnn.Open()
sql = "SELECT *FROM Close_Production_Order Where Production_Order='" & Me.cobOrder.Text & "'"
sResult_All = sResult_All
command = New SqlCommand(sql, cnn)
Dim sqlReader As SqlDataReader = command.ExecuteReader()
If Not sqlReader.HasRows Then
Me.Timer1.Enabled = False
token = 1
cnn.Close()
cnn.Open()
sqlUpdate = ("Update Result_Time SET Stop_Time='" + Format(dtServerDateTime2, "HH:mm") + "',Stop_Date='" + Format(dtServerDateTime2, "yyyy/MM/dd") + "'," & _
"Stamp_Stop='" + Format(dtServerDateTime2, "yyyy-MM-dd H:mm:ss") + "',IP_Address_Stop='" & Me.txtIP_Address.Text & "' " & _
" Where Production_Order ='" & Me.cobOrder.Text & "' And (Flag_Status='" & "0" & "') ")
command = New SqlCommand(sqlUpdate, cnn)
command.ExecuteNonQuery()
cnn.Close()
cnn.Open()
sqlUpdate = ("Update Result_Time SET Flag_Status='" & "2" & "' " & _
" Where Production_Order ='" & Me.cobOrder.Text & "' ")
command = New SqlCommand(sqlUpdate, cnn)
command.ExecuteNonQuery()
cnn.Close()
cnn.Open()
sqlUpdate = ("Update Result_Quantity SET Flag_Status='" & "2" & "' Where Production_Order ='" & Me.cobOrder.Text & "' ")
command = New SqlCommand(sqlUpdate, cnn)
command.ExecuteNonQuery()
cnn.Close()
cnn.Open()
sqlUpdate = ("Update Expected_Datetime SET Flag_Status='" & "2" & "' Where Production_Order ='" & Me.cobOrder.Text & "' ")
command = New SqlCommand(sqlUpdate, cnn)
command.ExecuteNonQuery()
cnn.Close()
cnn.Open()
sqlInsert = "Insert Into Close_Production_Order(Production_Order,Item_Code,Item_Desc,Result_Qty,Close_DateTime,IP_Address_Close) " & _
" Values('" & Me.cobOrder.Text & "','" & Me.lblItemCode.Text & "','" & Me.lblItemDesc.Text & "'," & Me.lblQty.Text & "," & _
"'" + Format(dtServerDateTime2, "yyyy-MM-dd H:mm:ss") + "','" & Me.txtIP_Address.Text & "')"
command = New SqlCommand(sqlInsert, cnn)
command.ExecuteNonQuery()
MsgBox("Close Production Order Complate.", vbInformation + vbOKOnly)
iClose = 1
cnn.Close()
'------------------
'Naowarat 10/01/2018
'Me.cobLineName.Text = ""
'------------------
Me.cobOrder.Text = ""
Me.cobOperator.Text = ""
Me.lblItemCode.Text = ""
Me.lblItemDesc.Text = ""
Me.lblQty.Text = "0"
Me.Timer1.Enabled = False
Me.btStart.Enabled = False
Me.btStop.Enabled = False
Me.btAdd.Enabled = False
Me.btRemove.Enabled = False
Me.lbHour.Text = "00"
Me.lbMinute.Text = "00"
Me.lbSecond.Text = "00"
Me.cobLineName.Enabled = True
Me.cobOperator.Enabled = True
Me.cobOrder.Enabled = True
Me.btCloseOrder.Enabled = False
'------------------
'Naowarat 10/01/2018
Me.cobOperator.Focus()
'Me.cobLineName.Focus()
'--------------------
sProduction_Order = ""
sResult_All = 0
Else
iClose = 0
sResult_All = sResult_All
cnn.Close()
End If
Else
iClose = 0
cnn.Close()
End If
Else
iClose = 0
cnn.Close()
End If
End Sub
Tag : .NET, VBScript, VB.NET, Windows
Date :
2018-07-20 15:55:13
By :
prayun
View :
1359
Reply :
2
ใช้ DataReader แล้ว จะไม่สามารถเปิด Command อื่นได้อีกครับ ให้ใช้ DataTable แทนครับ
Date :
2018-07-20 16:13:58
By :
mr.win
ควรประกาศตัวแปรใน sub ครับ
ในที่นี้รวมถึง connection/command/adepter ด้วยครับ
ควรใช้แล้วทิ้งไม่ควรให้ใครใช้ร่วมไม่งั้น
ก็สร้าว ฟังค์ชันขึ้นมา เพื่อลด โค้ดให้สั้นลงครับ
Code (VB.NET)
Public Sub CreateCommand(ByVal queryString As String, _
ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
Dim command As New SqlCommand(queryString, connection)
command.Connection.Open()
command.ExecuteNonQuery()
End Using
End Sub
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
แบบนี้ครับ
ใช้แล้วก็ปิด คนอื่นก็มาใช้ต่อได้ เช่น
Code (VB.NET)
sqlUpdate = ("Update Result_Time SET Stop_Time='" + Format(dtServerDateTime2, "HH:mm") + "',Stop_Date='" + Format(dtServerDateTime2, "yyyy/MM/dd") + "'," & _
"Stamp_Stop='" + Format(dtServerDateTime2, "yyyy-MM-dd H:mm:ss") + "',IP_Address_Stop='" & Me.txtIP_Address.Text & "' " & _
" Where Production_Order ='" & Me.cobOrder.Text & "' And (Flag_Status='" & "0" & "') ")
CreateCommand(sqlUpdate ,connectionString )
sqlUpdate = ("Update Result_Time SET Flag_Status='" & "2" & "' " & _
" Where Production_Order ='" & Me.cobOrder.Text & "' ")
CreateCommand(sqlUpdate ,connectionString )
Date :
2018-07-20 23:54:31
By :
lamaka.tor
Load balance : Server 04