Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,

Please help me. I am moving from Web Forms to Windows Forms and i have being using Viewstate and LINQ to SQL to connect to the database.I tried to use viewstate on windows forms and it throws errors. i have no idea on how can i change my class so that i works fine with windows forms.

Please help me on how can i modifies my class since i wont be using Web Forms, see my code below
public class Degree
{
    private MCX_COLLEGE_Data_ClassesDataContext _ds = new MCX_COLLEGE_Data_ClassesDataContext();
    private string Error = string.Empty;
    private int i = 0;

    public Degree()
    {
    }

    public string Error_Exception
    {
        get
        {
            return Error;
        }
        set
        {
            Error = value;
        }
    }
    public IList<degree> GetDegree()
    {
        return _ds.DEGREEs.ToList();
    }
    public IList<degree> GetDegree(int DegreeID)
    {
        IList<degree> _DegreeList = _ds.DEGREEs.ToList();

        var _List = from P in _ds.DEGREEs
                    where P.DegreeID == DegreeID
                    select P;

        if (_List.Count() > 0)
        {
            _DegreeList = _List.ToList();
        }
        return _DegreeList;
    }

    public int Save(int ParID, DEGREE _degreetbl)
    {

        using (var MyTran = new TransactionScope())

            try
            {
                _ds.Connection.Open();
                _ds.SP_DEGREE
                    (
                      ParID,
                     _degreetbl.DegreeID,
                     _degreetbl.Description,
                     _degreetbl.Duration_Years
                     );

                _ds.SubmitChanges();
                MyTran.Complete();
            }
            catch (Exception ex)
            {
                _ds.Connection.Close();
            }
         return ParID;

-----------------------------------------------------------------------

Code for main form
-----------------
private Degree _d = new Degree();
     private DEGREE _degtbl = new DEGREE();
     private int _Results = 0;

     private Enum_Class _enum = new Enum_Class();

     private string _ApplicationMessage = string.Empty;

     protected void Page_Load(object sender, EventArgs e)
     {
         if (!Page.IsPostBack)
         {
             ViewState["PageMode"] = "Save";
             this.Populate();
         }

     }


     public void Populate()
     {
         grdDegree.DataSource = _d.GetDegree();
         grdDegree.DataBind();
     }
     public int Save()
     {
         _degtbl.DegreeID = 0;
         _degtbl.Description = txtDescription.Text;
         _degtbl.Duration_Years = Convert.ToInt32(txtDuration.Text);
         return _d.Save((int)Enum_Class.SQL_Parameters.Save, _degtbl);
     }
     public int Update()
     {
         _degtbl.DegreeID = int.Parse(ViewState["DegreeID"].ToString());
         _degtbl.Description = txtDescription.Text;
         _degtbl.Duration_Years = int.Parse(txtDuration.Text);
         return _d.Save((int)Enum_Class.SQL_Parameters.Update, _degtbl);
     }
     public int Delete()
     {
         _degtbl.DegreeID = int.Parse(ViewState["DegreeID"].ToString());
         _degtbl.Description = txtDescription.Text;
         _degtbl.Duration_Years = int.Parse(txtDuration.Text);

         return _d.Save((int)Enum_Class.SQL_Parameters.Delete, _degtbl);
     }


     protected void btnSubmit(object sender, EventArgs e)
     {
         switch (ViewState["PageMode"].ToString())
         {
             case "Save":

                 switch(this.Save())
                 {
                     case 1:
                         _ApplicationMessage = _d.Error_Exception;
                         lblError.Text = _ApplicationMessage;
                         lblMessage.Visible = true;
                         break;

                     case 0:
                         _ApplicationMessage = "Degree sucessfully submited";
                         lblMessage.Text = _ApplicationMessage;
                         lblMessage.Visible = true;

                         this.NoDuplicate();
                         break;


                 }
                 break;

             case "Upade":

                 switch(this.Update())
                 {
                     case 1:
                         _ApplicationMessage = _d.Error_Exception;
                         lblError.Text = _ApplicationMessage;
                         lblError.Visible = true;
                         break;

                     case 0:
                         _ApplicationMessage = "Degree sucessfully updated submited";
                         lblMessage.Text = _ApplicationMessage;
                         lblMessage.Visible = true;

                         this.NoDuplicate();
                         break;
                 }
                 break;

             case "Delete":

                 switch (this.Delete())
                 {
                     case 1:
                         _ApplicationMessage = _d.Error_Exception;
                         lblError.Text = _ApplicationMessage;
                         lblError.Visible = true;
                         break;

                     case 0:
                         _ApplicationMessage = "Degree Deleted";
                         lblMessage.Text = _ApplicationMessage;
                         lblMessage.Visible = true;
                         break;
                 }
                 break;

         }
         this.Populate();
     }

     protected void btnDelete(object sender, EventArgs e)
     {
         txtDescription.Text = "";
         txtDuration.Text = "";
     }
     public void NoDuplicate()
     {
         txtDescription.Text = "";
         txtDuration.Text = "";
         this.Populate();
         ViewState["PageMode"] = "Save";
     }

     protected void grdDegree_SelectedIndexChanged(object sender, EventArgs e)
     {
         GridViewRow _Rows = grdDegree.SelectedRow;
         string str = _Rows.Cells[0].Text;
         ViewState["DegreeID"] = str;

         IList<degree> _Edit = _d.GetDegree(int.Parse(str));

         if(_Edit != null)
         {
             foreach (DEGREE _degreeList in _Edit)
             {
                 ViewState["DegreeID"] = _degreeList.DegreeID.ToString();
                 txtDescription.Text = _degreeList.Description;
                 txtDuration.Text = _degreeList.Duration_Years.ToString();

             }
         }
         ViewState["PageMode"] = "Update";
     }</degree></degree></degree></degree>
Posted
Updated 19-Aug-14 2:12am
v2
Comments
[no name] 19-Aug-14 8:19am    
A Winform application does not know anything about or need a viewstate. You need to rewrite your UI code.

1 solution

Win Forms programming uses a completely different approach to Web Forms.

With web forms the client code (HTML/Browser page) is detached from the Server Side (Application code) so ASP.Net uses the View State and Session State to manage application state. This problem is made worse by implementing Web Gardening as you cannot guarantee to get the same process for each subsequent request.

This is not an issue with Win Forms. The Form is attached to the application code and runs as a single process. So no view state and no session state are required.

To persist information in a Win Forms application you need simply assign the object to a reference which can be retrieved when required.

In Web Forms the page is constructed with every post back. With Win Forms the form is only constructed once and actions are generally an event on the form.

The purpose of the View State is to persist the state of the Web Form when the page is reconstructed after a post back. Because with Win Forms there is only one instance of the form which persists until closed, there is no need for a View State.

There are lots of other differences in how you should approach to design too.
 
Share this answer
 
Comments
Nkhanedzeni 20-Aug-14 4:21am    
Thanks for your reply and now am starting to understand...can you please give me some good references like links which i can learn to create win forms class...Thanks
Stephen Hewison 20-Aug-14 5:54am    
Just search for this in google:

introduction to win forms development

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