compilation error

chi2king

Member
Joined
Sep 28, 2006
Messages
20
Programming Experience
Beginner
I have an error in my code which states:
System.Data.SqlClient.SqlException: Conversion failed when converting the varchar value '(ListBox1.SelectedValue.ToString)'
to data type int. at System.Data.SqlClient.SqlConnection
Can anyone help me why this is happening. im using web developer express.

VB.NET:
Protected Sub DeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim DBConn As New Data.SqlClient.SqlConnection("Data Source=XXX;Initial Catalog=XXX;Integrated Security=True")
        Dim DBCmd As New Data.SqlClient.SqlCommand
        Dim DBAdap As New Data.SqlClient.SqlDataAdapter
        Dim DS As New Data.DataSet
        DBConn.Open()
        Try
            DBCmd = New Data.SqlClient.SqlCommand("DELETE FROM family WHERE firstname = '" & TextBox2.Text & "' AND ID = 
'(ListBox1.SelectedValue.ToString)'", DBConn)
 
            DBCmd.ExecuteNonQuery()
            Response.Write("Your record is deleted")
            DBAdap = New Data.SqlClient.SqlDataAdapter("SELECT * FROM family", DBConn)
            DBAdap.Fill(DS)
            ListBox1.DataBind()
        Catch exp As Exception
            Response.Write(exp)
        End Try
        DBCmd.Dispose()
        DBAdap.Dispose()
        DBConn.Close()
        DBConn = Nothing
 
    End Sub


My purpose is to delete the row when i click the delete button. firstname is displayed on a textbox when i click on the
listbox and ID is the value that is not displayed on the listbox.

Also how can i display the remaining details into the textboxes since i can only display one detail into one of the textbox.
VB.NET:
Protected Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles 
ListBox1.SelectedIndexChanged
        TextBox1.Text = ListBox1.SelectedItem.ToString
    End Sub
 
Last edited:
You can't just put windows forms code in a web application and expect it to work, they are very different development environments and the WebControls.ListBox control is not a Forms.ListBox. Start here by looking up the documentation from WebControl.ListBox members going into the ones you have problem with.
 
Back
Top