Click here to Skip to main content
15,113,991 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make a database that uses 5 textboxes where in, 4 textboxes contains numbers and 1 where it will save the sum of the 4 textboxes.

i tried some lines of codes which actually computed the sum of the 4 textboxes but im having a hard time saving them into the Database.

can anybody help me
i think my problem is because how im saving it as an integer

What I have tried:

I tried this line of code to compute the total of the textboxes
ESTOTALTextBox.Text = Val(KTTextBox.Text) + Val(G1TTextBox.Text) + Val(G2TTextBox.Text) + Val(G3TTextBox.Text) + Val(G4TTextBox.Text) + Val(G5TTextBox.Text) + Val(G6TTextBox.Text)

and Used this line of codes to save them in the database
provider = "provider = Microsoft.ACE.OLEDB.12.0;Data Source="
        datafile = Application.StartupPath & "\PLAN.accdb;"
        connstring = provider & datafile
        myConnecion.ConnectionString = connstring
        Dim str As String
        str = "Insert Into Elementary([SchoolName],[G1M],[G1F],[G1T],[G2M],[G2F],[G2T],[G3M],[G3F],[G3T],[G4M],[G4F],[G4T],[G5M],[G5F][G5T],[G6M],[G6F],[G6T],[ESTOTAL],[SY]) Values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
        Dim cmd As OleDbCommand = New OleDbCommand(str, myConnecion)

        cmd.Parameters.Add(New OleDbParameter("SchoolName", CType(SchoolNameTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G1M", CType(G1MTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G1F", CType(G1FTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G1T", CType(G1TTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G2M", CType(G2MTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G2F", CType(G2FTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G2T", CType(G2TTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G3M", CType(G3MTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G3F", CType(G3FTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G3T", CType(G3TTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G4M", CType(G4MTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G4F", CType(G4FTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G4T", CType(G4TTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G5M", CType(G5MTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G5F", CType(G5MTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G5T", CType(G5MTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G6M", CType(G6MTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G6F", CType(G6FTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("G6T", CType(G6TTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("ESTOTAL", CType(ESTOTALTextBox.Text, String)))
        cmd.Parameters.Add(New OleDbParameter("SY", CType(SYTextBox.Text, String)))


            MessageBox.Show("Data Saved!")
        Catch ex As Exception

        End Try
Updated 4-Jul-21 20:20pm

1 solution

TextBoxes ALWAYS contain strings, not numbers. You have to convert the strings in the textboxes to integers, or doubles, or whatever numeric type you're using first.

You're setting up every single one of your parameter objects to accept a string, nor an integer or other numeric type. What you use these is determined by the type you picked for each of those columns in the database.

Now, what's with all the CType(textbox.Text, String) conversions? Completely unnecessary as every Text property on every control returns a String. So, you're trying to convert a String to a String!

Since you're treating literally everything in your data as a String as far as every parameter object is concerned, and IF your database columns are setup to take numeric values and not strings, I'm not surprised you're having problems.

I've told you what you need to look at. Because there's information missing from your question, it's not possible to tell you exactly what's going on and how to fix it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900