0

I'm trying to post a List from my Model so that within the Controller I can view all the contacts that are within the List.

I have tried many different ways, but for some reason I get the same error that the List is null, even though there is data within my Microsoft Access Database. Also, when I try to edit the data, I can do so, but I'm not able to view the data that was previously present within the text boxes of the Controller/View.

If some could please help me.

MODEL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;

namespace CW2_Enterprise
{
    public class Contact
    {
        OleDbConnection connection = new OleDbConnection();

        public List<Contact> contacts = new List<Contact>();

        String id;
        String firstName;
        String lastName;
        String postcode;
        String email;
        String phone;

        public Contact(string id, string firstName, string lastName, string postcode, string email, string phone)
        {
            this.ID = id;
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Postcode = postcode;
            this.Email = email;
            this.Phone = phone;
        }

        public void addContacts(Contact contact)
        {
            string id = contact.ID;
            string firstName = contact.FirstName;
            string lastName = contact.LastName;
            string postcode = contact.Postcode;
            string email = contact.Email;
            string phone = contact.Phone;

            try
            {
                connection.ConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\Database_2.mdb";
                connection.Open();

                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;

                command.CommandText = "INSERT INTO Contact VALUES ('" + id + "','" + firstName + "','" + lastName + "','" + postcode + "','" + email + "','" + phone + "')";

                command.ExecuteNonQuery();

                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        public void viewContacts(Contact contact)
        {
            try
            {
                connection.ConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\Database_2.mdb";
                connection.Open();

                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;

                command.CommandText = "SELECT * FROM Contact";

                OleDbDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    contact.ID = reader["ID"].ToString();
                    contact.FirstName = reader["FirstName"].ToString();
                    contact.LastName = reader["LastName"].ToString();
                    contact.Postcode = reader["Postcode"].ToString();
                    contact.Email = reader["Email"].ToString();
                    contact.Phone = reader["Phone"].ToString();

                    contact.contacts.Add(contact);
                }

                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        public void find(Contact contact)
        {
            string id = contact.ID;

            try
            {
                connection.ConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\Database_2.mdb";
                connection.Open();

                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;

                command.CommandText = "SELECT * FROM Contact WHERE ID='" + contact.ID + "'";

                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        public void editContacts(Contact contact)
        {
            string id = contact.ID;
            string firstName = contact.FirstName;
            string lastName = contact.LastName;
            string postcode = contact.Postcode;
            string email = contact.Email;
            string phone = contact.Phone;

            try
            {
                connection.ConnectionString = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\Database_2.mdb";
                connection.Open();

                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;

                string see = "UPDATE Contact SET FirstName='" + firstName + "', LastName='" + lastName + "', Postcode='" + postcode + "', Email='" + email + "', Phone='" + phone + "' WHERE ID='" + id + "'";

                command.CommandText = see;

                OleDbDataReader reader = command.ExecuteReader();

                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex);
            }
        }

        public string ID { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Postcode { get; set; }

        public string Email { get; set; }

        public string Phone { get; set; }

        public List<Contact> ContactsView { get; set; }
    }
}

View/Controller (Partial Class)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CW2_Enterprise
{
    public partial class viewContacts : Form
    {
        Contact contact = new Contact();

        ComboBox comboBox = new ComboBox();

        public viewContacts()
        {
            InitializeComponent();
        }

        private void viewContacts_Load(object sender, EventArgs e)
        {
            comboBox.Location = new System.Drawing.Point(10, 10);
            comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox.Size = new System.Drawing.Size(136, 81);
            this.Controls.Add(comboBox);

            comboBox.Items.Add(contact.FirstName);

            contact.viewContacts(contact);
        }
   }
}

Edit Contacts View/Controller (Partial Class)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CW2_Enterprise
{
    public partial class editContacts : Form
    {
        TextBox idBox = new TextBox();
        TextBox firstNameBox = new TextBox();
        TextBox lastNameBox = new TextBox();
        TextBox postcodeBox = new TextBox();
        TextBox emailBox = new TextBox();
        TextBox phoneBox = new TextBox();

        public editContacts()
        {
            InitializeComponent();
        }

        private void editContacts_Load(object sender, EventArgs e)
        {
            Contact contact = new Contact();
            contact.ID = idBox.Text;

            Label idLabel = new Label();
            idLabel.Text = "ID: ";
            idLabel.Size = new Size(80, 30);
            System.Drawing.Point idLabelLocation = new System.Drawing.Point(10, 15);
            idLabel.Location = idLabelLocation;
            this.Controls.Add(idLabel);

            idBox.Name = "ID";
            idBox.Size = new Size(250, 200);
            System.Drawing.Point idBoxLocation = new System.Drawing.Point(100, 15);
            idBox.Location = idBoxLocation;
            this.Controls.Add(idBox);

            Button show = new Button();
            show.Text = "SHOW";
            show.Size = new Size(75, 23);
            System.Drawing.Point showLocation = new System.Drawing.Point(380, 13);
            showButton.Location = showLocation;
            this.Controls.Add(show);

            show.Click += new System.EventHandler(this.show_Click);

            contact.find(contact);
        }

        private void show_Click(object sender, EventArgs e)
        {
            Contact contact = new Contact();
            contact.FirstName = firstNameBox.Text;
            contact.LastName = lastNameBox.Text;
            contact.Postcode = postcodeBox.Text;
            contact.Email = emailBox.Text;
            contact.Phone = phoneBox.Text;

            Console.WriteLine("Contact: " + contact);

            Label firstNameLabel = new Label();
            firstNameLabel.Text = "First Name: ";
            firstNameLabel.Size = new Size(80, 30);
            System.Drawing.Point firstNameLabelLocation = new System.Drawing.Point(10, 45);
            firstNameLabel.Location = firstNameLabelLocation;
            this.Controls.Add(firstNameLabel);

            firstNameBox.Text = contact.FirstName;
            firstNameBox.Size = new Size(250, 200);
            System.Drawing.Point firstNameBoxLocation = new System.Drawing.Point(100, 45);
            firstNameBox.Location = firstNameBoxLocation;
            this.Controls.Add(firstNameBox);

            Button edit = new Button();
            edit.Text = "Edit";
            edit.Size = new Size(250, 50);
            System.Drawing.Point editLocation = new System.Drawing.Point(40, 200);
            edit.Location = editLocation;
            this.Controls.Add(edit);

            edit.Click += new System.EventHandler(this.edit_Click);
        }

        private void edit_Click(object sender, EventArgs e)
        {
            Contact contact = new Contact();
            contact.ID = idBox.Text;
            contact.FirstName = firstNameBox.Text;
            contact.LastName = lastNameBox.Text;
            contact.Postcode = postcodeBox.Text;
            contact.Email = emailBox.Text;
            contact.Phone = phoneBox.Text;

            contact.editContacts(contact);
        }
    }
}
Aadil Hafesji
  • 383
  • 2
  • 7
  • 23

0 Answers0