0

I'm trying to insert some data to my MySql database but I keep getting this type of error message. The exact code works fine in windows forms but not in xamarin.forms. Anybody could tell me what I'm doing wrong? Here's my code:

    public static void InitializeDB()
    {
        MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
        builder.Server = SERVER;
        builder.UserID = UID;
        builder.Password = PASSWORD;
        builder.Database = DATABASE;

        String connString = builder.ToString();

        builder = null;

        Console.WriteLine(connString);

        dbConn = new MySqlConnection(connString);
        dbConn.Close();

    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        string name = UserName.Text;
        string email = Email.Text;
        string phone = PhoneNumber.Text;
        string password = Password.Text;
        String query = string.Format("INSERT INTO users(id,username, password,email,phonenumber) VALUES (@id,@name,@password,@email,@phone");
        dbConn.Open();
        MySqlCommand cmd = new MySqlCommand(query, dbConn);
        cmd.ExecuteNonQuery();
        int id = (int)cmd.LastInsertedId;
        dbConn.Close();
    }
}

}

The error: System.NullReferenceException Message=Object reference not set to an instance of an object.

  • 1
    I'm going to take a wild guess that the line `dbConn.Open` is throwing this exception and you're not calling `InitializeDB()` before handling the button click. – ProgrammingLlama Mar 25 '20 at 07:32
  • If you do not provide a demo to reproduce this issue, please point out which line have this issue error. – Leon Mar 26 '20 at 08:24

0 Answers0