0

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
2
Contributors
1
Reply
6
Views
1 Year
Discussion Span
Last Post by the_5
0

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".

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.