C#C
C#2y ago
DJBonez

C# MySQL Windows form application

To preface this question I am learning development in C# on my own, no classes or formal training.

I am writing a very simple app to get familiarized with windows forms and databases. I have a working app, however; if I want to add a new field in the database, then go to the class where I list all my fields with public string or int. Then go to the DAO I created, where have it reading the table. I always get NULL issue, if I back out the new field in all the locations everything works again.

Sample of the class

namespace csapp
public class Customers
{
public int CustomerID { get; set; }
public string? BusinessName { get; set; }
public string? LastName { get; set; }
public string? FirstName { get; set; }....

addition when it breaks
public string? OtherPhone {get;set;}

Sample of the DAO file
internal class csappDAO
{
string connectionString = "datasource=localhost;port=3306;username=admin;password=admin;database=csappdb;";


public List<Customers> getAllCustomers()
{
// start with an empty list
List<Customers> returnThese = new List<Customers>();

// connect to the mysql server

MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();

// define the sql statement to fetch all customers
MySqlCommand command = new MySqlCommand("SELECT * FROM customers", connection);

using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Customers a = new Customers
{
CustomerID = reader.GetInt32(0),
BusinessName = reader.GetString(1),
LastName = reader.GetString(2),
FirstName = reader.GetString(3),...

addition when it breaks
OtherPhone=reader.GetString(21)


Hopefully this makes since
Was this page helpful?