tags:

views:

111

answers:

3

hi im having a problem with my code

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    list.Items.Clear()

    cmd.CommandText = "SELECT * FROM borrow where (Department LIKE '%" & TextBox2.Text & "%')"
    cmd.Connection = con
    cmd.CommandType = CommandType.Text
    con.Open()


    rd = cmd.ExecuteReader()
    If rd.HasRows = True Then
        While rd.Read()

            Dim listview As New ListViewItem

            listview.Text = rd("ID").ToString
            listview.SubItems.Add(rd("Department").ToString)
            listview.SubItems.Add(rd("Purpose").ToString)
            listview.SubItems.Add(rd("Items_Details").ToString)
            listview.SubItems.Add(rd("Requested_by").ToString)
            listview.SubItems.Add(rd("Approved_by").ToString)
            listview.SubItems.Add(rd("Date").ToString)
            listview.SubItems.Add(rd("Status").ToString)
            listview.SubItems.Add(rd("Date_Returned").ToString)

            list.Items.Add(listview)

        End While
    End If
    con.Close()

once i typed in the string in the textbox to search for an item i get this error ""The parameterized query '(@Parameter1 nvarchar(4000))SELECT * FROM borrow where (Departme' expects the parameter '@Parameter1', which was not supplied."" hope anyone could help me..

+1  A: 

Try adding parameters like this -

cmd.Parameters.Add("@Department", SqlDbType.VarChar)
cmd.Parameters("@Department").Value = TextBox2.Text

and change your command text to what @Abe Miessler does he is right i just thought you will figure it out.

Misnomer
why am i getting variable DepartmentText already declared?..
Joegabb
I dont know why....but i get the feeling that you have a datatype issue here...or something...seeing the `Departmen` i feel you dont have enough characters alloted or something...
Misnomer
hi i've tnx.. ive changed my code but still im getting the error.. cmd.CommandText = "SELECT * FROM borrow where (Department LIKE '%@DepartmentText%')" cmd.Parameters.Add("@DepartmentText", SqlDbType.VarChar) cmd.Parameters.AddWithValue("@DepartmentText", TextBox2.Text) error "The parameterized query '(@Parameter1 nvarchar(4000),@Department varchar(8000),@Departmen' expects the parameter '@Parameter1', which was not supplied.
Joegabb
+1  A: 

Your website is in serious danger of being hacked.

Read up on SQL Injection and how to prevent it in .NET

Your query problem is the least of your concerns right now.

But.....

@Misnomer's solution is close but not quite there:

Change your query to this:

cmd.CommandText = "SELECT * FROM borrow where (Department LIKE '%@DepartmentText%')"

and add parameters this way (or the way that @Misnomer does):

cmd.Parameters.AddWithValue("@DepartmentText",TextBox2.Text)

The important difference is that you need to change your CommandText.

Abe Miessler
hi i've tnx.. ive changed my code but still im getting the error.. cmd.CommandText = "SELECT * FROM borrow where (Department LIKE '%@DepartmentText%')" cmd.Parameters.Add("@Department", SqlDbType.VarChar) cmd.Parameters.AddWithValue("@DepartmentText", TextBox2.Text) error "The parameterized query '(@Parameter1 nvarchar(4000),@Department varchar(8000),@Departmen' expects the parameter '@Parameter1', which was not supplied."
Joegabb
I suspect your error is being thrown from somewhere else. Can you post your stack trace?
Abe Miessler
Also can you update your question with your updated code?
Abe Miessler
ive post my question as answer..
Joegabb
A: 

Hi here's my code

 Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    list.Items.Clear()
    Dim mycomand = <xml>
    SELECT * FROM borrow where (Department LIKE '%@DepartmentText%');
    </xml>

    cmd.CommandText = mycomand.Value
    cmd.CommandType = CommandType.Text
    cmd.Parameters.AddWithValue("@DepartmentText", TextBox2.Text)
    cmd.Connection = con

    con.Open()


    rd = cmd.ExecuteReader()
    If rd.HasRows = True Then
        While rd.Read()

            Dim listview As New ListViewItem

            listview.Text = rd("ID").ToString
            listview.SubItems.Add(rd("Department").ToString)
            listview.SubItems.Add(rd("Purpose").ToString)
            listview.SubItems.Add(rd("Items_Details").ToString)
            listview.SubItems.Add(rd("Requested_by").ToString)
            listview.SubItems.Add(rd("Approved_by").ToString)
            listview.SubItems.Add(rd("Date").ToString)
            listview.SubItems.Add(rd("Status").ToString)
            listview.SubItems.Add(rd("Date_Returned").ToString)

            list.Items.Add(listview)

        End While
    End If
    con.Close()

End Sub

what im getting is DepartmentText is already declared.. and if not that im getting this error

cmd.CommandText = "SELECT * FROM borrow where (Department LIKE '%@DepartmentText%')" cmd.Parameters.Add("@Department", SqlDbType.VarChar) cmd.Parameters.AddWithValue("@DepartmentText", TextBox2.Text) error "The parameterized query '(@Parameter1 nvarchar(4000),@Department varchar(8000),@Departmen' expects the parameter '@Parameter1', which was not supplied.

how can i post my stack trace?.. sorry about this noobie question..

Joegabb
You are getting DepartmentText is already declared because you are doing it twice do only addwithvalue - `cmd.Parameters.AddWithValue("@DepartmentText", TextBox2.Text)`
Misnomer
done.. ive removed it.. now im getting this error.. The parameterized query '(@Parameter1 nvarchar(4000),@DepartmentText nvarchar(1)) ' expects the parameter '@Parameter1', which was not supplied.
Joegabb
Your stack trace would be the thing that is thrown onto the webpage when you get an error (unless you are using custom errors). It basically tells you where the error is coming from. I still suspect that you are looking in the wrong spot.
Abe Miessler
i got it working now.. tnx for ur help.. d problem is im using tabs and i think conflict occurs that's why im having problems with my listview..
Joegabb