-1

Tengo un error cuando ejecuto el programa:


Error code:

The thread 0x2da0 has exited with code 0 (0x0). Exception thrown: 'System.NullReferenceException' in Dapper.dll Error: Exception Referencia a objeto no establecida como instancia de un objeto. Exception thrown: 'MySql.Data.MySqlClient.MySqlException' in MySql.Data.dll Warning:Exception Column count doesn't match value count at row 1 The program '[26420] UsuarisWindowsFormsApp.exe' has


Código:

namespace UsuarisWindowsFormsApp { public partial class Form1 : Form { public Form1() {

        InitializeComponent();
    }

    private const string connectionString = "Server=127.0.0.1;Port=3306;Database=biblioteca;Uid=usuari;Pwd=seCret_16";

    private void registerButton_Click_Click(object sender, EventArgs e)
    {
        if (PasswordTextBox.Text == ConfirmPasswordTextBox.Text) 
        { 
            MySqlConnection cnn = new MySqlConnection(connectionString);
            string sql = "SELECT MAX(ID_USR) FROM USUARIS";
            int newId = 0;
            try {
                newId = cnn.Query<int>(sql).FirstOrDefault();
            } catch(Exception ex)
            {
                Console.WriteLine("Error: Exception " + ex.Message);
            }

            //corregir linia insert into
            // string sqlInsert = $"INSERT ID_USR, DNI_USR, LLINATGE1, POB_USR, EMAIL_USR, PASSWORD INTO USUARIS VALUES " +
                //$"({newId + 1}, '{NIFTextBox.Text}', '{NameTextBox.Text}','{SurnameTextBox.Text}','{TownComboBox.Text}','{EmailTextBox.Text}','{PasswordTextBox.Text}')";
            string sqlInsert = $"INSERT INTO usuaris (ID_USR, DNI_USR, LLINATGE1, POB_USR, EMAIL_USR, PASSWORD) VALUES " +
                $"({newId + 1}, '{NIFTextBox.Text}', '{NameTextBox.Text}','{SurnameTextBox.Text}','{TownComboBox.Text}','{EmailTextBox.Text}','{PasswordTextBox.Text}')";
            try
            {
                var rowsAffected = cnn.Execute(sqlInsert);
                if (rowsAffected == 1)
                {
                    MessageBox.Show("User Register Correctly. ", "User Manager", MessageBoxButtons.OK );
                    NIFTextBox.Text = string.Empty;
                    NameTextBox.Text = string.Empty;
                    SurnameTextBox.Text = string.Empty;
                    TownComboBox.Text = string.Empty;
                    EmailTextBox.Text = string.Empty;
                    PasswordTextBox.Text = string.Empty;
                }
            } catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                Console.WriteLine("Warning:Exception " + ex.Message);
            }


            cnn.Close();
        }
    }

    private void PasswordTextBox_TextChanged(object sender, EventArgs e)
    {

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}

}

1 respuesta 1

0

The value {NameTextBox.Text} doesn't have a column assigned. The correct code:

(El value {NameTextBox.Text} no tiene una columna asignada. El código correcto es:)

private const string connectionString = "Server=127.0.0.1;Port=3306;Database=biblioteca;Uid=usuari;Pwd=seCret_16";

private void registerButton_Click_Click(object sender, EventArgs e)
{
    if (PasswordTextBox.Text == ConfirmPasswordTextBox.Text) 
    { 
        MySqlConnection cnn = new MySqlConnection(connectionString);
        string sql = "SELECT MAX(ID_USR) FROM USUARIS";
        int newId = 0;
        try {
            newId = cnn.Query<int>(sql).FirstOrDefault();
        } catch(Exception ex)
        {
            Console.WriteLine("Error: Exception " + ex.Message);
        }

        //corregir linia insert into
        // string sqlInsert = $"INSERT ID_USR, DNI_USR, NOM_USR, LLINATGE1, POB_USR, EMAIL_USR, PASSWORD INTO USUARIS VALUES " +
            //$"({newId + 1}, '{NIFTextBox.Text}', '{NameTextBox.Text}','{SurnameTextBox.Text}','{TownComboBox.Text}','{EmailTextBox.Text}','{PasswordTextBox.Text}')";
        string sqlInsert = $"INSERT INTO usuaris (ID_USR, DNI_USR, LLINATGE1, POB_USR, EMAIL_USR, PASSWORD) VALUES " +
            $"({newId + 1}, '{NIFTextBox.Text}', '{NameTextBox.Text}','{SurnameTextBox.Text}','{TownComboBox.Text}','{EmailTextBox.Text}','{PasswordTextBox.Text}')";
        try
        {
            var rowsAffected = cnn.Execute(sqlInsert);
            if (rowsAffected == 1)
            {
                MessageBox.Show("User Register Correctly. ", "User Manager", MessageBoxButtons.OK );
                NIFTextBox.Text = string.Empty;
                NameTextBox.Text = string.Empty;
                SurnameTextBox.Text = string.Empty;
                TownComboBox.Text = string.Empty;
                EmailTextBox.Text = string.Empty;
                PasswordTextBox.Text = string.Empty;
            }
        } catch (MySql.Data.MySqlClient.MySqlException ex)
        {
            Console.WriteLine("Warning:Exception " + ex.Message);
        }


        cnn.Close();
    }
}
1
  • 2
    por favor coloca tu respuesta en español. Recuerda que esto es StackOverflow en español!. Commented el 23 oct. 2019 a las 14:47

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.