Fill a ListBox using parameters

  • Thread starter Thread starter nigelsvision
  • Start date Start date
N

nigelsvision

Guest
Hi, So at the moment I am filling a ListBox in VB.NET Win-forms like this

I have a ListBox called DeleteRecords

I have a Label called CompanyHeader.Text

I have a Label called YesDefects.Text the value of this label says TRUE


Try
Using con As New MySqlConnection("server=My IP; userid=MyName; password=MyPassword; database=MyDB Name")
cmd.Connection = con
cmd.CommandText = ("select id from Users where Company ='" + CompanyHeader.Text() + "'")
con.Open()
Dim reader = cmd.ExecuteReader()
While reader.Read()
DeleteRecords.Items.Add(reader(0))
mySQLcon.Close()
End While
con.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try




The above does work in that it selects all id's that belong to a specific company. I just dont know how to add an extra check

how do I get to use parameters like in my example below of filling in DataGridView1?

IE I have a column in my database called Defects, I want to extract only those results in the column called Defects that say TRUE AND Those that match my CompanyHeader.Text

cmd.CommandText = ("select id from Users where Company ='" + CompanyHeader.Text() + "'AND YesDefects.Text")




This is how I did it in My DataGrid I just want same thing for my ListBox

Try
Using mySQLcon As New MySqlConnection("server=My IP; userid=My Name; password=My Pass database=My db name")

Dim Query = "select * FROM Users WHERE Broken = @Defects AND Company = @Company "

Using Command As New MySqlCommand(Query, mySQLcon)
Command.Parameters.AddWithValue("@Defects", YesDefects.Text)
Command.Parameters.AddWithValue("@Company", CompanyHeader.Text)
Using da As New MySqlDataAdapter(Command)
da.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try



Thanks,

Continue reading...
 
Back
Top