0

I want to populate the result of an SQL count query on a Access database in a textbox showing how many results there are. For example I have a code number inputted into the database 200 time, i want the textbox to show 200.

Heres my code so far:

    ID = DataGridView1.CurrentRow.Cells(0).Value
    fn = DataGridView1.CurrentRow.Cells(1).Value
    ln = DataGridView1.CurrentRow.Cells(2).Value
    SKU = DataGridView1.CurrentRow.Cells(4).Value
    FC = DataGridView1.CurrentRow.Cells(5).Value


    Dim countNoTwo As String = "SELECT COUNT skuNo FROM table WHERE skuNo = " & SKU & ""
    Dim connection As New OleDbConnection(duraGadgetDB)
    Dim dataadapter As New OleDbDataAdapter(countNoTwo, connection)
    Dim ds As New DataSet()
    connection.Open()
    dataadapter.Fill(ds, "dura")
    connection.Close()

   txtbox1.Text

How do i bind the result of the dataset to the txtbox1.Text?

user1352057
  • 3,162
  • 9
  • 51
  • 117

2 Answers2

1
  • First, do not use string concatenation to build sql commands (reason=Sql Injection + Parsing problems)
  • Second, if your command returns only one single result you could use ExecuteScalar
  • Third, use the Using statement to be sure that your connection and commands are correctly disposed after use

    Dim countNoTwo As String = "SELECT COUNT skuNo FROM table WHERE skuNo = ?"
    Using connection As New OleDbConnection(duraGadgetDB)
        Using command As New OleDbCommand(countNoTwo, connection)
           command.Parameters.AddWithValue("@sku", SKU)
           connection.Open()
           Dim result = Convert.ToInt32(command.ExecuteScalar())
           txtbox1.Text = result.ToString
        End Using
    End Using
    
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
0

Try this

Dim dt As DataTable = ds.Tables(0)
txtbox1.Text = dt.Rows(0).Item(0).ToString()
codingbiz
  • 26,179
  • 8
  • 59
  • 96