having error on my codes

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

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 by a moderator:
ok... this line

DBCmd = New Data.SqlClient.SqlCommand("DELETE FROM family WHERE firstname = '" & TextBox2.Text & "' AND ID =
'(ListBox1.SelectedValue.ToString)'", DBConn)


is wrong for two reasons, the first is you havent escaped out of the sql statement by using '"... you just used '. so the sql query thought it was dealing with a string. and from the sounds of it the id field in your table is integer, and when you enclose a value in 'value' the statement interprets it as a string, and it has a cry converting it. the corrected code is

DBCmd = New Data.SqlClient.SqlCommand("DELETE FROM family WHERE firstname = '" & TextBox2.Text & "' AND ID =
" & cint(ListBox1.SelectedValue.ToString) & ", DBConn)

note that the single quote marks are gone, and the ampersands are used to concatenate the id to the sql statement

hope it helps

regards
adam
 
Last edited:
ive tried using your code but the data is not deleted on the database when i click the delete button. there were no errors though.

also the data is still displayed in the listbox.

By the way thanks.
 
after you delete the row and re-create the dataset, unbind any existing datasource on the listbox and then re-bind it using the updated dataset.


good luck

regards
adam
 
Please can you show me the code on how to do it.

Im sorry, its just that im still on the learning stage with regards to this technology.
 
Back
Top