Link to home
Start Free TrialLog in
Avatar of dovk179
dovk179

asked on

parameter for sql query

Imports System.Data.SqlClient
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim vuser As String = InputBox("Enter user name")
        Dim connectionString As String = "Data Source=DOVHP\SQLEXPRESS;Initial Catalog=mala2;Integrated Security=True;Pooling=False"
        ' Dim sql As String = "SELECT * FROM tbusuario where login like 'sousa'"
        Dim sql As String = "SELECT * FROM tbusuario where login like" & vuser

        Dim connection As New SqlConnection(connectionString)
        Dim dataadapter As New SqlDataAdapter(sql, connection)
        Dim ds As New DataSet()

        dataadapter.Fill(ds, "usuario_table")
        connection.Close()
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "usuario_table"
    End Sub
End Class

Open in new window


Hi,
I  want to populate the DataGridView with the register(s) where the login name is the one entered into the inputBox - vuser
There should be here a parameter which I don't know how to include.
*********************
Thanks
Dov Kruman
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Hi Dov

Try something Like This....

 Imports System.Data.SqlClient
  Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ds As New DataSet
        Dim vuser As String = InputBox("Enter user name")
        Dim connectionString As String = "Data Source=DOVHP\SQLEXPRESS;Initial Catalog=mala2;Integrated Security=True;Pooling=False"

        Dim sql As String = "SELECT * FROM tbusuario WHERE login LIKE '%' + @SUser + '%'"
        Dim SqlCon As New SqlConnection(connectionString)

        Dim Command As New SqlCommand(sql, SqlCon)
        Dim UserParam As New SqlParameter("@SUser", vuser)
        Command.Parameters.Add(AddressParam)

        Dim adapter As New SqlDataAdapter

        adapter.SelectCommand = Command
        adapter.Fill(ds)

        Me.DataGridView1.DataSource = ds.Tables(0)

End Sub
End Class

Open in new window


Hope It Helps

Yiannis
Avatar of dovk179
dovk179

ASKER

Thanks Yiannis
I have a problem with line 14.
Name 'addressParam; is not declared!
any suggestion?

Dov
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dovk179

ASKER

Working thanks a lot
Dov
Glad to help Dov

Yiannis