读书人

错误详细信息: System.InvalidCastExc

发布时间: 2012-12-21 12:03:50 作者: rapoo

异常详细信息: System.InvalidCastException: 指定的转换无效。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class QianFei_QianfeiXiangxi : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!this.IsPostBack)
{
string name = Request["name"];
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from tb_qianfei where name='" + name + "'";
cmd.ExecuteNonQuery();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
if (name!= "")
{
this.lblqfname.Text = sdr.GetString(0).ToString();
this.lblzz.Text = sdr.GetString(1);
this.lbllb.Text = sdr.GetString(2);
this.lblrqzs.Text = sdr.GetDateTime(3).ToShortDateString();
this.lblzzsj.Text = sdr.GetDateTime(4).ToShortDateString();
this.lblje.Text = sdr.GetFloat(5).ToString();
this.Lblzt.Text = sdr.GetString(6).ToString();
this.lbljsr.Text = sdr.GetString(7).ToString();
}
else
{
Response.Write("暂无主题,不能显示");
Response.Redirect("~/Default.aspx");//将该页跳转到指定的页面中
}


con.Close();
}


出错的是这一句this.lblje.Text = sdr.GetInt32(5).ToString();
把它注释掉就可以运行了
数据库里this.lblje.Text的文本是float类型的
每次跳转到这个页面都会出错,昨晚做了一晚上没做出来
[最优解释]
首先金额的类型最好用Decimal。
至于你这里reader.GetFloat(string name,string defaultValue)
参数是字符串,name就是你的列名
[其他解释]
sdr.GetInt32(5)的内容看看是什么
[其他解释]
第一:sql脚本不要写成select * from tb_qianfei,把*号换成具体的字段。原因你很难保证GetInt32(5)就是你所需要绑定的字段。
第二:this.lblje.Text = sdr.GetFloat(5).ToString();改成this.lblje.Text = sdr.GetFloat(5)??"0":sdr.GetFloat(5).toString();
[其他解释]
或者this.lblje.Text = sdr[5].ToString();
另外你的sdr没有close吗?

[其他解释]
把36行改成:
this.lblje.Text = sdr.GetSqlValue(5).ToString()
并设上断点,看看到底返回什么了
[其他解释]
堆栈跟踪:


[InvalidCastException: 指定的转换无效。]
System.Data.SqlClient.SqlBuffer.get_Int32() +4838981
System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i) +38
QianFei_QianfeiXiangxi.Page_Load(Object sender, EventArgs e) in d:\RanJu\QianFei\QianfeiXiangxi.aspx.cs:36
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627
[其他解释]
引用:
sdr.GetInt32(5)的内容看看是什么

是数字
[其他解释]

引用:
sdr.GetInt32(5)的内容看看是什么



欠费金额那一栏就是sdr.GetInt32(5)的内容,点击详细信息跳转就出错

[其他解释]
你说的sdr.GetInt32(5)在数据库里是float类型 你用GetInt32来转 应该是报错的。你用
sdr.GetFloat(5)试试。
[其他解释]
引用:
你说的sdr.GetInt32(5)在数据库里是float类型 你用GetInt32来转 应该是报错的。你用
sdr.GetFloat(5)试试。

试了,还是有错
[其他解释]
引用:
首先金额的类型最好用Decimal。
至于你这里reader.GetFloat(string name,string defaultValue)
参数是字符串,name就是你的列名

SqlDataReader.GetFloat是有GetFloat(int index),从零开始的列序号。
[其他解释]
引用:
首先金额的类型最好用Decimal。
至于你这里reader.GetFloat(string name,string defaultValue)


参数是字符串,name就是你的列名

换了money类型就好了
sdr.GetSqlMoney(5).ToString();总算是过去了

读书人网 >asp.net

热点推荐