There are 3 textboxes->textbox1,textbox2,textbox3 and 1 button.
In textbox1 bname will be stored and in textbox2 aname is stored.
Bname and aname are columns of databasetable.
This are not unique.
There are same bnames and anames records.
What I am trying to do is:
when the user clicks on button then the code should match textbox1 and textbox2 with the databasetable and get the count of rows that match this data.
And print the count in the textbox3.
If there is no similar bname and aname in the table then the count should show 0.
I tried doing this but am pretty sure that its not right as its not working.
umm what is the solution?

6 Years
Discussion Span
Last Post by adam_k

And you will populate (show) all bnames into textbox1, and all anames into textbox2?
How they will be seperated?
This is not a good idea, better use a listBox, so items will be in a seperated rows.


Create a connection to the database, using this as a reference.

Connect to the database and create/execute a query, storing the values in a table:

Dim ConStr As String = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"

Dim con As OleDBConnection(ConStr)

Dim cmd As OleDBCommand
Dim da As New OledbDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Dim sqls As String = "SELECT aname,bname FROM table WHERE primary_key=value"


    cmd = new OleDBCommand(sqls,con)
    da.SelectCommand = cmd
    dt = ds.Tables("nameTable")

    If dt.Rows.Count < 1 Then
        'Table is empty, no names found'
        'Post names to multiline textboxes or listview.'
        'Listview is easier to use.'
    End If

Catch ex As Exception
End Try

The syntax for counting number of records with 2 criteria is :
select count(*) from table where column1 = 'value1' and column2= 'value2'

I'm guessing that you are a beginner and won't get you messed up with parameters, so set up your command like
x = "select count(*) from table where column1 = '" & textbox1.text &"' and column2= '" & textbox2.text & "' "

You haven't told us what database you are using or how you are connecting to it, so x is the command text for whatever you are using.
Since you are only looking for 1 number you don't need to use datatables and can ExecuteScalar to get your value and assign it to textbox3 (textbox3.text = cmd.ExecuteScalar)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.