6

There are 10 rows in primary_student_table.

When I execute the following code, the result was -1.

Dim count As Int16
con.Open()
query = "SELECT COUNT(roll) AS rollcount FROM primary_student_table WHERE admityear = 2011 AND batch = 1 "

cmd = New SqlCommand(query, con)

count = cmd.ExecuteNonQuery
MsgBox(count)

con.Close()

What's the problem in the above code?

2
  • 1
    1. Off-topic, but still a potential problem: You are not calling Dispose on cmd and con after you've finished using them. 2. Nothing wrong with Int16, but in VB.NET you'd usually refer to that type using the keyword Short. 3. Ideally, you'd call con.Open() at the latest-possible moment, i.e. right before executing the command; i.e. generally, try to keep connections open for the shortest-possible time. Commented Jul 21, 2013 at 7:11
  • Besides the problems mention by 491243, count(*) and count(row) can produce different results. Commented Jul 21, 2013 at 17:39

3 Answers 3

15

You should be using ExecuteScalar() rather than ExecuteNonQuery() because you are fetching a value.

count = Convert.ToInt16(cmd.ExecuteScalar())
MsgBox(count.ToString())

For proper coding

  • use using statement for proper object disposal
  • use try-catch block to properly handle exceptions

Example Code:

Dim connStr As String = "connection string here"
Dim query As String = "SELECT COUNT(roll) AS rollcount FROM primary_student_table WHERE admityear = 2011 AND batch = 1"
Using conn As New SqlConnection(connStr)
    Using cmd As New SqlCommand()
        With cmd
            .Connection = conn
            .CommandText = query
            .CommandType = CommandType.Text
        End With
        Try
            conn.Open()
            Dim count As Int16 = Convert.ToInt16(cmd.ExecuteScalar())
            MsgBox(count.ToString())
        Catch(ex As SqlException)
            ' put your exception here '
        End Try
    End Using
End Using
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. No offence, is there any other technique with less coding?
why would you want that?
I need it for my project. I am just a beginner and I can't fully understand this answer
dineshbabu is showing you the correct way to 1)get the information you want, and 2) detect when an error happens. The change to your original code would simply be to use ExecuteScalar() insted of ExecuteNonQuery(). If you don't understand the rest of the code, ask a specific questions and someone will usually help.
5

The solution is to replace

count = cmd.ExecuteNonQuery

with

count = cmd.ExecuteScalar 

Like Robert Beaubien said in his comments

Comments

1
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=1234;database=dblms"
    Dim READER As MySqlDataReader

    Try
        MysqlConn.Open()
        Dim Query As String

        Query = "Select * from dblms.accounts"
        COMMAND = New MySqlCommand(Query, MysqlConn)
        READER = COMMAND.ExecuteReader
        Dim count As Integer
        count = 0
        While READER.Read
            count = count + 1

        End While
      MysqlConn.Close()

    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()

    End Try

the value in count will be the number of rows in a table :) hope this helped

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.