none
error in execute.scalar RRS feed

  • Question

  • people, every time I try to compile this statement, an error appears saying Specified cast is not valid.

    how can I solve?

    MySqlConnection sqlconn = null;
            private string strconn = @"Server=localhost; User Id=root; DataBase=betagames";
            private string _sql = string.Empty;

            public Login()
            {
                InitializeComponent();
            }

            public void logar()
            {
                sqlconn = new MySqlConnection(strconn);
                string usu, pwd;
                txtusuario.Focus();

                try
                {
                    usu = txtusuario.Text;
                    pwd = txtsenha.Text;

                    _sql = "select count(coduser) from usuário where usuario = @usuario and senha = @senha";

                    MySqlCommand cmd = new MySqlCommand(_sql, sqlconn);

                    cmd.Parameters.Add("@usuario", MySqlDbType.VarChar).Value = usu;
                    cmd.Parameters.Add("@senha", MySqlDbType.VarChar).Value = pwd;

                    sqlconn.Open();

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

                    if (v > 0)
                    {
                        Home home = new Home();
                        home.ShowDialog();
                    }

                    else
                    {
                        MessageBox.Show("Erro ao logar");
                    }

                }
                catch(MySqlException erro)
                {
                    MessageBox.Show(erro+"No banco");
                }

            }

            private void btentra_Click(object sender, EventArgs e)
            {
                logar();
            }

            private void btapaga_Click(object sender, EventArgs e)
            {
                //Limpar registros nos textbox de usuario e senha
                txtusuario.Clear();
                txtsenha.Clear();
            }

            private void btsair_Click(object sender, EventArgs e)
            {
                //Encerra aplicação
                Application.Exit();
            }

                                                    
    • Moved by Amanda Zhu Monday, September 16, 2013 8:52 AM
    Monday, September 16, 2013 12:36 AM

Answers

  • Hi Amauri,

    If this was a SqlCommand, your ExecuteScalar would compile just fine. I'm surprised that the MySqlCommand doesn't like that. This is a compile-time error, not a run-time error?

    I don't have MySql, so I can't test this, but try using int.Parse() instead of casting. The following should work:

    int v = int.Parse(cmd.ExecuteScalar().ToString());
    
    if (v > 0)
    {
        Home home = new Home();
        home.ShowDialog();
    }
    else
    {
        MessageBox.Show("Erro ao logar");
    }


    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, September 20, 2013 4:43 AM
  • Hello,

    Welcome.

    I am confused about the “MySqlConnection”, is it the MySql DataBase or a class named MySqlConnection inherits the SqlConnection.

    With the code, I made a sample below with SqlServer:

    //1.Create A Connection             
    
                string connectionString = "server=(localdb)\\V11.0;Integrated Security=SSPI;database=TestDataBase";
    
                SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
    
                connectionStringBuilder.Add("min pool size", 0);
    
                connectionStringBuilder.Add("max pool size", 1);
    
                SqlConnection connection = new SqlConnection(connectionStringBuilder.ConnectionString);
    
                connection.Open();
    
                string sql = "select count(coursename) from course where coursename = @coursename";
    
                SqlCommand cmd = new SqlCommand(sql, connection);
    
                cmd.Parameters.Add("@coursename", SqlDbType.VarChar).Value = "English";
    
                int v = (int)cmd.ExecuteScalar();
    
                connection.Close();
    
                connection.Dispose();
    

    The result:

    So could please share your table structure of usuário so that we can help you better.

    And since I notice the program used Spanish, you can post to a Spanish forum, there you can find a better solution:

     http://social.msdn.microsoft.com/Forums/es-ES/home

    Thanks & Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 17, 2013 2:49 AM
    Moderator

All replies

  • Hello,

    I have moved this thread to ADO.NET DataSet forum for better response.

    Best regards,


    Amanda Zhu <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 16, 2013 8:52 AM
  • Hello,

    Welcome.

    I am confused about the “MySqlConnection”, is it the MySql DataBase or a class named MySqlConnection inherits the SqlConnection.

    With the code, I made a sample below with SqlServer:

    //1.Create A Connection             
    
                string connectionString = "server=(localdb)\\V11.0;Integrated Security=SSPI;database=TestDataBase";
    
                SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);
    
                connectionStringBuilder.Add("min pool size", 0);
    
                connectionStringBuilder.Add("max pool size", 1);
    
                SqlConnection connection = new SqlConnection(connectionStringBuilder.ConnectionString);
    
                connection.Open();
    
                string sql = "select count(coursename) from course where coursename = @coursename";
    
                SqlCommand cmd = new SqlCommand(sql, connection);
    
                cmd.Parameters.Add("@coursename", SqlDbType.VarChar).Value = "English";
    
                int v = (int)cmd.ExecuteScalar();
    
                connection.Close();
    
                connection.Dispose();
    

    The result:

    So could please share your table structure of usuário so that we can help you better.

    And since I notice the program used Spanish, you can post to a Spanish forum, there you can find a better solution:

     http://social.msdn.microsoft.com/Forums/es-ES/home

    Thanks & Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, September 17, 2013 2:49 AM
    Moderator
  • Hi Amauri,

    If this was a SqlCommand, your ExecuteScalar would compile just fine. I'm surprised that the MySqlCommand doesn't like that. This is a compile-time error, not a run-time error?

    I don't have MySql, so I can't test this, but try using int.Parse() instead of casting. The following should work:

    int v = int.Parse(cmd.ExecuteScalar().ToString());
    
    if (v > 0)
    {
        Home home = new Home();
        home.ShowDialog();
    }
    else
    {
        MessageBox.Show("Erro ao logar");
    }


    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, September 20, 2013 4:43 AM