Generic Types

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
Hi all,
I am getting this error when trying to enter code into my database.

The specified object must not be an instance of a generic type.
Parameter name: obj

This happens when trying to ExecuteNonQuery(Line marked in red below)

i really can't figure out what has happened here as i was using diffrent code a while ago then changed it as their was a few unneeded items in their (data set, data adapter, data rows)

but i am sure this code was working a while ago, and can't figure out the changes. I have included everything that is involved with this.

Sub being called producing the error
VB.NET:
Public Sub SaveCarToDB(Car As car, Optional ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Resources\DB.accdb")
        Dim con As New OleDb.OleDbConnection
        con.ConnectionString = ConnectionString

        Dim INSERT As String = "INSERT INTO Cars (Ref_Name, RefTo, Reg, Make, Model, Colour, Price, Paid, OrderNum, KeyNumber, Customer ID, Dates, Invoice Number,  Notes) " & _
                    "VALUES (@Ref_Name, @Reg, @RefTo, @Make, @Model, @Colour, @Paid, @KeyNumber, @Price, @Customer ID, @Dates, @Invoice Number, @OrderNum, @Notes)"

        Dim myCommand As New OleDb.OleDbCommand(INSERT)
        myCommand.Connection = con

        With myCommand.Parameters
            'CarID Is The Primary Key & AutoNumber Format
            .AddWithValue("@Ref_Name", Car.RefName)
            .AddWithValue("@Reg", Car.Reg)
            .AddWithValue("@RefTo", Car.RefTo)
            .AddWithValue("@Make", Car.Make)
            .AddWithValue("@Model", Car.Model)
            .AddWithValue("@Colour", Car.Colour)
            .AddWithValue("@Paid", Car.Paid)
            .AddWithValue("@KeyNumber", Car.KeyNumber)
            .AddWithValue("@Price", Car.Price)
            .AddWithValue("@Customer ID", Car.CustomerID)
            .AddWithValue("@Dates", Car.DateDone.Date)
            .AddWithValue("@Invoice Number", Car.Invoices)
            .AddWithValue("@OrderNum", Car.OrderNum)
            .AddWithValue("@Notes", Car.Notes)
        End With


        myCommand.Connection.Open()
        [COLOR=#ff0000]myCommand.ExecuteNonQuery()[/COLOR]
        myCommand.Connection.Close()

The Car class incase it is something to do with the propertys (idk) i coloured the class to try and help make it easier to read. i hope it helps
VB.NET:
Public Class car
    'DB Details
    [COLOR=#0000ff]Private [/COLOR]_CarID   [COLOR=#0000ff]As      Integer[/COLOR] 
    [COLOR=#0000ff]Private [/COLOR]  _RefName  [COLOR=#0000ff]As      String[/COLOR] 
    [COLOR=#0000ff]Private [/COLOR]   _RefTo  [COLOR=#0000ff]As   String[/COLOR] 
    'Car Details
    [COLOR=#0000ff]Private [/COLOR] _Reg   [COLOR=#0000ff]As String[/COLOR]   
    [COLOR=#0000ff]Private [/COLOR]  _Make     [COLOR=#0000ff]As String[/COLOR]
    [COLOR=#0000ff]Private [/COLOR]_Model    [COLOR=#0000ff]As String[/COLOR]   
    [COLOR=#0000ff]Private [/COLOR]_Colour     [COLOR=#0000ff]As String[/COLOR]  
    [COLOR=#0000ff]Private [/COLOR]_Price     [COLOR=#0000ff]As Integer[/COLOR]
    [COLOR=#0000ff]Private [/COLOR] _Paid      [COLOR=#0000ff]As Boolean[/COLOR]   =     False
    [COLOR=#0000ff]Private [/COLOR]_KeyNumber    [COLOR=#0000ff]As      String[/COLOR]
    [COLOR=#0000ff]Private [/COLOR]_OrderNum   [COLOR=#0000ff]As    New [/COLOR] List([COLOR=#0000ff]Of    Strin g[/COLOR]   )
    [COLOR=#0000ff]Private [/COLOR]_Notes  [COLOR=#0000ff]As  New [/COLOR]List([COLOR=#0000ff]Of   St ring[/COLOR]  )
    [COLOR=#0000ff]Private [/COLOR]_Damagelist   [COLOR=#0000ff]As    New[/COLOR]  List ([COLOR=#0000ff]Of String[/COLOR])  
    [COLOR=#0000ff]Private [/COLOR]_DateDone   [COLOR=#0000ff]As   Date[/COLOR]
    [COLOR=#0000ff]Private [/COLOR]_Invoices  [COLOR=#0000ff]As  New[/COLOR] List([COLOR=#0000ff]Of Strin g[/COLOR])
    [COLOR=#0000ff]Private [/COLOR]_CustomerID [COLOR=#0000ff]As Byte[/COLOR]

    'DB Details
   [COLOR=#0000ff] Public Property[/COLOR] CarID [COLOR=#0000ff]As Integer[/COLOR]
       [COLOR=#0000ff] Get[/COLOR]
            [COLOR=#0000ff]Return[/COLOR] _CarID
        [COLOR=#0000ff]End Get[/COLOR]

        [COLOR=#0000ff]Set[/COLOR](value [COLOR=#0000ff]As Integer[/COLOR])
            _CarID = value
      [COLOR=#0000ff]  End Set
    End Property[/COLOR]


   [COLOR=#0000ff] Public Property[/COLOR] RefName [COLOR=#0000ff]As String[/COLOR] 'Car Reference Name For Any More Info
           [COLOR=#0000ff]Get[/COLOR] 
              [COLOR=#0000ff]Return    [/COLOR]_RefName
           [COLOR=#0000ff]End Get[/COLOR]

          [COLOR=#0000ff]Set[/COLOR]  (value  [COLOR=#0000ff]As  String[/COLOR])  
            _RefName = value
        [COLOR=#0000ff]End Set[/COLOR]
   [COLOR=#0000ff] End Property


    Public Property[/COLOR] RefTo [COLOR=#0000ff]As String[/COLOR]
       [COLOR=#0000ff]Get[/COLOR] 
            [COLOR=#0000ff]Return [/COLOR]_RefTo
       [COLOR=#0000ff]End Get[/COLOR]

               [COLOR=#0000ff]Set[/COLOR]       (value [COLOR=#0000ff]As String[/COLOR])
              _RefTo = value
       [COLOR=#0000ff]                 End             Set
                    End            Property[/COLOR]


    'Car Details
  [COLOR=#0000ff]         Public              Property[/COLOR]       Reg [COLOR=#0000ff]As Strin g[/COLOR] ' Car Reg
        [COLOR=#0000ff]Get[/COLOR]
            [COLOR=#0000ff]Return [/COLOR]_Reg
      [COLOR=#0000ff]  End Get

        Set[/COLOR](value [COLOR=#0000ff]As String[/COLOR])
            _Reg = StrConv(value, VbStrConv.Uppercase)
      [COLOR=#0000ff]  End Set
    End Property


    Public Property[/COLOR] Make [COLOR=#0000ff]As String[/COLOR] ' Car Make
       [COLOR=#0000ff] Get
            Return [/COLOR]_Make
       [COLOR=#0000ff] End Get

        Set[/COLOR](value [COLOR=#0000ff]As String[/COLOR])
            _Make = StrConv(value, VbStrConv.ProperCase)
      [COLOR=#0000ff]  End Set
    End Property

    Public Property[/COLOR] Model [COLOR=#0000ff]As String[/COLOR] ' Car Model
       [COLOR=#0000ff] Get
            Return[/COLOR] _Model
       [COLOR=#0000ff] End Get

        Set[/COLOR](value [COLOR=#0000ff]As String[/COLOR])
            _Model = StrConv(value, VbStrConv.ProperCase)
      [COLOR=#0000ff]  End Set
    End Property


    Public Property[/COLOR] Colour [COLOR=#0000ff]As String[/COLOR] ' Car Colour
       [COLOR=#0000ff] Get
            Return[/COLOR] _Colour
        [COLOR=#0000ff]End Get

        Set[/COLOR](value [COLOR=#0000ff]As String[/COLOR])
            _Colour = StrConv(value, VbStrConv.ProperCase)
       [COLOR=#0000ff] End Set
    End Property


    Public Property[/COLOR] Price [COLOR=#0000ff]As Integer[/COLOR]
        [COLOR=#0000ff]Get
            Return[/COLOR] _Price
        [COLOR=#0000ff]End Get

        Set[/COLOR](value [COLOR=#0000ff]As Integer[/COLOR])
            _Price = value
        [COLOR=#0000ff]End Set
    End Property


    Public Property[/COLOR] Paid [COLOR=#0000ff]As Boolean[/COLOR]
      [COLOR=#0000ff]  Get
            Return[/COLOR] _Paid
        [COLOR=#0000ff]End Get

        Set([/COLOR]value [COLOR=#0000ff]As Boolean)
            [/COLOR]_Paid = value
       [COLOR=#0000ff] End Set
    End Property


    Public Property[/COLOR] KeyNumber [COLOR=#0000ff]As String
        Get[/COLOR]
            [COLOR=#0000ff]Return [/COLOR]_KeyNumber
        [COLOR=#0000ff]End Get

        Set[/COLOR](value [COLOR=#0000ff]As String[/COLOR])
            _KeyNumber = StrConv(value, VbStrConv.Uppercase)
       [COLOR=#0000ff] End Set
    End Property


    Public Property [/COLOR]OrderNum As List(Of String)
       [COLOR=#0000ff] Get
            Return[/COLOR] _OrderNum
        [COLOR=#0000ff]End Get

        Set[/COLOR](value [COLOR=#0000ff]As[/COLOR] List([COLOR=#0000ff]Of String[/COLOR]))
           [COLOR=#0000ff] For Each[/COLOR] item [COLOR=#0000ff]In[/COLOR] value
                _OrderNum.Add(item)
            [COLOR=#0000ff]Next[/COLOR]
        [COLOR=#0000ff]End Set
    End Property


    Public Property[/COLOR] Notes [COLOR=#0000ff]As[/COLOR] List([COLOR=#0000ff]Of String[/COLOR])
       [COLOR=#0000ff] Get
            Return[/COLOR] _Notes
        [COLOR=#0000ff]End Get

        Set[/COLOR](value [COLOR=#0000ff]As[/COLOR] List([COLOR=#0000ff]Of String[/COLOR]))
           [COLOR=#0000ff] For Each[/COLOR] item [COLOR=#0000ff]In[/COLOR] value
                _Notes.Add(item)
            [COLOR=#0000ff]Next
        End Set
    End Property


    Public Property[/COLOR] Damagelist [COLOR=#0000ff]As[/COLOR] List([COLOR=#0000ff]Of String[/COLOR])
       [COLOR=#0000ff] Get
            Return[/COLOR] _Damagelist
        [COLOR=#0000ff]End Get

        Set[/COLOR](value [COLOR=#0000ff]As[/COLOR] List([COLOR=#0000ff]Of String[/COLOR]))
            [COLOR=#0000ff]For Each[/COLOR] item [COLOR=#0000ff]In[/COLOR] value
                _Damagelist.Add(item)
           [COLOR=#0000ff] Next
        End Set
    End Property


    Public Property[/COLOR] DateDone [COLOR=#0000ff]As Date
        Get
            Return[/COLOR] _DateDone
       [COLOR=#0000ff] End Get

        Set[/COLOR](value [COLOR=#0000ff]As Date[/COLOR])
            _DateDone = CDate(Format(value, "dd/MM/yyyy"))
        [COLOR=#0000ff]End Set
    End Property


    Public Property[/COLOR] Invoices [COLOR=#0000ff]As [/COLOR]List([COLOR=#0000ff]Of String[/COLOR])
        [COLOR=#0000ff]Get[/COLOR]
            [COLOR=#0000ff]Return [/COLOR]_Invoices
       [COLOR=#0000ff] End Get

        Set[/COLOR](value [COLOR=#0000ff]As [/COLOR]List([COLOR=#0000ff]Of String[/COLOR]))
           [COLOR=#0000ff] For Each[/COLOR] item [COLOR=#0000ff]In[/COLOR] value
                _Invoices.Add(item)
           [COLOR=#0000ff] Next
        End Set
    End Property


    Public Property[/COLOR] CustomerID [COLOR=#0000ff]As Byte
        Get
            Return[/COLOR] _CustomerID
       [COLOR=#0000ff] End Get

        Set[/COLOR](value [COLOR=#0000ff]As Byte)[/COLOR]
            _CustomerID = value
       [COLOR=#0000ff] End Set
    End Property
[/COLOR]
End Class

And here is the database i am trying to enter it into

CarIDRef_NameRefToRegMakeModelColourPricePaidOrderNumWorkDoneDatesInvoice NumberCustomer IDKeyNumber
1ULZ 8989
ULZ 8989FordFocusRed100No

21/05/2012LFC2012p14
2ULZ 1111
ULZ 1111FordFocusYellow100No

21/05/2012VWB2012W4
3ULZ 1113David MorisonULZ 1113VWGolfSilver100No

21/05/2012VWB2012P1
4ULZ 1112
ULZ 1112VWPoloRed25No

21/05/2012VWB2012W1
5ULZ 1114
ULZ 1114VWGolfWhite75No

21/05/2012VWB2012P1
6ULZ 1115
ULZ 1115toyotanissanPink85No

21/05/2012VWB2012P1
7ULZ 1119
ULZ 1119VWPoloWhite150No

21/04/2012


Cars
 
Dim INSERT As String = "INSERT INTO Cars (Ref_Name, RefTo, Reg, Make, Model, Colour, Price, Paid, OrderNum, KeyNumber, Customer ID, Dates, Invoice Number, Notes) " & _
"VALUES (@Ref_Name, @Reg, @RefTo, @Make, @Model, @Colour, @Paid, @KeyNumber, @Price, @Customer ID, @Dates, @Invoice Number, @OrderNum, @Notes)"


???????????
 
sorry, that was a peice of code i had been messing around with, was getting the error when things where in order, i tried redoing the code in another page and it still didn't work, but i did change it back and still no luck( below is how it looks now and stil getting the error)
VB.NET:
Dim INSERT As String = "INSERT INTO Cars (Ref_Name, RefTo, Reg, Make, Model, Colour, Price, Paid, OrderNum, Dates, Invoice Number, Customer ID, KeyNumber, Notes) " & _
                    "VALUES (@Ref_Name, @RefTo, @Reg, @Make, @Model, @Colour, @Price, @Paid, @OrderNum, @Dates, @Invoice Number, @Customer ID, @KeyNumber, @Notes)"

        Dim myCommand As New OleDb.OleDbCommand(INSERT)
        myCommand.Connection = con

        With myCommand.Parameters
            'CarID Is The Primary Key & AutoNumber Format
            .AddWithValue("@Ref_Name", Car.RefName)
            .AddWithValue("@RefTo", Car.RefTo)
            .AddWithValue("@Reg", Car.Reg)
            .AddWithValue("@Make", Car.Make)
            .AddWithValue("@Model", Car.Model)
            .AddWithValue("@Colour", Car.Colour)
            .AddWithValue("@Price", Car.Price)
            .AddWithValue("@Paid", Car.Paid)
            .AddWithValue("@OrderNum", Car.OrderNum)
            .AddWithValue("@Dates", Car.DateDone.Date)
            .AddWithValue("@Invoice Number", Car.Invoices)
            .AddWithValue("@Customer ID", Car.CustomerID)
            .AddWithValue("@KeyNumber", Car.KeyNumber)
            .AddWithValue("@Notes", Car.Notes)
        End With

but it does make me wonder if the OleDb provider is place specific do i need to enter the coloums that arn't in the INSERT but are in the table and apply null values to them.

i shall try it now and come back with a result, see how it goes, but i don't think it is going to work

EDIT :
Their is the possable reason, the table im using has no note coloum. so am updating the table, i have like 3 or 4 diffrent DB files lying around. i am struglying to get the databases managed since my solution contains 3 projects

Another EDIT: No, it didn't work.

my table , coloum names and values are all their and in the correct order and still getting the same error
 
Last edited:
Has any1 even have any good ideas on another way of adding the data to the table, their are 3 coloums that are relationships to another database table so, if i could take care of all the tables at once . even better lol
 
WOW, just cracked it, its was something quite simple. i was messing around witht hings and got a diffrent error.

it was because i hadn't set default values on any of my variables BUT the problem i was getting was trying to pass a List(of String) to a String value lol
 
Back
Top