Hi,
I am posting my code.Can you please check once. Still i am getting same error.
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
BindMonth();
GetCurrentMonth();
DataTable dt = Griddata();
if (dt != null)
{
DataTable dtcamltest = dt;
DataView dtview = new DataView(dtcamltest);
DataTable dtdistinct = dtview.ToTable(true, "UserName", "V_FirtsName", "V_LastName");
if (dtdistinct != null)
{
ViewState["Data"] = dtdistinct;
GvBindData.DataSource = dtdistinct;
GvBindData.DataBind();
}
}
}
}
catch(Exception ex)
{
Response.Write(ex);
}
}
private void GetCurrentMonth()
{
DateTime dt = DateTime.Now;
string month = DateTime.Now.ToString("MMMM");
int year = dt.Year;
ddlMonth.SelectedItem.Text = month + " " + year;
}
decimal MonthlyHours=0;
decimal MonthlyTotal=0;
decimal total;
decimal yearlyTotal = 0;
protected void GvBindData_RowDataBound(object sender, GridViewRowEventArgs e)
{
string ddlvalue = ddlMonth.SelectedItem.Text.Trim();
if (e.Row.RowType == DataControlRowType.DataRow)
{
string userName = ((System.Web.UI.WebControls.Label)e.Row.FindControl("lblUserName")).Text;
Label lbfirstname = ((System.Web.UI.WebControls.Label)e.Row.FindControl("fstname"));
Label lblastName = ((System.Web.UI.WebControls.Label)e.Row.FindControl("lstname"));
Label lblHours = ((System.Web.UI.WebControls.Label)e.Row.FindControl("HOURS"));
string s = ((System.Web.UI.WebControls.Label)e.Row.FindControl("HOURS")).Text;
string ID = ((System.Web.UI.WebControls.Label)e.Row.FindControl("lblid")).Text;
string Month = ((System.Web.UI.WebControls.Label)e.Row.FindControl("lblMonth")).Text;
DataTable dtUsers = GetMonthHours(userName, ddlvalue);
if(dtUsers!=null)
{
if(dtUsers.Rows.Count>0)
{
for(int i=0;i<dtUsers.Rows.Count;i++)
{
MonthlyHours +=Convert.ToDecimal( dtUsers.Rows[i]["Hours"]);
}
//lbfirstname.Text = dtUsers.Rows[0]["V_FirtsName"].ToString();
// lblastName.Text = dtUsers.Rows[0]["V_LastName"].ToString();
}
}
lblHours.Text = MonthlyHours.ToString();
MonthlyTotal += MonthlyHours;
MonthlyHours = 0;
string dept = "Docent Speakers Bureau";
string month = ddlMonth.SelectedItem.Text.Trim();
System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnDetails");
btnEdit.PostBackUrl = "/Pages/Details.aspx?CustomID=" + userName + "&Department=" + dept + "&Month=" + month;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
System.Web.UI.WebControls.Label lblTotal = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblTotal");
lblTotal.Text = MonthlyTotal.ToString();
System.Web.UI.WebControls.Label lblYearly = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblYearly");
string s = ddlMonth.SelectedItem.Text;
string[] year = s.Split(' ');
if (!string.IsNullOrEmpty(year[1].ToString()))
{
DataTable dtyear = GetYearlyData(year[1].ToString());
if (dtyear != null)
{
if (dtyear.Rows.Count > 0)
{
foreach (DataRow dr in dtyear.Rows)
{
yearlyTotal += Convert.ToDecimal(dr["Hours"]);
}
}
}
}
lblYearly.Text = yearlyTotal.ToString();
}
}
private void BindMonth()
{
int Current_year;
Current_year = DateTime.Now.Year;
for (int i = 1999; i <= Current_year; i++)
{
for (int j = 1; j <= 12; j++)
{
if (j == 1)
{
ListItem LI = new ListItem("January " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 2)
{
ListItem LI = new ListItem("Feburary " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 3)
{
ListItem LI = new ListItem("March " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 4)
{
ListItem LI = new ListItem("April " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 5)
{
ListItem LI = new ListItem("May " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 6)
{
ListItem LI = new ListItem("June " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 7)
{
ListItem LI = new ListItem("July " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 8)
{
ListItem LI = new ListItem("August " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 9)
{
ListItem LI = new ListItem("September " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 10)
{
ListItem LI = new ListItem("October " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 11)
{
ListItem LI = new ListItem("November " + i.ToString());
ddlMonth.Items.Add(LI);
}
if (j == 12)
{
ListItem LI = new ListItem("December " + i.ToString());
ddlMonth.Items.Add(LI);
}
}
}
}
private DataTable Griddata()
{
DataTable dt = new DataTable();
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["TimeSheet"];
string ddvalue = ddlMonth.SelectedItem.Text;
SPQuery query = new SPQuery();
query.Query = "<Where><And><Eq><FieldRef Name='Month_x0026_Year'/><Value Type='Text'>" + ddvalue.Trim() + "</Value></Eq><Eq><FieldRef
Name='Department'/><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And></Where>";
query.ViewFields = string.Concat("<FieldRef Name='V_FirtsName'/>",
"<FieldRef Name='V_LastName'/>",
"<FieldRef Name='UserName'/>",
"<FieldRef Name='Hours'/>", "<FieldRef Name='Month_x0026_Year'/>");
dt = list.GetItems(query).GetDataTable();
if (dt != null)
{
if (dt.Rows.Count > 0)
{
}
}
}
}
return dt;
}
private DataTable GetHours(string username)
{
DataTable dt;
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList listTime = web.Lists["TimeSheet"];
string ddvalue = ddlMonth.SelectedItem.Text;
SPQuery query = new SPQuery();
query.Query = "<Where><And><Eq><FieldRef Name='UserName'/><Value Type='Text'>" + username.Trim() + "</Value></Eq><Eq><FieldRef
Name='Department' /><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And></Where>";
query.ViewFields = string.Concat(
"<FieldRef Name='Hours'/>");
dt = listTime.GetItems(query).GetDataTable();
}
}
return dt;
}
protected void btnSubmuit_Click(object sender, EventArgs e)
{
try
{
GvBindData.DataSource = null;
GvBindData.DataBind();
DataTable dt = Griddata();
if (dt != null)
{
DataTable dtcamltest = dt;
DataView dtview = new DataView(dtcamltest);
DataTable dtdistinct = dtview.ToTable(true, "UserName", "V_FirtsName", "V_LastName");
if (dtdistinct != null)
{
GvBindData.DataSource = dtdistinct;
GvBindData.DataBind();
}
}
}
catch(Exception ex)
{
Response.Write(ex);
}
}
private DataTable GetYearlyData(string year)
{
DataTable dt = null;
using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb oweb = osite.OpenWeb())
{
SPList olist = oweb.Lists.TryGetList("TimeSheet");
if (olist != null)
{
SPQuery oquery = new SPQuery();
oquery.Query = "<Where><And><Eq><FieldRef Name='Year' /><Value Type='Text'>" + year + "</Value></Eq><Eq><FieldRef
Name='Department' /><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And></Where>";
dt = olist.GetItems(oquery).GetDataTable();
}
}
}
return dt;
}
private DataTable GetSelectedMonthData()
{
DataTable dt = new DataTable();
string ddvalue = ddlMonth.SelectedItem.Text;
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList listTime = web.Lists["TimeSheet"];
SPQuery query = new SPQuery();
query.Query = "<Where><And><Eq><FieldRef Name='Month_x0026_Year'/><Value Type='Text'>" + ddvalue + "</Value></Eq><Eq><FieldRef
Name='Department'/><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And></Where>";
string.Concat("<FieldRef Name='Hours'/>",
"<FieldRef Name='V_LastName'/>",
"<FieldRef Name='V_FirtsName'/>");
dt = listTime.GetItems(query).GetDataTable();
}
}
return dt;
}
private DataTable GetMonthHours(string username,string ddlvalue)
{
DataTable dtuser;
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList listTime = web.Lists["TimeSheet"];
string ddvalue = ddlMonth.SelectedItem.Text;
SPQuery query = new SPQuery();
query.Query = "<Where><And><And><Eq><FieldRef Name='UserName'/><Value Type='Text'>" + username.Trim() + "</Value></Eq><Eq><FieldRef
Name='Department' /><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And><Eq><FieldRef Name='Month_x0026_Year'/><Value Type='Text'>" + ddlvalue + "</Value></Eq></And></Where>";
dtuser = listTime.GetItems(query).GetDataTable();
}
}
return dtuser;
}
protected void GvBindData_Sorting(object sender, GridViewSortEventArgs e)
{
string ddvalue1 = ddlMonth.SelectedItem.Text;
string sortingDirection = string.Empty;
if (sorDirection == SortDirection.Ascending)
{
sorDirection = SortDirection.Descending;
sortingDirection = "Desc";
}
else
{
sorDirection = SortDirection.Ascending;
sortingDirection = "Asc";
}
;
DataTable table = (DataTable)ViewState["Data"];
table.DefaultView.Sort = e.SortExpression + " " + sortingDirection;
GvBindData.DataSource = table;
GvBindData.DataBind();
}
public SortDirection sorDirection
{
get
{
if (ViewState["dirState"] == null)
{
ViewState["dirState"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["dirState"];
}
set
{
ViewState["dirState"] = value;
}
}
Can u suggest me how to fix that issue....