Question: I have 1 DataGridView with 3 columns, 1 button named as **AssignID**, 1 Database with **EmployeeList** Table.

Columns of Datagridview

1. Column 1 = EmployeeID
2. Column 2 = EmployeeName
3. Column 3 = Department

I'm pasting the data of **EmployeeName** and **Department** from excel into DataGridView leaving column 1 as blank. My problem is how to automatically assign the **EmployeeID** per Employee in the DataGridView when I press the button **AssignID**. The last **IDNumber** in the database is 10000.

Appreciate your help. Im still learning vb.net. Thanks in Advance.

Im using below code:

```
Private Sub AssignID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AssignID.Click
    Dim Dbcon As New OleDbConnection(connStr)

    Dbcon.Open()

    Dim query As String
    Dim dbup As New OleDbCommand
    Try

        query = "SELECT Max (EmployeeID) from EmployeeList"
        dbup = New OleDbCommand(query, Dbcon)

        Dim EmpID As Integer

        EmpID = dbup.ExecuteScalar

        Dim i2 As Integer
        i2 = Me.NewEmployeesDataGridview.CurrentRow.Index

        NewEmployeesDataGridview.Item(0, i2).Value() = EmpID

        For x As Integer = 0 To NewEmployeesDataGridview.Rows.Count - 2
            NewEmployeesDataGridview.Rows(x).Cells(0).Value = NewEmployeesDataGridview.Rows(0).Cells(0).Value + 1

        Next

        NewEmployeesDataGridview.AllowUserToAddRows = False

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

```

Answer: 
Paraphrased from Eric Lippert's blog:

<http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx>

The C# cast operator is used for identity preserving conversions (e.g. interpret this reference to base class as a reference to derived class) and identity destroying conversions (e.g. convert this `int` to a `double`). A single use of the cast operator is only allowed to invoke *one* of these.

Since you boxed an `int`, the only thing that the `int` may be unboxed to is `int` (this is an identity preserving case). Once it is unboxed to `int`, it may be converted (in the identity destroying case) to `bool`.

That is:

```
int iii = 123;
object ooo = iii;            // Box the int
bool bbb = (bool) iii;       // Perfectly legal
bool jjj = (bool) (int) ooo; // Perfectly legal
bool kkk = (bool) ooo;       // Invalid cast exception

```
First thing first: a cast of an `object` to `bool` is valid only when the `object` is a reference type wrapper for the `bool` or `Nullable<bool>` type:

```
bool val1 = true;
object mydata1=val1;
if ((bool) val1) { ... } // This is valid
bool? val2 = false;
object mydata2=val2;
if ((bool) val2) { ... } // This is valid too

```

When you cast an `object` to another type in C#, the runtime has enough information about the runtime type of the object being typecast to validate the operation, and throw an `InvalidCastException` when the cast is invalid.

In situations when the dynamic type of the object at runtime does not match the type to which you would like it to cast, you should use methods of the [`Convert`](http://msdn.microsoft.com/en-us/library/system.convert%28v=vs.110%29.aspx) class.