Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to learn MVC pattern using winforms and I have read some tutorial to understand. Now, i'm working on my own example to just add a customer records in a database using MVC pattern.

The problem, I have a delegate method to add a record into a db that calls from the controller to the model.

I got stuck at the Model and also in the UserInterface to add a new record to the DB.(Please correct me if the MVC pattern is not good)

Somebody please help me..thank you.

Model:

namespace MVCwithDBFormReg.Model
{
    public class User
    {

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Language{ get; set; }
        public string Country{ get; set; }
        public string State{ get; set; }
        public int Zipcode { get; set; }
        public string  TimeZone{ get; set; }
        public Gender Sex { get; set; }
        public enum Gender
         {
            Male = 1,
            Female = 2

         }
         public string Month { get; set; }
         public string Day { get; set; }
         public int Year { get; set; }
         public string Occupation { get; set; }


    }

   public class UserRegister
   {
       private readonly User _user;

       public UserRegister()
       {
           _user = new User();
       }


       public void Create()
       {
           throw new NotImplementedException();
       }
   }
}

Controller:

namespace MVCwithDBFormReg.Controller
{
   public class UserController
   {
       private readonly UserRegister model;
       private readonly IUserView view;


       public UserController(UserRegister model, IUserView view)
       {
           this.model = model;
           this.view = view;

           this.view.Create += ViewOnSave;

       }

       private void ViewOnSave(object sender, EventArgs e)
       {
           model.Create();
       }
   }
}

View:

namespace MVCwithDBFormReg.View
{
    public partial class Form1 : Form, IUserView
    {

        private readonly UserRegister _model;
        public Form1(UserRegister model)
        {
            // TODO: Complete member initialization
            _model = model;
            InitializeComponent();
        }


        public string FirstName
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        public string LastName
        {
            get { return textBox2.Text; }
            set { textBox2.Text = value; }
        }

        public string Language
        {
            get { return comboBox1.SelectedItem.ToString(); }
            set { comboBox1.SelectedItem = value; }
        }

        public string Country
        {
            get { return comboBox2.SelectedItem.ToString(); }
            set { comboBox2.SelectedItem = value; }
        }

        public string State
        {
            get { return comboBox3.SelectedItem.ToString(); }
            set { comboBox3.SelectedItem = value; }
        }

        public int Zipcode
        {
            get { return int.Parse(textBox3.Text); }
            set { textBox3.Text = value.ToString(CultureInfo.InvariantCulture); }
        }

        public string TimeZone
        {
            get { return comboBox4.SelectedItem.ToString(); }
            set { comboBox4.SelectedItem = value; }
        }

        public User.Gender Sex
        {
            get
            {
                if (rdMale.Checked)
                    return User.Gender.Male;
                else
                    return User.Gender.Female;
            }
            set
            {
                if (value == User.Gender.Male)
                    rdMale.Checked = true;
                else
                    rdFamele.Checked = true;
            }
        }

        public string Month
        {
            get { return comboBox5.SelectedItem.ToString(); }
            set { comboBox5.SelectedItem = value; }
        }

        public string Day
        {
            get { return comboBox6.SelectedItem.ToString(); }
            set { comboBox6.SelectedItem = value; }
        }

        public int Year
        {
            get { return int.Parse(textBox4.Text); }
            set { textBox4.Text = value.ToString(CultureInfo.InvariantCulture); }
        }

        public string Occupation
        {
            get { return comboBox7.SelectedItem.ToString(); }
            set { comboBox7.SelectedItem = value; }
        }

        public event System.EventHandler Create;

        private void button1_Click(object sender, System.EventArgs e)
        {

            if (FirstName == "" || LastName == "")
                MessageBox.Show("Please enter your name");
            else
            {
                sqlConnection1.Open();

                string insert = "INSERT INTO REGISTER(FNAME, LNAME, LANG, COUNTRY, STATE, ZIPCODE, TIMEZONE, GENDER, BDAY, BMONTH, BYEAR, OCCUPATION) " +
                                "VALUES ('" + FirstName + "','" + LastName + "','"
                                + Language + "','" + Country + "','"
                                + State + "','" + Zipcode
                                + "','" + TimeZone + "','"
                                + Sex + "','" + Day + "','"
                                + Month + "','" + Year + "','"
                                + Occupation + "')";
                SqlCommand cmd = new SqlCommand(insert, sqlConnection1);
                cmd.ExecuteNonQuery();

                sqlConnection1.Close();
                MessageBox.Show("You have been successfully registered to our database.");
            }

            if (Create != null)
            {
                Create(this, EventArgs.Empty);
            }
        }

    }
}

IUserView:

using System;
using MVCwithDBFormReg.Model;

namespace MVCwithDBFormReg.View
{
    public interface IUserView
    {
        string FirstName  { get; set; }
        string LastName   { get; set; }
        string Language   { get; set; }
        string Country    { get; set; }
        string State      { get; set; }

        int Zipcode       { get; set; }

        string TimeZone   { get; set; }

        User.Gender Sex   { get; set; }

        string Month      { get; set; }
        string Day        { get; set; }
        int Year       { get; set; }
        string Occupation { get; set; }

        event EventHandler Create;

    }
}
share|improve this question
Your question is off topic here. At this site we strictly work to improve working code. If you have a specific question you can ask on Stack Overflow. – Winston Ewert Sep 4 '12 at 16:07
@linguini I am working on refactoring a windows forms application and would be interested to share ideas about a MVC approach on Windows Forms. Do you have an example project, that you would like to share? – threeFourOneSixOneThree May 16 at 10:00
@threeFourOneSixOneThree: codeproject.com/Articles/383153/… This is project I have used when i started learning about MVC Winforms. There are various suggestions. Here is my project : sdrv.ms/16HLfad – linguini May 19 at 13:47

closed as off topic by svick, Corbin, Jeff Vanzella, Winston Ewert Sep 4 '12 at 16:07

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

Two points:

  • You should consider moving your database code into a separate class altogether and then calling that class from within the controller.
  • It is highly recommended that you do not build your SQL text by concatenating strings. You set yourself up for SQL injection attacks. Even if this is just a simple throwaway project, it is good to get into the habit of using parameterized queries.
share|improve this answer

If your intent on learning MVC then I'd highly recommend using Entity Framework to, I'm currently doing it and the two go hand in hand quite nicely... Have a quick look on youtube... search for Entity Framework and watch some of the vids on there, massively simplifies working with a database.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.