• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

c# mysql what wrong with it

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;");
            conDatabase.Open();





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

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


                        Console.WriteLine(count.ToString());

                    }
                    catch (MySqlException ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }

Open in new window

0
teera
Asked:
teera
3 Solutions
 
Rajkumar GsSoftware EngineerCommented:
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

0
 
Friman001Commented:
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.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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 ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now