请教C#高手,我在更新数据库数据时,运用System.Web.UI.WebControls.TextBox进行更新,为什么总是出现以下的错误????(更新语句在下面),请问以下的语句有错吗??“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------指定的转换无效。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。  
更新语句:
private void dGridLinkUpdateCommand(object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strSql = "update [Article] set title = @title, linkUrl = @linkUrl, orderIndex = @orderIndex where linkID = @linkID";
SqlCommand webCmd = new SqlCommand(strSql, conn);
webCmd.Parameters.Add("@title", SqlDbType.VarChar, 100);
webCmd.Parameters.Add("@linkUrl", SqlDbType.VarChar,30);
webCmd.Parameters.Add("@orderIndex", SqlDbType.BigInt, 8);
webCmd.Parameters.Add("@linkID", SqlDbType.BigInt, 8); webCmd.Parameters["@linkID"].Value = dGridArticle.DataKeys[(int)e.Item.ItemIndex];
string title = ((System.Web.UI.WebControls.TextBox)e.Item.Cells[0].Controls[0]).Text;
string linkUrl = ((System.Web.UI.WebControls.TextBox)e.Item.Cells[1].Controls[0]).Text;
long orderIndex =Convert.ToInt64(((System.Web.UI.WebControls.TextBox)e.Item.Cells[2].Controls[0]).Text); webCmd.Parameters["@title"].Value = title;
webCmd.Parameters["@linkUrl"].Value = linkUrl;
webCmd.Parameters["@orderIndex"].Value = orderIndex;

webCmd.Connection.Open(); try
{

webCmd.ExecuteNonQuery();


}
catch
{
lblMessage.Text = "更新失败!";
return;
}
dGridLink.EditItemIndex = -1;
webCmd.Connection.Close();
dataBind();
}

解决方案 »

  1.   

    看看最后生成 的SQL语句,去调试一下,是不是有错误
      

  2.   

    是这句话Convert.ToInt64(((System.Web.UI.WebControls.TextBox)e.Item.Cells[2].Controls[0]).Text);报的错吧。1、确认e.Item.Cells[2]中输入的是否是数字?
    2、确认e.Item.Cells[2]中是否输入?
    3、确认e.Item.Cells[2]是否存在?
      

  3.   

    TO swordragon(古道热肠) 
    不是那句报的错,而是:
    string title = ((System.Web.UI.WebControls.TextBox)e.Item.Cells[0].Controls[0]).Text;
    报错了,个人觉得这个没什么错,请问,是什么原因?对于:
    long orderIndex =Convert.ToInt64(((System.Web.UI.WebControls.TextBox)e.Item.Cells[2].Controls[0]).Text);
    1.确认e.Item.Cells[2]中输入的是数字
    2.确认e.Item.Cells[2]中是可以输入的.
    3.确认e.Item.Cells[2]也是存在的.
    就是在再新编辑输入或者不修改点击更新按钮后就出现了以上所述的错误.请教一下,出现这样的问题有可能出现问题的地方有哪些??
      

  4.   

    string title = ((System.Web.UI.WebControls.TextBox)e.Item.Cells[0].Controls[0]).Text;
    报错了,个人觉得这个没什么错,请问,是什么原因?
    ------------------------------------------
    确认e.Item.Cells[0].Controls[0]是不是TextBox,或许是Label。