I have a vb.net 2010 application that adds registries to a MS Access Patient db, but sometimes it does not add anything, until I reset the application (close and run it again) and that is very bad!
The table I want to add some information looks like this:
PatientId Proyect Email RFC
-----------------------------------
1 firstP [email protected] KEY
2 secondP [email protected] GTE
....
(Proyect is on purpose not project)
The structure of my table has an autoincrement ID value (PatientID): To know the next id I need to assign I have a function
Public Function GenerateAutoID()
Return Me._patientDataTable.Rows(Me._patientDataTable.Rows.Count - 1)("PatientID").ToString()
End Function
I am suspecting this is the bug that does not allow to sometimes insert new data to db... Then the function that executes when the save button is clicked is:
Public Sub Save()
Dim query = String.Empty
If Not _updateFlag Then
' save query
query = "INSERT INTO Patient(Proyect,Email,RFC)"
query &= " VALUES ('" & txtProyect.Text & "','" & txtEmail.Text & "','" & txtRFC.Text & "')"
DatabaseFunctions.ExecuteQuery(query)
MessageBox.Show("Data saved.", "System", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
' update query
'... code that updates
End If
Clear()
Reload()
End Sub
the codes includes Clear() that just resets all textfields in the form, Reload() and LoadDBData() :
Public Sub Reload()
SetToolTipsToButtons() 'this is for some fancy buttons
LoadDBData()
txtPatientID.Text = (Integer.Parse(GenerateAutoID()) + 1).ToString()
FillListNames()
SetAutoComplete()
End Sub
Public Sub LoadDBData()
Dim query As String = "SELECT * FROM Patient "
Me._patientDataTable = DatabaseFunctions.GetDataTable(query)
dgvInfo.Rows.Clear()
For Each dtRow In Me._patientDataTable.Rows
dgvInfo.Rows.Add()
dgvInfo.Rows(dgvInfo.RowCount - 1).Cells("colPatientID").Value = dtRow("PatientID").ToString()
dgvInfo.Rows(dgvInfo.RowCount - 1).Cells("colProyect").Value = dtRow("Proyect").ToString()
dgvInfo.Rows(dgvInfo.RowCount - 1).Cells("colEmail").Value = dtRow("Email").ToString()
dgvInfo.Rows(dgvInfo.RowCount - 1).Cells("colRFC").Value = dtRow("RFC").ToString()
Next
dgvInfo.ClearSelection()
End Sub
the class that deals with odbc connection is:
Imports System.Data.Odbc
Imports System
Public Class DatabaseFunctions
Shared con As OdbcConnection
Public Shared Sub CreateConnection()
con = New OdbcConnection
'con.ConnectionString = "Dsn=XXX"
con.ConnectionString = "Dsn=XXX;uid=sa;pwd=XXXxxx;"
con.Open()
End Sub
Private Shared Sub CheckConnection()
If con Is Nothing OrElse con.State = ConnectionState.Closed Then
CreateConnection()
End If
End Sub
Public Shared Function GetDataReader(ByVal SQL As String) As OdbcDataReader
CheckConnection()
Dim cmd As New OdbcCommand(SQL, con)
Dim dr As OdbcDataReader
dr = cmd.ExecuteReader
Return dr
End Function
Public Shared Function GetDataTable(ByVal SQL As String) As DataTable
CheckConnection()
Dim cmd As New OdbcCommand(SQL, con)
Dim table As New DataTable
Dim da As New OdbcDataAdapter(cmd)
da.Fill(table)
Return table
End Function
Public Shared Sub ExecuteQuery(ByVal SQL As String)
CheckConnection()
Dim cmd As New OdbcCommand(SQL, con)
cmd.ExecuteNonQuery()
End Sub
End Class
And the code when the form loads:
Private Sub frmPatientRegistration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Reload()
Catch ex As Exception
MessageBox.Show(ex.Message, "Systema", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
So I was thinking to change a little the save() code to know if the table has the same number of rowsm and if it has one more row everything is good, but if thats not the case try again....
I know this solution i was thinking of is not elegant, but maybe to avoid this the problem is in other part of the code... but I think it could be in the autoincrement, or something with the Access db file? Have you had any experience like this ??
I have debugged the program and it works fine, only when I run it as a standalone app sometimes it does not save anything.... until I rerun the program...