Inserting Records in MS Access Using VB.net

gie23 1 Reputation point
2021-08-06T03:56:09.087+00:00

I am trying to Insert Data In my Ms Access Data Base from Vb.net

The Code Below I'm Using can Actually Save Data
Im using Parameterized Query
The Problem is when I save the Computed Values from a certain lines of Codes the Values of the sum will Change.

The Save Code Below:

Try
Dim sqlconn As New OleDb.OleDbConnection
Dim sqlquery As New OleDb.OleDbCommand
Dim connstring As String
connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\PlanX.mdb"
sqlconn.ConnectionString = connstring
sqlquery.Connection = sqlconn
sqlconn.Open()
sqlquery.CommandText = "INSERT INTO SchoolProfile([Grade1Female],[Grade1Male],[Grade1Total])VALUES(@Grade1Female,@Grade1Male,@Grade1Total )"

        sqlquery.Parameters.AddWithValue("@Grade1Female", Grade1FemaleTextBox.Text)
        sqlquery.Parameters.AddWithValue("@Grade1Male", Grade1MaleTextBox.Text)
        sqlquery.Parameters.AddWithValue("@Grade1Total ", Grade1TotalTextBox.Text)

        sqlquery.ExecuteNonQuery()
        sqlconn.Close()

      SchoolProfileTableAdapter.Fill(PlanXDataSet.SchoolProfile)
        SchoolProfileDataGridView.Update()
        MessageBox.Show("School Added")
        Update()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

For the Compute Code: ( inserted at the Grade1Male TextBox)

   Dim A As Double
    Dim B As Double
    Dim Result as Double

     If Not Double.TryParse(Grade1FemaleTextBox.Text, A) Then
        MessageBox.Show("INVALID")
    Else
        If Not Double.TryParse(Grade1MaleTextBox.Text, B) Then
            MessageBox.Show("INVALID")
        Else
            result1 = A + B
           Grade1TotalTextBox.Text = result1.ToString("")
        End If
    End If

Problem: After I Press the Save Button,
"INVALID" pops up from the message box
I can't seem to pinpoint the problem.
I also used toggle Breakpoint, but it wont show what is the problem.

Developer technologies | Visual Basic for Applications
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-08-06T12:53:28.603+00:00

    It is not clear why your second code is called after you press the Save button, but you can rewrite the line from the first part in this manner:

    . . .
    Dim A As Double
    Dim B As Double
    Double.TryParse(Grade1FemaleTextBox.Text, A)
    Double.TryParse(Grade1MaleTextBox.Text, B)
    sqlquery.Parameters.AddWithValue("@Grade1Total", A + B)
    . . .
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.