Need help in MVC working example

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p style="font:inherit; vertical-align:baseline; list-style-type:none; color:#666666; font-family:Segoe UI,Helvetica,Garuda,Arial,sans-serif; line-height:21px
I want to learn MVC pattern using winforms and I have read some tutorial to understand. Now, im working on my own example to just add a customer records in a database using MVC pattern.
<p style="font:inherit; vertical-align:baseline; list-style-type:none; color:#666666; font-family:Segoe UI,Helvetica,Garuda,Arial,sans-serif; line-height:21px
The problem, I have a delegate method to add a record into the db that calls from the controller to the model. I got stuck at the Model and also in the UserInterface.
<p style="font:inherit; vertical-align:baseline; list-style-type:none; color:#666666; font-family:Segoe UI,Helvetica,Garuda,Arial,sans-serif; line-height:21px
Somebody please help me..thank you.
<p style="font:inherit; vertical-align:baseline; list-style-type:none; color:#666666; font-family:Segoe UI,Helvetica,Garuda,Arial,sans-serif; line-height:21px
Model:
<pre class="prettyprint" style=" 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();
}
}
}[/code]
Controller:
<pre class="prettyprint" style=" 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();
}
}
}[/code]
<br/>

View:
<pre class="prettyprint" style=" 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);
}
}

}
}[/code]
IUserView:
<pre class="prettyprint 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;

}
}
[/code]
<br/>


<p style="font:inherit; vertical-align:baseline; list-style-type:none; color:#666666; font-family:Segoe UI,Helvetica,Garuda,Arial,sans-serif; line-height:21px

<br/>


View the full article
 
Back
Top