0

I have written a code which has a working database connection. I'm using phpmyadmin, so it's local. Everything works, except for when it doesn't have an active connection with the database; it just crashes. I've tried a timer which check for connection, but it still crashes.

My code so far:

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;
using HPJFRMS;

namespace HPJFRMS
{

public partial class HomeFRM : Form
{
    private string conn;
    MySqlConnection connect;
    string _naam = "";
    Form _Loginfrm;

    public HomeFRM(Form logFrom, string _name)
    {
        _Loginfrm = logFrom;
        InitializeComponent();
        lbWelkom.Text = "welkom " + _name;
        _naam = _name;
    }

    private void HomeFRM_Load(object sender, EventArgs e)
    {
        tmCheck.Enabled = true;
    }

    private bool Todo_ophalen()
    {
        db_connection();
        MySqlCommand cmd = new MySqlCommand();
        cmd.CommandText = "SELECT `todo` FROM `user` WHERE `username` LIKE '" + _naam + "'";
        cmd.Connection = connect;
        MySqlDataReader tdOphalen = cmd.ExecuteReader();
        if (tdOphalen.Read())
        {
            tbTodo.Text = tdOphalen.GetString(0);
            connect.Close();
            return true;
        }
        else
        {
            connect.Close();
            return false;
        }
    }

    private void db_connection()
    {
        try
        {
            conn = "Server=127.0.0.1;Database=users;Uid=root;Pwd=;";
            connect = new MySqlConnection(conn);
            connect.Open();
        }

        catch (MySqlException e)
        {
            tmCheck.Enabled = false;
            this.Hide();
            MessageBox.Show("Je bent offline! Het programma word afgesloten");
            this.Close();
        }
        finally
        {
            lbStatus.ForeColor = Color.Red;
        }
    }

    private void btBewerk_Click(object sender, EventArgs e)
    {
        if (btBewerk.Text == "Bewerken")
        {
            tbTodo.ReadOnly = false;
            btBewerk.Text = "Opslaan";

        }
        else
        {
            tbTodo.ReadOnly = true;
            btBewerk.Text = "Bewerken";
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Dispose();
    }

    private void tmCheck_Tick(object sender, EventArgs e)
    {
        bool T = Todo_ophalen();
        if (T)
        {
            lbStatus.ForeColor = Color.Green;
            lbStatus.Text = "Online";
        }
        else
        {
            lbStatus.ForeColor = Color.Red;
            lbStatus.Text = "Offline";
        }
    }
Tjazz
  • 57
  • 1
  • 11
  • Use try/catch blocks everywhere you want to connect to database. – Reza Aghaei Sep 16 '16 at 13:52
  • That happens in the void db_connection? am i right? – Tjazz Sep 16 '16 at 13:55
  • Use VS debugger with exceptions settings set to break on all Common Language Runtime Exceptions and see why your application crashes exactly. Then paste the exception with stack trace in your question. – Mateusz Krzaczek Sep 16 '16 at 13:56
  • 1
    Currently you have try/catch block in `db_connection` but there are some problems. *1)* You called `this.Close();` in catch which cause the application close. *2)* In `Todo_ophalen()` method, after calling `db_connection` if the connection get lost, the rest of code will cause an exception. – Reza Aghaei Sep 16 '16 at 13:59
  • @RezaAghaei 1) > that was just to test if it would fix the crash. 2) > So, is there a way to stop the explosion? – Tjazz Sep 16 '16 at 14:33
  • Yes, read the first comment. – Reza Aghaei Sep 16 '16 at 14:35
  • @DeX3r I have no idea how to set the exception. Never done it before. Care to explain? Sorry for my lack of knowledge. – Tjazz Sep 16 '16 at 14:37
  • @RezaAghaei So basically, copy and paste my current try catch to everywhere where i make an exception? – Tjazz Sep 16 '16 at 14:40
  • Don't copy/paste exactly current code. But yes, use it the same way you are using it in `db_connection` method. – Reza Aghaei Sep 16 '16 at 14:44
  • @RezaAghaei im am sorry for asking, but after trying for so long, i can't figure it out. i am kinda new to the database section – Tjazz Sep 16 '16 at 15:35
  • 1
    No problem, take a look at [this post](http://stackoverflow.com/a/21133452/3110834). I believe it's a basic concept and I can not describe it here. You should read more about it. But the linked post will help you :) – Reza Aghaei Sep 16 '16 at 15:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123556/discussion-between-tjazz-and-reza-aghaei). – Tjazz Sep 16 '16 at 18:09

0 Answers0