getting conversion error

I am creating a website using a VB

Error: if I print getID1
Conversion from string to type 'Integer' is not valid.

Error: if I print getID2
NullReferenceException was unhandled by user code. Object reference not set to an instance of an object.

**

.aspx file

**

<asp:DataList ID="DataList1" runat="server" DataKeyField="ID" OnDeleteCommand="DeleteCommand">
    <ItemTemplate>
        <asp:ImageButton runat="server" ID="deleteIB" CommandName="Delete" ImageUrl="x-icon.png"  />
        <%# DataBinder.Eval(Container.DataItem, "ID") %>
    </ItemTemplate>
</asp:DataList>

**

.aspx.vb file

**

Dim ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("ID", GetType(Int32)))
Dim dr As DataRow
...
While reader.Read()
    dr = dt.NewRow()
    dr(0) = "<a href='" & reader(3) & "' target='_blank'>" & reader(2) & "</a>"
    dt.Rows.Add(dr)
End While
...

Protected Sub DeleteCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs)
    'Dim getID1 As Integer = CInt(DataList1.DataKeys(e.Item.ItemIndex))
    Dim getID2 As String = (CType(e.Item.FindControl("ID"), Label)).Text
    ErrorL.Text = getID2
End Sub

the first error (my vb is rusty), i think you need brackets:

Dim getID1 As Integer = CInt(DataList1.DataKeys[e.Item.ItemIndex]
or
Dim getID1 As String = DataList1.DataKeys[e.Item.ItemIndex].ToString()

the second error, as far as i can tell, you don't have a control with an id of "ID".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.