C
C
Chipu2018-09-07 17:26:06
MySQL
Chipu, 2018-09-07 17:26:06

C#, System.NullReferenceException error while working with MySql database. How to fix?

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 bd_test_program
{


    public partial class Form3 : Form
    {
        public static string constr = "server =31.31.196.8; user id=tytid; password =tytpass; persistsecurityinfo = True; port =3306; database =u0558875_default; SslMode = none";
        public MySqlConnection conn = new MySqlConnection(constr);

        public Form3()
        {
            conn.Open();
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar!=' ')
                return;
            else
                e.Handled = true;
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ' ')
                return;
            else
                e.Handled = true;
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ' ')
                return;
            else
                e.Handled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length<4 || textBox2.Text.Length<5 || textBox3.Text.Length < 2)
            {
                MessageBox.Show("Некорректные данные для регистрации.");
            }
            else
            {
                try
                {
                    string sql = "Select Login from Accounts WHERE Login = '" + textBox1.Text + "'";
                    MySqlCommand command = new MySqlCommand(sql, conn);
                    string name = command.ExecuteScalar().ToString();
                    MessageBox.Show("Аккаунт уже зарегистрирован");
                }
                catch (Exception Ex)
                {
                     string reg = "INSERT INTO Accounts (ID, Login, Password, Name, Post) VALUES(NULL, '" + textBox1.Text + "', '" + textBox2.Text + "', '" + textBox3.Text + "', '" + "Неопределен')";                 
                    MySqlCommand registration = new MySqlCommand(reg, conn);
                    registration.ExecuteNonQuery();

                }

            }

        }
    }
}

Error occurs while handling catch error (Expection Ex)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2018-09-07
@mindtester

one -

NullReferenceException
and so reports the essence of error
2 - remove all this
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar!=' ')
                return;
            else
                e.Handled = true;
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ' ')
                return;
            else
                e.Handled = true;
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != ' ')
                return;
            else
                e.Handled = true;
        }
who advised you to intervene in the processing of these events? I'm not saying that it's impossible, I just need to understand why? *
3 - try / catch you again have a mess of thoughts. it is catch that should handle the error situation. but using try/catch to separate logic is just crazy.
try doing something much simpler first, like connecting with a known account. further complicate slowly
upd * - if you filter spaces like this, google validation , it will be more reliable

E
eRKa, 2018-09-07
@kttotto

One word NullReferenceException is not enough for assumptions. To you for certain together with it and stektray gives also a line in which an error and it is possible even at a call of what method on what field. This message says that something is not initialized and you want to either read the property or call the method on it.
Judging by the code, I can assume that Select Login from Accounts WHERE ...it cannot give you a scalar value. You either need to write another request, or call another method on command.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question