none
Connection error during login : The Error is throwing the else statement in the code RRS feed

  • Question

  • I am trying to use a code from a template I got .Now each time I try to login the else statement in the code is thrown.

    What could be the problem ?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using MySql.Data.MySqlClient;
    using System.Data;
    using System.Windows.Forms;
    
    
    namespace LeaveManagementSystem1._1
    {
        class SQLConfig
        {
    
            private MySqlConnection con = new MySqlConnection("server=localhost;user id=root;Rivers123*#=;database=db_leave;sslMode=none");
            private MySqlCommand cmd;
            private MySqlDataAdapter da;
            public DataTable dt; 
            int result;
            usableFunction funct = new usableFunction(); 
            public void Execute_CUD(string sql, string msg_false, string msg_true)
            {
                try
                {
                    con.Open();
                    cmd = new MySqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    result = cmd.ExecuteNonQuery();
    
                    if(result > 0)
                    {
                        MessageBox.Show(msg_true);
                    }
                    else
                    {
                        MessageBox.Show(msg_false);
                    } 
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close(); 
                }
            }
            public void Execute_Query(string sql)
            {
                try
                {
                    con.Open();
                    cmd = new MySqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    result = cmd.ExecuteNonQuery();
                     
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                }
            }
            public void Load_DTG(string sql,DataGridView dtg)
            {
                try
                {
                    //con.Open();
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open();
                    }
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    dtg.DataSource = dt;
    
                   
                    funct.ResponsiveDtg(dtg);
                    dtg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dtg.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    
                    
                    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    da.Dispose();
                    con.Close();
                }
    
            }
    
            public void Load_ResultList(string sql, DataGridView dtg)
            {
                try
                {
                    //con.Open();
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open();
                    }
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    dtg.DataSource = dt;
    
    
                    funct.ResponsiveDtg(dtg);
                    dtg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                    dtg.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    
    
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    da.Dispose();
                    con.Close();
                }
    
            }
            public void fiil_CBO(string sql, ComboBox cbo)
            {
                try
                {
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open(); 
                    } 
    
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
    
                    cbo.DataSource = dt;
                    cbo.ValueMember = dt.Columns[0].ColumnName;
                    cbo.DisplayMember = dt.Columns[1].ColumnName;
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                    da.Dispose();
                }
    
            }
            public void combo(string sql, ComboBox cbo)
            {
                try
                {
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open();
                    }
    
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
    
                    cbo.Items.Clear();
                    cbo.Text = "Select";
                    foreach(DataRow r in dt.Rows)
                    {
                        cbo.Items.Add(r.Field<string>(0));
                    }
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                    da.Dispose();
                }
    
            }
            public void singleResult(string sql)
    
            {
                try
                {
                    con.Open();
                    if (con.State == ConnectionState.Open)
                    {
                       con.Close();
                    }
                    else
                    {
                        con.Open(); 
                    }
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    da.SelectCommand = cmd;
                    da.Fill(dt);  
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    da.Dispose();
                    con.Close();
                }
            }
            public int maxrow(string sql)
    
            {
                int maxrow = 0;
                try
                {
                    con.Open(); 
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
    
                    maxrow = dt.Rows.Count;
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    da.Dispose();
                    con.Close();
                }
                return maxrow;
            }
            public void loadReports(string sql)
    
            {
                try
                {
                    con.Open();
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                    da.Dispose();
                }
            }
    
            public void autocomplete(string sql,TextBox txt)
            {
                try
                {
                    con.Open();
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = sql;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
    
                    txt.AutoCompleteCustomSource.Clear();
                    txt.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
    
                    foreach (DataRow r in dt.Rows)
                    {
                        txt.AutoCompleteCustomSource.Add(r.Field<string>(0));
                    }
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                    da.Dispose();
                }
            }
    
            public void autonumber(string AUTOKEY, TextBox txt)
            {
                try
                {
    
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open(); 
                    }
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = "SELECT concat(`STRT`, `END`) FROM `tblautonumber` WHERE `DESCRIPTION`='" + AUTOKEY + "'";
                    da.SelectCommand = cmd;
                    da.Fill(dt);
    
                    txt.Text = DateTime.Now.ToString("yyyy") + dt.Rows[0].Field<string>(0);
                
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                    da.Dispose();
                }
            }
    
            public void trans_autonumber(string AUTOKEY, Label txt)
            {
                try
                {
    
                    if (con.State != ConnectionState.Open)
                    {
                        con.Open();
                    }
                    cmd = new MySqlCommand();
                    da = new MySqlDataAdapter();
                    dt = new DataTable();
    
    
                    cmd.Connection = con;
                    cmd.CommandText = "SELECT concat(`STRT`, `END`) FROM `tblautonumber` WHERE `DESCRIPTION`='" + AUTOKEY + "'";
                    da.SelectCommand = cmd;
                    da.Fill(dt);
    
                    txt.Text = DateTime.Now.ToString("yyyy") + dt.Rows[0].Field<string>(0);
    
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    con.Close();
                    da.Dispose();
                }
            }
            public void update_Autonumber(string id)
            { 
                Execute_Query("UPDATE `tblautonumber` SET `END`=`END`+`INCREMENT` WHERE `DESCRIPTION`='" + id + "'");
            }
    
          
        }
    }

    ALTER TABLE `tbluser`
      MODIFY `user_id` int(30) NOT NULL AUTO_INCREMENT, AUTO_ItbluserNCREMENT=10329;
    COMMIT;

    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
        </startup>
    </configuration>

    Friday, November 29, 2019 11:09 AM

All replies

  • Hello,

    Lots of code posted but no indication which method and line in the method you are referring too.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, November 29, 2019 11:16 AM
    Moderator
  • Can you provide more details about exception you get? Maybe stack trace or line number and message.

    With quick review at least following code might be source of error since connection is closed.

    con.Open();
    
    if (con.State == ConnectionState.Open)
    {
        con.Close();
    }
    else
    {
        con.Open(); 
    }


    • Edited by MasaSam Friday, November 29, 2019 11:32 AM
    Friday, November 29, 2019 11:31 AM
  • query = "SELECT * FROM tbluser WHERE username= '" + UsernameTextBox.Text + "' and pass = sha1('" + PasswordTextBox.Text + "')";
                maxrow = SQL.maxrow(query);
                if(maxrow > 0)
                {
                    MessageBox.Show("Welcome user");
                    this.Close();
    
                    frm.enabled_menu();
    
                }
                else
                {
                    MessageBox.Show("Account does not exist. Please contact administrator.","Invalid account",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            }
    

    rr

    The error is from the messagebox ! Account does not exist .Please contact administrator ............................

    Friday, November 29, 2019 12:22 PM
  • That then indicates that there is not such user in database server or as database user. Or possibly user name/password pair is invalid.
    Friday, November 29, 2019 12:48 PM