Question

When I finished inputting all the data in the datagrid columns upon clicking the save button this error returns:

ERROR: An unhandled exception of type 'System.InvalidCastException' occurred in system.data.dll

Additional information: Object must implement IConvertible.

 Dim sqlsyntax As String
    sqlsyntax = "INSERT INTO tblOfficeEquipmentProfile(OE_ID, OE_Category,OE_SubCategory, OE_Name, OE_User, OE_Brand, OE_Model, OE_Specs, OE_SerialNo, OE_PropertyNo, OE_Static_IP, OE_Vendor, OE_PurchaseDate, OE_WarrantyInclusiveYear, OE_WarrantyStatus, OE_Status, OE_Dept_Code,OE_Location_Code,OE_Remarks)" _
             & "VALUES(@OE_ID,@OE_Category,@OE_SubCategory,@OE_Name,@OE_User,@OE_Brand,@OE_Model,@OE_Specs,@OE_SerialNo,@OE_PropertyNo,@OE_Static_IP,@OE_Vendor,@OE_PurchaseDate,@OE_WarrantyInclusiveYear,@OE_WarrantyStatus,@OE_Status,@OE_Dept_Code,@OE_Location_Code,@OE_Remarks)"

    Dim adapter As New SqlDataAdapter
    adapter.InsertCommand = New SqlCommand(sqlsyntax, sqlconn)

    adapter.InsertCommand.Parameters.Add("@OE_ID", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_Category", SqlDbType.Char)
    adapter.InsertCommand.Parameters.Add("@OE_SubCategory", SqlDbType.Char)
    adapter.InsertCommand.Parameters.Add("@OE_Name", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_User", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_Brand", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_Model", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_Specs", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_SerialNo", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_PropertyNo", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_Static_IP", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_Vendor", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_PurchaseDate", SqlDbType.SmallDateTime)
    adapter.InsertCommand.Parameters.Add("@OE_WarrantyInclusiveYear", SqlDbType.Int)
    adapter.InsertCommand.Parameters.Add("@OE_WarrantyStatus", SqlDbType.Char)
    adapter.InsertCommand.Parameters.Add("@OE_Status", SqlDbType.VarChar)
    adapter.InsertCommand.Parameters.Add("@OE_Dept_Code", SqlDbType.Char)
    adapter.InsertCommand.Parameters.Add("@OE_Location_Code", SqlDbType.Char)
    adapter.InsertCommand.Parameters.Add("@OE_Remarks", SqlDbType.VarChar)


    For i As Integer = 0 To DataGrid1.VisibleRowCount - 1

        adapter.InsertCommand.Parameters(0).Value = DataGrid1(i, 0).GetType
        adapter.InsertCommand.Parameters(1).Value = DataGrid1(i, 1).GetType
        adapter.InsertCommand.Parameters(2).Value = DataGrid1(i, 2).GetType
        adapter.InsertCommand.Parameters(3).Value = DataGrid1(i, 3).GetType
        adapter.InsertCommand.Parameters(4).Value = DataGrid1(i, 4).GetType
        adapter.InsertCommand.Parameters(5).Value = DataGrid1(i, 5).GetType
        adapter.InsertCommand.Parameters(6).Value = DataGrid1(i, 6).GetType
        adapter.InsertCommand.Parameters(7).Value = DataGrid1(i, 7).GetType
        adapter.InsertCommand.Parameters(8).Value = DataGrid1(i, 8).GetType
        adapter.InsertCommand.Parameters(9).Value = DataGrid1(i, 9).GetType
        adapter.InsertCommand.Parameters(10).Value = DataGrid1(i, 10).GetType
        adapter.InsertCommand.Parameters(11).Value = DataGrid1(i, 11).GetType
        adapter.InsertCommand.Parameters(12).Value = DataGrid1(i, 12).GetType
        adapter.InsertCommand.Parameters(13).Value = DataGrid1(i, 13).GetType
        adapter.InsertCommand.Parameters(14).Value = DataGrid1(i, 14).GetType
        adapter.InsertCommand.Parameters(15).Value = DataGrid1(i, 15).GetType
        adapter.InsertCommand.Parameters(16).Value = DataGrid1(i, 16).GetType
        adapter.InsertCommand.Parameters(17).Value = DataGrid1(i, 17).GetType
        sqlconn.Open()

        adapter.InsertCommand.ExecuteNonQuery()
        MsgBox("success fully added")
        adapter.InsertCommand.Parameters.Clear()

    Next
    sqlconn.Close()

How do I implement IConvertible in an object?

Était-ce utile?

La solution

To be honest, I really do not understand why you keep assigning a type to the value of the parameter...

Surely, what you want is casting the content of your cell to the right type, for example in your first line you would want to do:

adapter.InsertCommand.Parameters(0).Value = CStr(DataGrid1(i, 0))

Or possibly

adapter.InsertCommand.Parameters(0).Value = DataGrid1(i, 0).ToString()

For any parameter of type Char. For DateTime or Int type parameters you would want to use CDate() and CInt, but I don't think you want to use GetType() as that just gets the type of the value in the cell and not it's actual value.

Also, whilst you seem to have 19 parameters to your command, you are only adding 18 values to it, I don't know if that is desired behaviour, but it seems odd.

Autres conseils

It seems odd that you want to insert a Type value into each parameter:

adapter.InsertCommand.Parameters(0).Value = DataGrid1(i, 0).GetType

The error is also most likely due to trying to convert a Type to the various data types the parameter is intended to hold.

Perhaps you intended to do the following instead:

adapter.InsertCommand.Parameters(0).Value = DataGrid1(i, 0).Value
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top