Results 1 to 10 of 10

Thread: [RESOLVED] Count isnt returning anything

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Resolved [RESOLVED] Count isnt returning anything

    i have used this code, but i always get msgbox as 0.
    What am i doing wrong.

    vb Code:
    1. SqlStr = "SELECT Count(memberid) FROM members WHERE membername Like '*" & Me.TxtFind.Text & "*'"
    2.                 Using Conn As New OleDb.OleDbConnection(My.Settings.DMSConnectionString)
    3.                     Using Cmd As New OleDb.OleDbCommand(SqlStr, Conn)
    4.                         Conn.Open()
    5.                         MessageBox.Show(Cmd.ExecuteNonQuery().ToString)
    6.                         Conn.Close()
    7.                     End Using
    8.                 End Using

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Count isnt returning anything

    ExecuteNonQuery is when you are performing something OTHER than a "SELECT" on a database, aka you are not querying data, you are deleting or updating, or inserting data.

    You should be using ExecuteScalar instead, which executes a query that is only supposed to return a single value.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Count isnt returning anything

    before posting it did use executescalar but it isnt working

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Count isnt returning anything

    "it isn't working" means nothing to me. You need to explain what happened when you tried. Did you get an error, did you get an undesired result? How can we help you if you provide no information other than "it isn't working"

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Count isnt returning anything

    sorry for the improperness

    I am getting 0 as the message in the msgbox

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Count isnt returning anything

    It is probable that your answer is actually 0 then.

    Are you using the correct wildcard type for your database ie sql should be
    '%" & Me.TxtFind.Text & "%'"

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Count isnt returning anything

    I agree, you probably are just returning no values because no values match, and as Grimfort said, % is used for wildcard SQL matching when you use the LIKE statement.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: Count isnt returning anything

    ok % is working. Then when should * be used

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Count isnt returning anything

    When selecting all rows of a table in a select statement so you don't have to list every single column name in the given table you are pulling data from.

    "Select * from members where memberid = 1234"

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Count isnt returning anything

    * is only used in DAO as a wildcard charactor.

    --Matt I think he ment in the Like part of the where clause
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width