主题:指定的转换无效。
在做DataGrid1_DeleteCommand时出现的问题。。
指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidCastException: 指定的转换无效。
源错误:
行 91: private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
行 92: {
行 93: [i]int empID =(int)DataGrid1.DataKeys[e.Item.ItemIndex];[/i]
行 94: string sqlCom = "delete userreg where login="+empID.ToString();
行 95: string sqlconn = ConfigurationSettings.AppSettings["SQLConnectionString"];
附原码:
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int empID =(int)DataGrid1.DataKeys[e.Item.ItemIndex];
string sqlCom = "delete userreg where login="+empID.ToString();
string sqlconn = ConfigurationSettings.AppSettings["SQLConnectionString"];
SqlConnection conn = new SqlConnection(sqlconn);
SqlCommand cmd = new SqlCommand(sqlCom,conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
lastEditedPage = DataGrid1.CurrentPageIndex;
if((DataGrid1.PageCount- DataGrid1.CurrentPageIndex )==1&&DataGrid1.Items.Count==1)
{
if(DataGrid1.PageCount > 1)
{
lastEditedPage=lastEditedPage-1;
}
else
{
lastEditedPage=0;
}
}
DataGrid1.CurrentPageIndex =lastEditedPage;
BindGrid();
}
指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidCastException: 指定的转换无效。
源错误:
行 91: private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
行 92: {
行 93: [i]int empID =(int)DataGrid1.DataKeys[e.Item.ItemIndex];[/i]
行 94: string sqlCom = "delete userreg where login="+empID.ToString();
行 95: string sqlconn = ConfigurationSettings.AppSettings["SQLConnectionString"];
附原码:
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int empID =(int)DataGrid1.DataKeys[e.Item.ItemIndex];
string sqlCom = "delete userreg where login="+empID.ToString();
string sqlconn = ConfigurationSettings.AppSettings["SQLConnectionString"];
SqlConnection conn = new SqlConnection(sqlconn);
SqlCommand cmd = new SqlCommand(sqlCom,conn);
conn.Open();
try
{
cmd.ExecuteNonQuery();
lastEditedPage = DataGrid1.CurrentPageIndex;
if((DataGrid1.PageCount- DataGrid1.CurrentPageIndex )==1&&DataGrid1.Items.Count==1)
{
if(DataGrid1.PageCount > 1)
{
lastEditedPage=lastEditedPage-1;
}
else
{
lastEditedPage=0;
}
}
DataGrid1.CurrentPageIndex =lastEditedPage;
BindGrid();
}