I want to add a record into a DB using MVC pattern winforms. The function to add a record into the DB is working but I want to do it by using the object method.
Some one please what is the mistake i’m doing here with the MVC pattern?
View:
using System;
using System.Data.SqlClient;
using System.Globalization;
using System.Windows.Forms;
using MVCwithDBFormReg.Controller;
using MVCwithDBFormReg.Model;
namespace MVCwithDBFormReg.View
{
public partial class Form1 : Form, IUserView
{
private readonly User _model;
public Form1(User model)
{
_model = model;
InitializeComponent();
}
public event CreateUserHandler Create;
private void button3_Click(object sender, System.EventArgs e)
{
_model.FirstName = FirstName;
_model.LastName = LastName;
_model.Language = Language;
_model.Country = Country;
_model.State = State;
_model.Zipcode = Zipcode;
_model.TimeZone = TimeZone;
_model.Sex = Sex;
_model.Month = Month;
_model.Day = Day;
_model.Year = Year;
_model.Occupation = Occupation;
Create(_model);
}
}
}
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; }
}
Controller:
namespace MVCwithDBFormReg.Controller
{
public class UserController
{
private SqlConnection sqlConnection1;
private void ViewOnSave(User model)
{
if (model.FirstName == "" || model.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 ('" + model.FirstName + "','" + model.LastName + "','"
+ model.Language + "','" + model.Country + "','"
+ model.State + "','" + model.Zipcode
+ "','" + model.TimeZone + "','"
+ model.Sex + "','" + model.Day + "','"
+ model.Month + "','" + model.Year + "','"
+ model.Occupation + "')";
SqlCommand cmd = new SqlCommand(insert, this.sqlConnection1);
cmd.ExecuteNonQuery();
sqlConnection1.Close();
MessageBox.Show("You have been successfully registered to our database.");
}
}
}
}
The thing is not technically fully MVC (and I doubt there’s much meaning to MVC in WinForms projects, MVVM pattern has prooved itself to be more useful. Though I may be wrong since there are special cases where MVC makes sense).
First of all, your model class is not UserRegister, it’s User. Secondly, you don’t necessarily need the UserRegister class, since you can do saving in controller (not really a good practice, but simple enough for training purposes). Or if you really want to separate database actions from controller code, you could something handy like Repository pattern(It’s like UserRegister, UserLoad, UserFind classes in one Repository class). I’d recommend reading some Martin Fowler books on architecture and design (or just internet tutorials).
Now that we have understanding what your model is (pure data) – User. Your view shouldn’t have any logic (like saving data to DB or calculating the discounts for teddy bears) – only view. View is bound to model, meaning it shows what model has, and sets model’s properties through controls. Like – you have a textbox for user name, it shows data from Model.Name and sets Model.Name whenever it’s changed.
Whenever
Savebutton is clicked, your view sends your controller the changed model so that it could act upon it. Controller then pieces things together – like asks your UserRegister to save the User it got from the View. Btw, no need for events here, though you could use them.I’ll try to show your code changed in a way that supports everything said above:
View:
Model – no changes;
UserRegister – dies.
Somewhere (like in Delegates.cs seperate file):
Controller:
Notice, repository isn’t used here. Anyway, this MVC is still incomplete and buggy, I’d recommend reading some tutorials on ASP.NET MVC to get a hold on MVC pattern in .NET environment.