0

how can I possibly populate my table if like textbox.text matches from my data inside database. I'm stuck here, not sure where I did go wrong

 Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    Dim dbcommand As String
    dbcommand = "SELECT * FROM aws_rdp where csn_user like " & txtCSNUser.Text & ""
    adt = New OleDbDataAdapter(dbcommand, dbconn)
    datatable = New DataTable
    adt.Fill(datatable)
    DataGridView1.DataSource = datatable
End Sub
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Why is it that people think that it's a good idea to ask questions about SQL code without showing the SQL code? VB code that constructs SQL code is not SQL code. Regardless, any basic reading on the the [SQL `LIKE` operator](https://www.w3schools.com/SQL/sql_like.asp) will tell you what is almost certainly the problem. – jmcilhinney Aug 04 '20 at 09:56
  • Also, you should learn how to use parameters in your queries. The way you're doing it is a great way to allow a malicious user to delete your entire database. – jmcilhinney Aug 04 '20 at 09:56
  • So what’s the problem? Are you not getting the expected results back or maybe an error? – Hursey Aug 04 '20 at 09:56
  • OK, there's more going on there than I first thought, which makes it even worse that you have provided all the relevant information and probably not even looked at your own SQL code. – jmcilhinney Aug 04 '20 at 10:00
  • You need to use an [OleDbDataAdapter.SelectCommand](https://learn.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbdataadapter.selectcommand?view=dotnet-plat-ext-3.1) with the `OleDbDataAdapter`. See the linked documentation for how to add an SQL parameter to the `SelectCommand` to pass the value of `txtCSNUser.Text`. – Andrew Morton Aug 04 '20 at 10:11
  • ![This is what I wanted to do and I'm still new on this programming][1] [1]: https://i.stack.imgur.com/KMav1.png – Nnek Lecxe Aug 04 '20 at 10:32
  • @NnekLecxe 1) It appears to me that you want `=` instead of `LIKE`. 2) You will need to *join* the tables: [SQL Inner-join with 3 tables?](https://stackoverflow.com/q/10195451/1115360). 3) Make sure to use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) so that all the variable types match up. – Andrew Morton Aug 04 '20 at 10:50

2 Answers2

0

Try putting ' before and after the quotation marks surrounding the textbox text. If you're trying to find that text within the text stored in the database you will also need wildcards (%) surrounding it too. Try:

dbcommand = "SELECT * FROM aws_rdp where csn_user like '%" & txtCSNUser.Text & "%'"

Also, as others have stated, look into using parameters in your SQL code as it will help prevent things like SQL injection and is always good practice

  • Tried this code, but still no luck with it. Anyways, heres what I really wanted https://i.stack.imgur.com/KMav1.png – Nnek Lecxe Aug 04 '20 at 10:37
  • If you don't mind, how does that parameters work or so? How can I look at it? Sorry, amateur coder here – Nnek Lecxe Aug 04 '20 at 10:42
  • Put a placeholder in your SQL statement where the value would be, like this: dbcommand = "SELECT * FROM aws_rdp where csn_user like ?" Then insert the parameter using: command.Parameters.Add(New OleDb.OleDbParameter("csn_user", txtCSNUser.Text)) I'm not too familiar with OleDB so if that doesn't work check out: https://learn.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbcommand.parameters?view=dotnet-plat-ext-3.1 – lewis.kimber Aug 05 '20 at 11:10
0

Found the answer to my problem by using this code. Anyways, thanks for your time replying on my query, will surely take note of your advises for my future references

 Dim dbcommand As String = "SELECT * FROM aws_rdp where csn_user like '%" & txtCSNUser.Text & "%'"
    Dim command As New OleDbCommand(dbcommand, dbconn)
    Dim adapter As New OleDbDataAdapter(command)
    Dim datatable As New DataTable
    adapter.Fill(datatable)
    DataGridView1.DataSource = datatable

    DataGridView1.Columns(0).HeaderText = "ID"
    DataGridView1.Columns(1).HeaderText = "IP Address"
    DataGridView1.Columns(2).HeaderText = "Username"
    DataGridView1.Columns(3).HeaderText = "Password"