c# mysql  what wrong with it

Posted on 2011-10-23
Last Modified: 2012-05-12
c# mysql  what wrong with it code error
MySqlConnection conDatabase = new MySqlConnection("Data Source=localhost;" +
                                       "Persist Security Info=yes;" +
                                       "UserId=tee; PWD=t579; database=ph3;");

            using (MySqlCommand cmd = new MySqlCommand("SELECT count(*) FROM `member` WHERE `tambol` LIKE '¿¿¿¿¿¿¿' ", conDatabase))

                        int count = (int)cmd.ExecuteScalar();


                    catch (MySqlException ex)

Open in new window

Question by:teera
    LVL 23

    Accepted Solution

    Query may be returning NULL
    SELECT count(*) FROM `member` WHERE `tambol` LIKE '¿¿¿¿¿¿¿' "

    Open in new window

    Try change this query to
    SELECT ISNULL(count(*), 0) FROM `member` WHERE `tambol` LIKE '¿¿¿¿¿¿¿' "

    Open in new window

    LVL 4

    Assisted Solution

    Try putting a break on the cmd.  When it hits that, press F10 and see if cmd is null.  If it is, you could just put in "if (cmd != null) { int count = (int)cmd.ExecuteScalar(); }".  Otherwise,  I would try what RajkumarGS said.  I am just going off what he said.
    LVL 142

    Assisted Solution

    by:Guy Hengel [angelIII / a3]
    I usually debug by doing this in 2 steps:

     object res = cmd.ExecuteScalar();
     int count = (res == null || res == DBNull.Value) ? 0 : (int)res;

    Open in new window

    and eventually debug on "res" value to see what it is ...

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now