Question I need help plz for a listview

htmalbenur

New member
Joined
Sep 5, 2021
Messages
1
Programming Experience
1-3
VB.NET:
    Private Sub Txt_ChercherHomme_TextChanged(sender As Object, e As EventArgs) Handles Txt_ChercherHomme.TextChanged
        Dim con As New SqlConnection("Data Source=DESKTOP-8COQPOR; Initial Catalog=ActeMariage; Integrated Security=True")
        con.open()
        Dim cmd As New SqlCommand("select * from Homme where CIN_Homme like '%'+@parm1+'%' And @parm2 >= 2", con)
        cmd.Parameters.AddWithValue("parm1", Txt_ChercherHomme.Text.Trim)
        cmd.Parameters.AddWithValue("parm2", Txt_ChercherHomme.TextLength.ToString)
        Dim dr As SqlDataReader = cmd.ExecuteReader
        ListViewHomme.Items.Clear()
        Do While dr.Read()
            Dim list1 = ListViewHomme.Items.Add(dr(0))
            list1.SubItems.Add(dr(1))
            list1.SubItems.Add(dr(2))
            list1.SubItems.Add(dr(3))
            list1.SubItems.Add(dr(4))
            list1.SubItems.Add(dr(5))
        Loop
        con.Close()
    End Sub

it's not working.... I'm new with vb.net..... some can help me plz....
 
Last edited by a moderator:
The code is working because it is doing exactly what that code should do. It may not be doing what you want it to do but you haven't actually told us what that is, so we could only guess, which we should not have to do. "it's not working" is not a problem statement. You need to provide a FULL and CLEAR explanation of the problem, which includes exactly what you're trying to achieve (missing), how you're trying to achieve it (present) and what happens when you try (missing). The specific desired and actual behaviour determine whether it's working and, if not, what needs to be changed to make it work.
 
It seems rather odd to have a text length check in your SQL code. If the user has entered 0 or 1 characters then you're still going to query the database, even though the result set is guaranteed to be empty. Wouldn't it make more sense to do the length check first and then only query the database if at least 2 characters have been entered?

Also, I would recommend against using a ListView at all. You can populate a DataTable from that data reader and then bind that to a DataGridView, either directly or via a BindingSource. You can even bind the data reader via a BindingSource, which will be fine if the data is not to be edited and saved.
 
This is absolutely wrong:
VB.NET:
cmd.Parameters.AddWithValue("parm2", Txt_ChercherHomme.TextLength.ToString)
The whole point of AddWithValue is that the data type gets inferred from the value. Why would you start with a number and convert it to a String, thus having the data type inferred as some text type, when you specifically want to use the value as a number in the SQL code? DO NOT convert data that is not text into text willy-nilly. Only do so if you specifically need a String, e.g. to display in a control. Numbers and date/time values should stay in their native formats all the time.
 
Back
Top