Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:02:37+00:00 2026-06-10T03:02:37+00:00

I want to add a record into a DB using MVC pattern winforms. The

  • 0

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.");
           }
       }
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:02:39+00:00Added an answer on June 10, 2026 at 3:02 am

    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 Save button 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:

    private readonly User _model;
    public Form1(User model)
    {
        _model = model;
        InitializeComponent();
    }
    
    public event CreateUserHandler Create; // should be changed to use User 
    //Function to create a new record into the DB
    private void btnSave_click(object sender, System.EventArgs e)
    {
        // if model is not bound via binding, it needs update before Create.
        Create(_model); // needs changes to current EventHandler
    }
    

    Model – no changes;
    UserRegister – dies.

    Somewhere (like in Delegates.cs seperate file):

    public delegate void CreateUserHandler(User model);
    

    Controller:

    public UserController(IUserView view)
    {
        _view = view;
        _view.Create += ViewOnSave;
    }
    
    private void ViewOnSave(User model)
    {
        SqlConnection connection = new SqlConnection(connString);
        connection.Open();
        //... save query goes here
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've following Grid to display data, now when I want to add new record
When I add a new record I want SQL Server to automatically add a
I want add UIGestureRecognizerDelegate to UIWebView,but failed. if [self.view addsubView:webView]; So UIWebView is ok,but
I have a website built with ASP.NET (3.5) and want add some level of
I want to add only unique records into a database table. Is there any
I have some code where I am using a Linq-To-SQL DataContext to add and
class Post< ActiveRecord::Base end post_array = Post.first If I want to add some data
In a stored procedure (Oracle in my case), I want to add some values
I m using MVC Contrib grid in the application , i want to change
I want to be able to add up to 10 tags to a record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.