protected void OnPaging(object sender, GridViewPageEventArgs e) { OnClick_getReport(sender, e); GridViewID_Report.Visible = true; GridViewID_Report.PageIndex = e.NewPageIndex; GridViewID_Report.DataBind(); }
protected void OnClick_getReport(object sender, EventArgs e) { try { string strStartDate = start.Value; string strEndDate = end.Value; string strResultName = DropDownListID_PersonName.Text; con.Open(); if (strResultName == string.Empty) { SqlCommand sqlcommand = new SqlCommand("SELECT EntryDate,Name,Period,Amount from FoodEntry Where EntryDate between '" + strStartDate + "' and '" + strEndDate + "' ", con); SqlDataAdapter sqlDA = new SqlDataAdapter(sqlcommand); DataSet ds = new DataSet(); sqlDA.Fill(ds, "FoodEntry"); DataTable dtFoodEntry = new DataTable(); dtFoodEntry = ds.Tables["FoodEntry"]; DataTable dtCloneTable = dtFoodEntry.Clone(); int nCount = 0; foreach (DataRow dr in dtFoodEntry.Rows) { object[] objData = dr.ItemArray; for (int i = 0; i < objData.Length; i++) { DateTime date; var val = objData[i].ToString(); if (DateTime.TryParse(val, out date)) { date = Convert.ToDateTime(val); string strDate = date.ToString("dd/MM/yyyy"); val = strDate; if (nCount == 0) { dtCloneTable.Columns[i].DataType = typeof(string); } objData[i] = val.ToString(); dtCloneTable.Rows.Add(objData); } } } con.Close(); if (ds.Tables["FoodEntry"].Rows.Count > 0) { GridViewID_Report.DataSource = dtCloneTable; GridViewID_Report.DataBind(); con.Open(); SqlCommand sqlcmd = new SqlCommand("Select SUM(Amount) from FoodEntry where EntryDate between '" + strStartDate + "' and '" + strEndDate + "' ", con); SqlDataReader sqlreader = sqlcmd.ExecuteReader(); sqlreader.Read(); LabelID_TotalAmount.Text = "OverallTotal = ₹" + sqlreader[0].ToString() + " "; LabelID_TotalAmount.Visible = true; string strUserName = Session["UserName"].ToString(); if (strUserName == "phoenix") { SendPeriodReport.Visible = false; SendMonthlyReport.Visible = false; } else if (strUserName == "admin") { SendPeriodReport.Visible = true; SendMonthlyReport.Visible = false; } con.Close(); } } else { SqlCommand sqlCommand = new SqlCommand("SELECT EntryDate,Name,Period,Amount from FoodEntry Where Name = '" + strResultName + "' and EntryDate between '" + strStartDate + "' and '" + strEndDate + "' ", con); SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCommand); DataSet ds = new DataSet(); sqlDA.Fill(ds, "FoodEntry"); DataTable dtFoodEntry = new DataTable(); dtFoodEntry = ds.Tables["FoodEntry"]; DataTable dtCloneTable = dtFoodEntry.Clone(); int nCount = 0; foreach (DataRow dr in dtFoodEntry.Rows) { object[] objData = dr.ItemArray; for (int i = 0; i < objData.Length; i++) { DateTime date; var val = objData[i].ToString(); if (DateTime.TryParse(val, out date)) { date = Convert.ToDateTime(val); string strDate = date.ToString("dd/MM/yyyy"); val = strDate; if (nCount == 0) { dtCloneTable.Columns[i].DataType = typeof(string); } objData[i] = val.ToString(); dtCloneTable.Rows.Add(objData); } } } con.Close(); if (ds.Tables["FoodEntry"].Rows.Count > 0) { GridViewID_Report.DataSource = dtCloneTable; GridViewID_Report.DataBind(); con.Open(); SqlCommand sqlCmd = new SqlCommand("Select SUM(Amount) from FoodEntry where Name = '" + strResultName + "' and EntryDate between '" + strStartDate + "' and '" + strEndDate + "'", con); SqlDataReader sqlReader = sqlCmd.ExecuteReader(); sqlReader.Read(); LabelID_TotalAmount.Text = sqlReader[0].ToString(); LabelID_TotalAmount.Text = " " + strResultName + "'s Total = ₹" + sqlReader[0].ToString() + " "; LabelID_TotalAmount.Visible = true; string steUserName = Session["UserName"].ToString(); if (steUserName == "phoenix") { SendPeriodReport.Visible = false; SendMonthlyReport.Visible = false; } else if (steUserName == "admin") { SendPeriodReport.Visible = true; SendMonthlyReport.Visible = false; } SendMonthlyReport.Visible = false; } else { GridViewID_Report.Visible = false; ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Enter Valid Details');" + "window.location ='Report.aspx';", true); } } } catch (Exception ex) { if ("String was not recognized as a valid DateTime." == ex.Message) { ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Enter the valid Date');" + "window.location ='Report.aspx';", true); } else { ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Enter the name');" + "window.location ='Report.aspx';", true); } } finally { con.Close(); } }