0

Hello i'm using c# to build an application to connect to remote mysql server.

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace login
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 

        private void button1_Click(object sender, EventArgs e)
        {
            if (tryLogin(textBox1.Text, textBox2.Text) == true)
            {
                MessageBox.Show("Authed!");
            }
            else
            {
                MessageBox.Show("Auth Failure.");

            }

        }
        public bool tryLogin(string username, string password)
        {
MySqlConnection con = new MySqlConnection("host=myhostname;user=myusername;password=mypassword;database=mydatabase;");
MySqlCommand cmd = new MySqlCommand("SELECT * FROM test WHERE username = '" + username + "' AND password = '" + password + "';");
            cmd.Connection = con;
            con.Open();
            MySqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read() != false)
            {
                if (reader.IsDBNull(0) == true)
                {
                    cmd.Connection.Close();
                    reader.Dispose();
                    cmd.Dispose();
                    return false;
                }
                else
                {
                    cmd.Connection.Close();
                    reader.Dispose();
                    cmd.Dispose();
                    return true;

                }
            }
            else
            {
                return false;
            }
        }
    }
}

It shows the following error:

"OverflowException was unhandled

Arithmetic operation resulted in an overflow "

I'm not using any arithmetic operations here. Any help ???

animuson
  • 53,861
  • 28
  • 137
  • 147
Ajay
  • 61
  • 3
  • 13
  • 3
    Could you post the stack trace or other details of the exception? You'll find the exception is occurring in one of the libraries you're calling. – Philip Kendall Jan 16 '13 at 12:59
  • 3
    also, your code is currently exposed to [SQL injections](http://en.wikipedia.org/wiki/SQL_injection) – Oren Jan 16 '13 at 13:00
  • 3
    Off the topic, you might want to take a look at the [using Statement (C#)](http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx) – Alex Filipovici Jan 16 '13 at 13:01
  • @philip kendall you mean this : login.exe!login.Form1.tryLogin(string username, string password) Line 39 C# – Ajay Jan 16 '13 at 13:01
  • In which line this is happening? – Tommaso Belluzzo Jan 16 '13 at 13:22
  • If its an odbc driver, maybe [this question][1] is the same problem. [1]: http://stackoverflow.com/questions/2545175/idatarecord-isdbnull-causes-an-system-overflowexception-arithmetic-overflow – Peter Wishart Jan 16 '13 at 13:23
  • @Ajay : what is your Build -> ConfigurationManager - > Platform(AnyCPU/x86) ? – C-va Jan 16 '13 at 14:04

1 Answers1

0

Check the test table in mysql server.

Make sure that all fields have valid values in the username and password columns.

UPDATE

try to include the option "Use Pipe=false;",with your connection string.Then it will opens the connection just fine.

I hope this will help to you.

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • @Ajay could you put your 'stack trace' on your post ? – Sampath Jan 16 '13 at 13:29
  • @Ajay you can put a brake point and then get the stack trace when error pops up.And also check my update above. – Sampath Jan 16 '13 at 13:35
  • when i add Use Pipe=false; in the connection string, it says "Keyword not supported" – Ajay Jan 16 '13 at 13:49
  • @Ajay you have to put stack trace for get more help.for get stack trace see above comment. – Sampath Jan 16 '13 at 14:14
  • Ok thanks for all of your help. It turns out the problem lies with the Mysql connector/net 6.6.4 version. It dropped support for old password authorization. So i have to uninstall it and install an older version preferably 6.5.5 – Ajay Jan 17 '13 at 01:50