Question not retrieving data

Chrisryn

New member
Joined
May 19, 2010
Messages
2
Programming Experience
Beginner
I am trying to build a front end for an access database I created I can connect to the database but when I try to pull data into a data grid view I get no results

Here is the code what did I do wrong(I'm also new to vb)

VB.NET:
 Private Sub searchbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchbtn.Click
        Dim srval As String
        Dim type As String
        Dim srtype As String
        Dim cmd As OleDb.OleDbCommand
        srval = txtsearch.Text
        type = searchcmb.Text
        If type = "gage id" Then
            srtype = "gage_id"
        ElseIf type = "description" Then
            srtype = "description"
        Else : srtype = "current_location"
        End If
        If type = "gage_id" Then
            cmd = New OleDb.OleDbCommand("Select gage_id,description,operating_range,Status,Current_location,calibration_due from gage where @type = @search ", LoginForm1.con)
            cmd.Parameters.Add("@type", OleDb.OleDbType.LongVarChar, 50).Value = srtype
            cmd.Parameters.Add("@search", OleDb.OleDbType.LongVarChar, 50).Value = srval
        Else
            cmd = New OleDb.OleDbCommand("Select gage_id,description,operating_range,Status,Current_location,calibration_due from gage where @type like @search ", LoginForm1.con)
            cmd.Parameters.Add("@type", OleDb.OleDbType.LongVarChar, 50).Value = srtype
            cmd.Parameters.Add("@search", OleDb.OleDbType.LongVarChar, 50).Value = srval
        End If
        Dim ds As New DataSet
        ds.Clear()
        dgvsearch.DataSource = ds.Tables("gage")
       
     
    End Sub
 
Now, I've never used any kind of database system with VB.NET, but when do you tell cmd to execute the queries? I see that you create the new cmd object and add some parameters to it, but I can't see any code that executes these queries.

Maybe I'm missing something here, but that's my two cents..
 
You are correct. I guess I had a cut and past error when posting.

this is wrong

VB.NET:
ds.clear()

this is what it is supposed to say

VB.NET:
 Dim da As OleDb.OleDbDataAdapter
        da = New OleDb.OleDbDataAdapter(cmd)
        da.Fill(ds, "gage")
 
I thought that I'd answered this question elsewhere but maybe not. The issue is that you are trying to use parameters to insert identifiers into SQL code, which is not possible. This part:
VB.NET:
Select gage_id,description,operating_range,Status,Current_location,calibration_due from gage [B][U]where @type = @search[/U][/B]
is not doing what you think it's doing. You might think that that is becoming something like:
WHERE SomeColumn = "SomeValue"
but it isn't. Parameters can only be used to insert values, not identifiers, so it's actually becoming something like:
WHERE "SomeColumn" = "SomeValue"
What you intend to be a column name is actually text, and those two bits of text obviously aren't equal.

If you want to insert variable identifiers into SQL code then you need to use string concatenation. That means that you may need to take precautions against SQL injection.
 
Back
Top