Home > Software design >  Can't insert data into database with ASP.NET/C#
Can't insert data into database with ASP.NET/C#

Time:03-27

I've been trying to create a simple registration page and connecting it with database, but when I click on the button to register a new user it returns the following message:

at MappingDatabase.Connection () [0x00016] in <bea5b5103c9f4cfba42ba460a8c4180f>:0 
at PersonDatabase.Insert (Person p) [0x00002] in <bea5b5103c9f4cfba42ba460a8c4180f>:0 

Here's the code:

using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;

public static class MappingDatabase
{

    public static IDbConnection Connection()
    {
        MySqlConnection connection = new MySqlConnection(ConfigurationManager.AppSettings["databaseConnection"]);
        connection.Open();
        return connection;
    }

    public static IDbCommand Command(string sqlQuery, IDbConnection connection)
    {
        IDbCommand runCommand = connection.CreateCommand();
        runCommand.CommandText = sqlQuery;
        return runCommand;
    }
    
    public static IDbDataAdapter Adapter(IDbCommand command)
    {
        IDbDataAdapter adapter = new MySqlDataAdapter();
        adapter.SelectCommand = command;
        return adapter;
    }

    public static IDbDataParameter Parameter(string parameterName, object value)
    {
        return new MySqlParameter(parameterName, value);
    }
}
public static class PersonDatabase
{

    public static int Insert(Person p)
    {
        try
        {
            IDbConnection dbConnection = MappingDatabase.Connection();
            IDbCommand dbCommand;
            string sql = @"INSERT INTO PER_PERSON VALUES(0, ?name, ?email, ?password);";
            dbCommand = MappingDatabase.Command(sql, dbConnection);
            dbCommand.Parameters.Add(MappingDatabase.Parameter("?name", p.Name));
            dbCommand.Parameters.Add(MappingDatabase.Parameter("?email", p.Email));
            dbCommand.Parameters.Add(MappingDatabase.Parameter("?password", p.Password));
            dbCommand.ExecuteNonQuery();
            dbConnection.Close();
            dbCommand.Dispose();
            dbConnection.Dispose();
            return 0;
        }
        catch(Exception e)
        {
            Console.WriteLine(e.StackTrace);
            return -1;
        }
    }
}

Here's the repository on GitHub

It worked before on a computer running Windows 10. Could it be a problem with the connector file (MySql.Data.dll)? How can I fix that?

Note: I use Linux Ubuntu 20.04

CodePudding user response:

You can fix this error by downloading the right connector in the hyperlink below:

Download Connector

  • Related