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>