Ito yung sample save function ko, ang problema ay di nya nasasave ang new row sa datagridview, or ayaw nya mag-add ng new row at error lagi lumalabas, pa help naman mga master, school project po, TIA sa makakatulong.
Public Function SaveAccntInfo(usr As String)
Dim successful As Boolean
If gridchanged Then 'If changes made in datagridview
Try
Using con9 As New OleDbConnection(AI_Str)
con9.Open()
Using cmd9 As New OleDbCommand("SELECT * FROM [" & usr & "]", con9)
Using adapter As New OleDbDataAdapter(cmd9)
Dim dt As DataTable = DirectCast(AI_DGV.DataSource, DataTable)
Dim updCom As New OleDbCommand("UPDATE [" & usr & "] SET [LogInDate] = ?, etc. . . .")
Dim insCom As New OleDbCommand("INSERT INTO [" & usr & "] ([LogInDate], etc . . .")
Dim delCom As New OleDbCommand("DELETE FROM [" & usr & "] WHERE [LogInDate] = code", con9)
For Each rows As DataGridViewRow In AI_DGV.Rows
For Each cell As DataGridViewCell In rows.Cells
Dim dateval As DateTime
Dim doubleval As Double
If rows.IsNewRow AndAlso newRow = True Then
With insCom
'INSERT add parameters here
If rows.Cells("LogInDate") IsNot Nothing AndAlso rows.Cells("LogInDate").Value IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(rows.Cells("LogInDate").Value.ToString()) Then
If Date.TryParseExact(rows.Cells("LogInDate").Value.ToString(), formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, dateval) Then
.Parameters.Add("@[LogInDate]", OleDbType.Date).Value = dateval
Else
.Parameters.AddWithValue("@[LogInDate]", Date.Now)
End If
Else
.Parameters.AddWithValue("@[LogInDate]", Date.Now)
End If
End With
'Other Insert add parameters here
ElseIf Not rows.IsNewRow Then
With updCom
'UPDATE parameters here
If rows.Cells("LogInDate") IsNot Nothing AndAlso rows.Cells("LogInDate").Value IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(rows.Cells("LogInDate").Value.ToString()) Then
If Date.TryParseExact(rows.Cells("LogInDate").Value.ToString(), formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, dateval) Then
.Parameters.Add("@[LogInDate]", OleDbType.Date).Value = dateval
Else
.Parameters.AddWithValue("@[LogInDate]", Date.Now)
End If
Else
.Parameters.AddWithValue("@[LogInDate]", Date.Now)
End If
'Other UPDATE parameters here
End With
adapter.UpdateCommand = updCom
adapter.InsertCommand = insCom
adapter.DeleteCommand = delCom
Try
adapter.Update(dt)
dt.AcceptChanges()
successful = True
Catch ex As Exception
successful = False
MsgBox(ex.ToString)
End Try
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message)
End Try
End If
Return successful
End Function
Public Function SaveAccntInfo(usr As String)
Dim successful As Boolean
If gridchanged Then 'If changes made in datagridview
Try
Using con9 As New OleDbConnection(AI_Str)
con9.Open()
Using cmd9 As New OleDbCommand("SELECT * FROM [" & usr & "]", con9)
Using adapter As New OleDbDataAdapter(cmd9)
Dim dt As DataTable = DirectCast(AI_DGV.DataSource, DataTable)
Dim updCom As New OleDbCommand("UPDATE [" & usr & "] SET [LogInDate] = ?, etc. . . .")
Dim insCom As New OleDbCommand("INSERT INTO [" & usr & "] ([LogInDate], etc . . .")
Dim delCom As New OleDbCommand("DELETE FROM [" & usr & "] WHERE [LogInDate] = code", con9)
For Each rows As DataGridViewRow In AI_DGV.Rows
For Each cell As DataGridViewCell In rows.Cells
Dim dateval As DateTime
Dim doubleval As Double
If rows.IsNewRow AndAlso newRow = True Then
With insCom
'INSERT add parameters here
If rows.Cells("LogInDate") IsNot Nothing AndAlso rows.Cells("LogInDate").Value IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(rows.Cells("LogInDate").Value.ToString()) Then
If Date.TryParseExact(rows.Cells("LogInDate").Value.ToString(), formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, dateval) Then
.Parameters.Add("@[LogInDate]", OleDbType.Date).Value = dateval
Else
.Parameters.AddWithValue("@[LogInDate]", Date.Now)
End If
Else
.Parameters.AddWithValue("@[LogInDate]", Date.Now)
End If
End With
'Other Insert add parameters here
ElseIf Not rows.IsNewRow Then
With updCom
'UPDATE parameters here
If rows.Cells("LogInDate") IsNot Nothing AndAlso rows.Cells("LogInDate").Value IsNot Nothing AndAlso Not String.IsNullOrWhiteSpace(rows.Cells("LogInDate").Value.ToString()) Then
If Date.TryParseExact(rows.Cells("LogInDate").Value.ToString(), formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, dateval) Then
.Parameters.Add("@[LogInDate]", OleDbType.Date).Value = dateval
Else
.Parameters.AddWithValue("@[LogInDate]", Date.Now)
End If
Else
.Parameters.AddWithValue("@[LogInDate]", Date.Now)
End If
'Other UPDATE parameters here
End With
adapter.UpdateCommand = updCom
adapter.InsertCommand = insCom
adapter.DeleteCommand = delCom
Try
adapter.Update(dt)
dt.AcceptChanges()
successful = True
Catch ex As Exception
successful = False
MsgBox(ex.ToString)
End Try
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show("An error occurred: " & ex.Message)
End Try
End If
Return successful
End Function