Dear Sir,
i am stack to solve mention error. please help me sir.
Error:-
Unable to cast object of type 'System.Int32' to type 'System.String'.
Server Error in '/' Application.
Unable to cast object of type 'System.Int32' to type 'System.String'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'. Source Error:
Line 163: //decimal total = Convert.ToDecimal(dt.Rows[i][2].ToString());
Line 164: decimal deviation, near_miss, first_Aid, fire, non_Reportable, reportable, fatal;
Line 165: deviation = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Deviation")) ? row.Field<string>("Deviation") : "0"));
Line 166: near_miss = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Near miss")) ? row.Field<string>("Near miss") : "0"));
Line 167: first_Aid = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("First Aid")) ? row.Field<string>("First Aid") : "0"));
|
Source File: c:\Users\Administrator\Documents\Visual Studio 2012\Projects\sfty_Lead_lag\sfty_Lead_lag\dept_report1.aspx.cs Line: 165
namespace sfty_Lead_lag
{
public partial class dept_report1 : System.Web.UI.Page
{
Decimal dPageTotal;
string strr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
Session["Department"] = null;
Session["Month"] = null;
Session["Lag"] = null;
Session["Lead"] = null;
if (!IsPostBack)
{
int currentYear = DateTime.Today.Year;
ddl_Year.Items.Clear();
// ddl_Year.Items.Add("Select");
for (int i = 0; i >= 0; i--)
{
int fy = currentYear - i;
int fy1 = fy + 1;
if (DateTime.Now.Date > Convert.ToDateTime(fy + "-03-31").Date)
{
ddl_Year.Items.Add(fy.ToString() + "-" + fy1.ToString());
}
}
DateTime currentDate = DateTime.Now;
List<ListItem> items = new List<ListItem>();
items.Add(new ListItem
{
Text = currentDate.AddMonths(-1).Month.ToString(),
Value = currentDate.AddMonths(-1).Month.ToString()
});
items.Add(new ListItem
{
Text = currentDate.Month.ToString(),
Value = currentDate.Month.ToString()
});
ddl_Month.DataSource = items;
ddl_Month.DataTextField = "Text";
ddl_Month.DataValueField = "Text";
ddl_Month.DataBind();
}
if (!this.IsPostBack)
{
BindGrid("", "");
}
}
private void BindGrid(string Month, string year)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("ld_lg_count3", con))
{
if (!string.IsNullOrEmpty(Month) && !string.IsNullOrEmpty(year))
{
cmd.Parameters.AddWithValue("@Month", Month);
cmd.Parameters.AddWithValue("@year", year);
}
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.Visible = true;
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
//decimal total = Convert.ToDecimal(dt.Rows[i][2].ToString());
decimal deviation, near_miss, first_Aid, fire, non_Reportable, reportable, fatal;
deviation = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Deviation")) ? row.Field<string>("Deviation") : "0"));
near_miss = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Near miss")) ? row.Field<string>("Near miss") : "0"));
first_Aid = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("First Aid")) ? row.Field<string>("First Aid") : "0"));
fire = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Fire")) ? row.Field<string>("Fire") : "0"));
non_Reportable = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Non-Reportable")) ? row.Field<string>("Non-Reportable") : "0"));
reportable = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Reportable")) ? row.Field<string>("Reportable") : "0"));
fatal = dt.AsEnumerable().Sum(row => Convert.ToDecimal(!string.IsNullOrEmpty(row.Field<string>("Fatal")) ? row.Field<string>("Fatal") : "0"));
GridView1.FooterRow.Cells[0].Text = "Total";
GridView1.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Right;
GridView1.FooterRow.Cells[1].Text = deviation.ToString("N0");
GridView1.FooterRow.Cells[2].Text = near_miss.ToString("N0");
GridView1.FooterRow.Cells[3].Text = first_Aid.ToString("N0");
GridView1.FooterRow.Cells[4].Text = fire.ToString("N0");
GridView1.FooterRow.Cells[5].Text = non_Reportable.ToString("N0");
GridView1.FooterRow.Cells[6].Text = reportable.ToString("N0");
GridView1.FooterRow.Cells[7].Text = fatal.ToString("N0");
}
else
{
GridView1.Visible = false;
Label1.Visible = true;
Label1.Text = "The Records for Month " + ddl_Month.SelectedValue + " is not Available.";
}
}
}
}
}
}
}
CREATE PROCEDURE [dbo].[ld_lg_count3]
@Month nvarchar(10) = NULL
,@Year nchar(10) = NULL
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM
(
SELECT COUNT(b.INDICATER_TYPE)as totalRow,a.DEPARTMENT,b.INDICATER_Desc
FROM emp_mast_lead_lag a
LEFT OUTER JOIN lead_lag1 b ON a.EMPNO = b.EMPNO
WHERE (b.Month = @Month OR b.Month IS NULL)
AND (b.Year = @Year OR b.year IS NULL)
GROUP BY a.DEPARTMENT,b.INDICATER_Desc
) t
PIVOT (SUM(totalRow) FOR INDICATER_Desc IN ([Deviation],[Near miss],[First Aid],[Fire],[Non-Reportable],[Reportable],[Fatal]))P
END