Using VB 2008, I am stumped. I have a fillby and getdataby table adapter query that does a SELECT COUNT from FILENAME where fieldname = textbox and I want to use the number of rows returned, but can't figure out how to reference it. This seems so simple, but I have been Googling this for nearly two hours and can't figure it out. Any help would be appreciated.
Thanks,
Bill P.

Recommended Answers

All 4 Replies

"Select Count (*) from FILENAME where fieldname = '" & Trim(textbox.text) & "' "

Thanks for your reply. That is basically what I am doing. My problem is that I want to use the value of COUNT in code, i.e.

if count = 0 then
' do something
end if

How do I refence it?

Thanks again,
Bill P.

im used sql server (you can modified this code with other database)

Dim myReader As SqlDataReader
conn = GetConnect()
conn.Open()
Dim temp As String
    Try
    Dim sql As String = "Select Count (*) AS 'KODE' from FILENAME where fieldname = '" & Trim(textbox.text) & "'"
    Dim comm As SqlCommand = New SqlCommand(sql, conn)
    myReader = comm.ExecuteReader
    If myReader.HasRows Then
        While myReader.Read()
            temp = myReader.Item("KODE")
        End While
	End If
		myReader.Close()
    Catch ex As Exception
    End Try
    conn.Close()
		
    If temp = 0 then
        'Do Something
    End If

im used sql server (you can modified this code with other database)

Dim myReader As SqlDataReader
conn = GetConnect()
conn.Open()
Dim temp As String
    Try
    Dim sql As String = "Select Count (*) AS 'KODE' from FILENAME where fieldname = '" & Trim(textbox.text) & "'"
    Dim comm As SqlCommand = New SqlCommand(sql, conn)
    myReader = comm.ExecuteReader
    If myReader.HasRows Then
        While myReader.Read()
            temp = myReader.Item("KODE")
        End While
	End If
		myReader.Close()
    Catch ex As Exception
    End Try
    conn.Close()
		
    If temp = 0 then
        'Do Something
    End If

You may wish to simplify the task further by using ExecuteScalar instead of ExecuteReader, and avoid the overhead of using myReader, like this:

conn = GetConnect()
conn.Open()
Dim temp As Integer
Try
Dim sql As String = "Select Count (*) AS 'KODE' from TABLENAME where fieldname = '" & Trim(textbox.text) & "'"
Dim comm As SqlCommand = New SqlCommand(sql, conn)
temp= comm.ExecuteScalar
Catch ex As Exception
End Try
conn.Close()

If temp = 0 then
'Do Something
End If

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.