Click here to Skip to main content
15,886,079 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys I'm working in small project where i created main form and strip-menu which will display the user control within the main form without displaying various form.... i have created a model
C#
class Model
    {
        private string connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        //"Data Source=mysource;Initial Catalog=myCatalogu;Integrated Security=True";
        private SqlConnection conn = new SqlConnection();
        public Model()
        {
            conn = new SqlConnection(connectionString);
        }
        public DataSet GetSingleRoute(int id)
        {
            DataSet dataSet = new DataSet();
            string query = string.Format("SELECT * FROM routes WHERE id = {0}", id);
            conn.Open();

            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dataSet);
            }

            conn.Close();
            return dataSet;
        }

        //Retrieve all routes from the database
        public DataTable GetAllRoutes()
        {
            DataSet dataSet = new DataSet();
            string query = "SELECT * FROM routes";
            conn.Open();

            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dataSet);
            }

            conn.Close();
            return dataSet.Tables[0];
        }

        #region ManipulateRoutes

        //Add a new route
        public void InsertRoute2(Route route)
        {
            string routecode = route.RouteCode;
            string vehiclecode = route.VehicleCode;
            string firststudentname = route.FirstStudentName;


            using (SqlCommand cmd = new SqlCommand("InsertRoute2", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@RouteCode", routecode));
                cmd.Parameters.Add(new SqlParameter("@vehicleCode", vehiclecode));
                cmd.Parameters.Add(new SqlParameter("@FirstStudentName", firststudentname));


                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

        //Update existing route
        public void UpdateRoute(int id, Route route)
        {

        }

        //Delete a route
        public void DeleteRoute(int id)
        {

        }
        #endregion
    }

a controller
C#
//Controller class: This class makes decisions, calculations and gives orders to both Views and Models
    class Controller
    {
        //Only a controller can access a Model
        private Model model;

        //Constructor
        public Controller()
        {
            model = new Model();
        }

        /*-    Retrieve a dataset from the Model.
        * -    Convert it to a movie-object
        * -    Send it to the view */
        public Route GetSingleRoute(int id)
        {
            DataSet routeData = model.GetSingleRoute(id);
            DataRow dRow = routeData.Tables[0].Rows[0];

            string routecode = dRow.ItemArray[1].ToString();
            string vehiclecode = dRow.ItemArray[2].ToString();
            string firststudentname = dRow.ItemArray[3].ToString();
          

            Route route= new Route(routecode, vehiclecode, firststudentname);
            return route;
        }

        //Get all Database data from the model and sent it to the view
        public DataTable GetAllMovies()
        {
            return model.GetAllRoutes();
        }

        //Creates a movie object based on user input in the UCRouteCreate
        public Route CreateRouteFromText(UCRouteCreate form)
        {
            
            string routecode =form.txtRouteCode.Text;
            string vehiclecode = form.txtVehicleCode.Text;
            string firststudentname = form.txtFStudent.Text;
           

            Route route = new Route(routecode, vehiclecode, firststudentname);
            return route;
        }


        public void InsertRoute2(Route route)
        {
            model.InsertRoute2(route);
        }
        //Update existing route
        public void UpdateRoute(int id, Route route)
        {
            model.UpdateRoute(id, route);
        }

        //Delete a route
        public void DeleteRoute(int id)
        {
            model.DeleteRoute(id);
        }


    }

and a class for setter and geter..

C#
class Route
  {
      UCRouteCreate ucOnt = new UCRouteCreate();
      public string RouteCode
      {
          get
          {
              return ucOnt.txtRouteCode.Text;
          }
          set
          {
              ucOnt.txtRouteCode.Text = value;
          }
      }
          // { get; set; }
      public string VehicleCode { get; set; }

      public string FirstStudentName { get; set; }

      public Route(string routecode, string vehiclecode, string firststudentname)
      {

          RouteCode = routecode;
          VehicleCode = vehiclecode;
          FirstStudentName = firststudentname;


      }
  }

and a button event handler to perform data insertion from user control text value entered by users
C#
private void btnCreateRoute_Click_1(object sender, EventArgs e)
       {
           UCRouteCreate uc = new UCRouteCreate();

          // DialogResult dresult = DialogResult.OK;// the show dialogue() function not working
          // if (dresult == DialogResult.OK)
          // {
               Route route = controller.CreateRouteFromText(uc);
               controller.InsertRoute2(route);
           //}


       }


i have textbox called txtRoue i want to get it's value from user control textbox and i have use the class Route to get the setter and getter but when i click on button to fire event handler i got txtroute value as ""..any idea why my user control txtbox value hasn't been captured..
if you need code will submit..
ps. i'm a first timer here so patient if not follwed the rule of posting
Posted
Updated 27-May-13 1:29am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900