1
votes

I'm trying to a create a windows form application in vb.net using a access database (2010) with 2 tables (as the main idea, I'm trying to create a bank creditation application). One table I use it for login, and another one to insert elements into it from a windows form.

For the login table, everything works fine, but when it comes for the second table (clients) I encounter some problems.

The table details for the clients is: ID (autonumber), first name (text), last name (text), PID (number), adress (text), city (text), marital status (text), nochildren (number), date (date/time).

I've created an insert query for the clients table that goes like this:

INSERT INTO `Clienti` (`nume`, `prenume`, `cnp`, `adresa`, `localitate`, `stare civila`) VALUES (?, ?, ?, ?, ?, ?, ?, ?).

In the Windows form I've created I have the following elements: first name (textbox), last name (textbox), PID (textbox), adress (text), city (text), marital status (text) and a button that should save those into my clients table.

The code for the save to DB button is:

Convert.ToInt32(cnptxt.Text)
Convert.ToInt32(numarcopiitxt.Text)

Dim cnpp As Integer
cnpp = Val(cnptxt.Text)
Dim nrcopii As Integer
nrcopii = Val(numarcopiitxt.Text)

Dim nume As String

Dim prenume As String
Dim cnp As Integer
Dim varsta As Integer
Dim adresa As String
Dim localitate As String
Dim starecivila As String
Dim numarcopii As Integer

nume = numetxt.Text
prenume = prenumetxt.Text
cnp = cnpp
varsta = varstatxt.Text
adresa = adresatxt.Text
localitate = localitatetxt.Text
starecivila = starecivilatxt.Text
numarcopii = numarcopiitxt.Text
If Me.ClientiTableAdapter.InsertQueryClienti(nume, prenume, cnp, varsta, adresa, localitate, starecivilatxt.Text) Then
    MsgBox("Client adaugat cu succes in baza de date")
Else
    MsgBox("O eroare a fost intalnita intr-unul din campurile completate. Reincercati!")
End If

I know it's incomplete when compared with the rows from the clients table, it's due to the problems I've encountered with the save to DB. When I run the application and I tap on the save to DB button, I receive the following error:

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "necasatorit" to type 'Integer' is not valid.

The code for InsertQueryClienti is INSERT INTO Clienti (nume, prenume, cnp, adresa, localitate, stare civila) VALUES (?, ?, ?, ?, ?, ?, ?, ?) and the definition for it is Public Overloads Overridable Function InsertQueryClienti(ByVal nume As String, ByVal prenume As String, ByVal cnp As Global.System.Nullable(Of Integer), ByVal adresa As String, ByVal localitate As String, ByVal stare_civila As String, ByVal numarcopii As Global.System.Nullable(Of Integer)) As Integer

necasatorit is referring to the marital status textbox. I can't understand why it gives me that error since the textbox obviously is a string and in the table that row is set as text.

1
At which line does the error occur? You are trying to set a locale Integer Variable to String. It has nothing to do with your DB Definition. - dummy
The error occurs at line: If Me.ClientiTableAdapter.InsertQueryClienti(nume, prenume, cnp, varsta, adresa, localitate, starecivilatxt.Text) - lewilddude
So it is not a local variable, but a paramter for InsertQueryClienti() where an Integer is excpected, but a String is given. Where does InsertQueryClienti() come from, did you write it yourself or is it auto generated somehow? - dummy
The InsertQueryClienti is created by me through the DB connection used in the application. For a better understanding how i've created it, i've used the exameple from youtube.com/watch?v=HVkxgpeqSeY min 5:30 to 6:40. - lewilddude
Please add the code for InsertQueryClienti() to the question. - dummy

1 Answers

1
votes

Your definition of InsertQueryClienti() is as follows:

Public Overloads Overridable Function InsertQueryClienti(
    ByVal nume As String, 
    ByVal prenume As String, 
    ByVal cnp As Global.System.Nullable(Of Integer), 
    ByVal adresa As String, 
    ByVal localitate As String, 
    ByVal stare_civila As String, 
    ByVal numarcopii As Global.System.Nullable(Of Integer)) As Integer

But you are calling it this way:

InsertQueryClienti(nume, prenume, cnp, varsta, adresa, localitate, starecivilatxt.Text)

The first three parameter nume, prenume, cnp are identical, but after that you use varsta. This shifts everything to the right. starecivilatxt.Text is passed as the last parameter which is of type Integer.

This way the parameter are in the same order in the definition and the call.

InsertQueryClienti(nume, prenume, cnp, adresa, localitate, starecivilatxt.Text, ???)

I don't know what the last parameter should be. Maybe varsta?