[.NET] 能幫我看一下VB.NET的問題嗎??無法更新
修改之後更新會出現問題~
新增之後更心是可以的~
想問一下板上的前輩那邊出了問題
兩個資料表的表單可以正確新增跟修改
一個資料表的表單可以正確更新~但是修改之後會出問題
以下是我的程式碼
Private Sub btn_update_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btn_update.Click
Dim vbAns As Microsoft.VisualBasic.MsgBoxResult
Dim vbDS As postgres_odbc.dsPersonnel
Dim vbResult As Boolean
Dim vbOpState As OpState
vbOpState = Me.JudgeModified
vbAns = MsgBoxResult.No
Select Case vbOpState
Case Module1.OpState.InsertState
If (Me.txt_p_id.Text = "NaN") Then
MsgBox("未輸入身分證字號 !!!", MsgBoxStyle.Critical)
Me.txt_p_id.Focus()
Return
End If
If (Me.txt_p_name.Text = "NaN") Then
MsgBox("未輸入姓名 !!!", MsgBoxStyle.Critical)
Me.txt_p_name.Focus()
Return
End If
vbAns = MsgBox("確定新增紀錄 ???", MsgBoxStyle.YesNo)
vbResult =
UpdateDatabasetabasetabase(Module1.OpState.InsertState, "新增紀錄中 !!!")
Case Module1.OpState.UpdateState
vbAns = MsgBox("確定更新記錄 ???", MsgBoxStyle.YesNo)
vbResult =
UpdateDatabasetabasetabase(Module1.OpState.UpdateState, "更新紀錄中 !!!")
End Select
If (vbResult = False) Then
Select Case vbOpState
Case Module1.OpState.InsertState
MsgBox("無法新增紀錄 !!!", MsgBoxStyle.Critical, "帳號["
& LOGIN_ACCOUNT & "]")
Me.BindingContext(Me.DsPersonelEdit,
vbTableName).Position = vbLoc
Case Module1.OpState.UpdateState
MsgBox("無法更新紀錄 !!!", MsgBoxStyle.Critical, "帳號["
& LOGIN_ACCOUNT & "]")
End Select
Me.DsPersonelEdit.RejectChanges()
Me.BindingContext(Me.DsPersonelEdit,
vbTableName).CancelCurrentEdit()
Else
Select Case vbOpState
Case Module1.OpState.InsertState
vbRecordCount = vbRecordCount + 1
vbRecordIndex = vbRecordCount - 1
Me.ShowStatusBar("新增紀錄成功 !!!")
Me.cb_Add_Order.Items.Add(Me.txt_p_id.Text)
Case Module1.OpState.UpdateState
Me.ShowStatusBar("更新紀錄成功 !!!")
End Select
Me.DsPersonelEdit.AcceptChanges()
End If
End Sub
Private Function UpdateDatabasetabasetabase(ByVal vbOpState As OpState, ByVal
vbMessage As String) As Boolean
Dim vbTransaction As System.Data.Odbc.OdbcTransaction
Dim vbDS As postgres_odbc.dsPersonnel
Dim vbDSDeleted As postgres_odbc.dsPersonnel
Me.ShowStatusBar(vbMessage)
Select Case vbOpState
Case Module1.OpState.InsertState
Case Module1.OpState.DeleteState
'複製記錄集(Dataset)為暫存記錄集 vbDSdeleted
vbDSDeleted = New postgres_odbc.dsPersonnel
vbDSDeleted = Me.DsPersonelEdit.Copy
End Select
Me.BindingContext(Me.DsPersonelEdit, vbTableName).EndCurrentEdit()
Select Case vbOpState
Case Module1.OpState.DeleteState
'刪除暫存記錄集 vbDSdeleted記錄集
vbDSDeleted.tbl_personnel.Rows(Me.BindingContext(Me.DsPersonelEdit,
vbTableName).Position).Delete()
vbDS = CType(vbDSDeleted.GetChanges,
postgres_odbc.dsPersonnel)
Case Else
vbDS = CType(Me.DsPersonelEdit.GetChanges,
postgres_odbc.dsPersonnel)
End Select
Try
Me.OdbcConnection1.Open()
Catch ex As Exception
MsgBox("Connection Error:" & ex.Message)
Return False
End Try
vbTransaction = Me.OdbcConnection1.BeginTransaction()
Me.OdbcDataAdapter1.UpdateCommand.Transaction = vbTransaction
Me.OdbcDataAdapter1.DeleteCommand.Transaction = vbTransaction
Me.OdbcDataAdapter1.SelectCommand.Transaction = vbTransaction
Me.OdbcDataAdapter1.InsertCommand.Transaction = vbTransaction
Try
Select Case vbOpState
Case Module1.OpState.UpdateState
Me.OdbcDataAdapter1.Update(vbDS, vbTableName)
Case Module1.OpState.DeleteState
Me.OdbcDataAdapter1.Update(vbDS, vbTableName)
Case Module1.OpState.InsertState
Me.OdbcDataAdapter1.Update(vbDS, vbTableName)
End Select
vbTransaction.Commit()
'交易成功傳回True
Return True
Catch ex As Exception
vbTransaction.Rollback()
'交易失敗傳回False
Return False
Finally
Me.OdbcConnection1.Close()
End Try
End FunctionPrivate Function UpdateDatabasetabasetabase(ByVal vbOpState As OpState, ByVal
vbMessage As String) As Boolean
Dim vbTransaction As System.Data.Odbc.OdbcTransaction
Dim vbDS As postgres_odbc.dsPersonnel
Dim vbDSDeleted As postgres_odbc.dsPersonnel
Me.ShowStatusBar(vbMessage)
Select Case vbOpState
Case Module1.OpState.InsertState
Case Module1.OpState.DeleteState
'複製記錄集(Dataset)為暫存記錄集 vbDSdeleted
vbDSDeleted = New postgres_odbc.dsPersonnel
vbDSDeleted = Me.DsPersonelEdit.Copy
End Select
Me.BindingContext(Me.DsPersonelEdit, vbTableName).EndCurrentEdit()
Select Case vbOpState
Case Module1.OpState.DeleteState
'刪除暫存記錄集 vbDSdeleted記錄集
vbDSDeleted.tbl_personnel.Rows(Me.BindingContext(Me.DsPersonelEdit,
vbTableName).Position).Delete()
vbDS = CType(vbDSDeleted.GetChanges,
postgres_odbc.dsPersonnel)
Case Else
vbDS = CType(Me.DsPersonelEdit.GetChanges,
postgres_odbc.dsPersonnel)
End Select
Try
Me.OdbcConnection1.Open()
Catch ex As Exception
MsgBox("Connection Error:" & ex.Message)
Return False
End Try
vbTransaction = Me.OdbcConnection1.BeginTransaction()
Me.OdbcDataAdapter1.UpdateCommand.Transaction = vbTransaction
Me.OdbcDataAdapter1.DeleteCommand.Transaction = vbTransaction
Me.OdbcDataAdapter1.SelectCommand.Transaction = vbTransaction
Me.OdbcDataAdapter1.InsertCommand.Transaction = vbTransaction
Try
Select Case vbOpState
Case Module1.OpState.UpdateState
Me.OdbcDataAdapter1.Update(vbDS, vbTableName)
Case Module1.OpState.DeleteState
Me.OdbcDataAdapter1.Update(vbDS, vbTableName)
Case Module1.OpState.InsertState
Me.OdbcDataAdapter1.Update(vbDS, vbTableName)
End Select
vbTransaction.Commit()
'交易成功傳回True
Return True
Catch ex As Exception
vbTransaction.Rollback()
'交易失敗傳回False
Return False
Finally
Me.OdbcConnection1.Close()
End Try
End Function
--
我的馬子相簿喔...
http://www.wretch.cc/album/homb
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.170.54.13
→
220.139.151.70 08/10, , 1F
220.139.151.70 08/10, 1F
→
218.170.54.13 08/10, , 2F
218.170.54.13 08/10, 2F
推
210.58.156.43 08/10, , 3F
210.58.156.43 08/10, 3F
→
210.58.156.43 08/10, , 4F
210.58.156.43 08/10, 4F
推
218.170.67.166 08/11, , 5F
218.170.67.166 08/11, 5F
Visual_Basic 近期熱門文章
PTT數位生活區 即時熱門文章