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?

2 Answers 2

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
    
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

Dim dt As DataTable = ds.Tables(0)
txtbox1.Text = dt.Rows(0).Item(0).ToString()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.