Database search help

Ive copied a database search out of a progrsamming book I have and as far as I can tell I've adjusted it correctly to use my db.

Whenever I run the search , it tells me theres no matching records to disply, even though there deffinetly is.
I have a suspcion (may be wrong) that its a problem with the number of row counting code, but does anyone ahve any ideas.

Thanks

If ForecastsDataSet1.Tables.Count = 0 Then
MessageBox.Show("No records found for supplied case reference", "Search") : Exit Sub
End If

Dim s As String
Dim re As String
Dim i As Integer
Dim x As Integer

s = LCase(txtsearch.Text)

If s = "" Then MessageBox.Show("Please enter search value") : Exit Sub

lstResults.Items.Clear()

For i = 0 To ForecastsDataSet1.Forecast.Rows.Count - 1
re = ForecastsDataSet1.Tables(0).Rows(i).Item(1)
If InStr(LCase(re), s) Then
lstResults.Items.Add(ForecastsDataSet1.Tables(0).Rows(i).Item(0))
x = x + 1
End If
Next
If x = 0 Then MessageBox.Show("No matching records found for " & s) : Exit Sub
lstResults.Visible = True



Answer this question

Database search help

  • WolframW

    What version of VB are you using

    The SQL is simply something like select * from myTable where col LIKE '%txt%'

    You need to replace myTable and col with real names, and you can build the string to replace txt with the text box text. This is not terrible safe, you wouldn't do it like that without some scrubbing for a production system, but it would be fine for learning.



  • smc750

    OK - is the book for VB.NET A lot of it is using VB6 syntax and not .NET syntax.



  • zapacila89

    I thought so. What's happening is that VB.NET has lots of legacy VB6 stuff in it. You're better off learning VB.NET syntax if you can. However, that won't solve the immediate problem, for that I recommend setting a break point ( right click on a line of code and choose 'insert breakpoint', make sure you're doing a debug build and hit F5. Now the code stops on this line every time, so you can set one on the line that checks for match and see what's going on. But don't forget, what you're being shown is not production level code, if I found someone doing this on my team, I would give them a warning for it. You search a database by passing it SQL.



  • ChrisMoje

    Its supposed to be for vb.net (According to the title and what it says in the book), but its beginning to look like its a bit of a mixture

  • Steve100100101

    Thanks

    The books vb.net database programming for dummies.(Iam only learning )

    I tried to use this code as I couldn't work out how how to pass a variable from a textbox, to the database and then dispaly the requested record back in to a form.

  • wolfsch

    Iam using vb.net with visual basic studio express

  • b-man

    What's the book, out of interest I've never seen code that iterates over a dataset like that put forward by a book, and almost everyone I know would agree that it's a bad idea. Where is the data coming from, and why can't you ask the database to do the search for you, with a SQL query

    Is this VB6 If not, then I presume the book is VB6, because I see the use of a lot of legacy VB6 stuff in there. You should use ToLower() instead of LCase, for example, so that you're actually using .NET and not legacy stuff.

    Have you stepped through the code to see where it's going wrong Have you stepped through the search to see where you expect it to succeed and it fails

    Are the arguments to InStr in the right order I don't know VB6.



  • Database search help