irishwill2008 irishwill2008 - 7 months ago 10
Vb.net Question

How do i display data that exceeds a given value?

I have a table with a price and in there has all the prices "Customers" paid so

| ID | Price |
|----|-------|
| 1 | 5.00 |
| 50 | 6.70 |


I want to make something so when you enter 3 or 3.00 in a textbox then it will show all the records that exceeds that value so since
ID
1 paid 5 quid then they will show and same with
ID
50. I want them to display in my datagridview.

What i have so far is:

myDA = New OleDbDataAdapter
myDataSet = New DataSet
myDA.SelectCommand = New OleDbCommand()

Try
cmd = New OleDbCommand()
cmd.CommandText = "Select sum(Paid) as total from tblorder"
cmd.Connection = con
If con.State = ConnectionState.Closed Then con.Open()
myDA.SelectCommand = cmd
myDA.Fill(myDataSet, "TTotal")
TextBox1.Text = CStr(myDataSet.Tables("TTotal").Rows(0).Item(0))
DataGridView1.DataSource = myDataSet.Tables("TTotal").DefaultView
con.Close()

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Answer

I have change your code as follows:

myDA = New OleDbDataAdapter
myDataSet = New DataSet

Dim price As Double = Convert.ToDouble(TextBox1.Text)
'Save the value entered in textbox on the variable price

Try
    cmd = New OleDbCommand()
    cmd.CommandText = "Select ID,Price from tableName where Price > @Price "
    'tableName should be replaced with the name of your table
    cmd.Parameters.AddWithValue("@Price", price)
    'Add parameter to pass the price to the query
    cmd.Connection = con
    If con.State = ConnectionState.Closed Then con.Open()
    myDA.SelectCommand = cmd
    myDA.Fill(myDataSet, "tableName")
    DataGridView1.DataSource = myDataSet.Tables("tableName").DefaultView
    con.Close()
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try