类型转换问题(急!!!)
string docid = "";string userid = "";
string i易做图pire = "";
string today = "";
string tablename = "";
float fine;
//string Bdate = "";
//string Rdate = "";
string title = "";
public void select()
{
docid = this.Docidtxt.Text;
userid=this.Useridtxt.Text;
SqlConnection conn=db.createcon();
conn.Open();
string sql = "select * from Borrowinfo where Allbookid='"+this.docid+"'";
SqlCommand cmdselect = new SqlCommand(sql,conn);
SqlDataReader sdr = cmdselect.ExecuteReader();
while(sdr.Read())
{
Session["Bdate"]=sdr["Borrowdate"].ToString();
Session["Rdate"]=sdr["Shredate"].ToString();
Session["Title"] = sdr["Title"].ToString();
title = Session["Title"].ToString();
}
}
public void select1()
{
SqlConnection conn = db.createcon();
conn.Open();
string sql = "select * from Updateinfo ";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
Session["dfine"] = sdr["Dayfine"].ToString();//数据库中定义的日罚款金额Dayfine是float型
}
today = (System.DateTime.Now.Date).ToString();//获取系统当前日期
DateTime t = Convert.ToDateTime(today);//将当前日期转换成日期类型
DateTime s = Convert.ToDateTime(Session["Rdate"]);//
if (t > s)//比较是否超期
{
i易做图pire = "是";
TimeSpan days = t.Subtract(s);
int daynum = days.Days;
int dayfine = (Int32)Session["dfine"];
fine = daynum *dayfine;//计算罚款金额
}
else
{
i易做图pire = " 否";
fine = 0;
}
}
对于:int dayfine = (Int32)Session["dfine"];出错提示为:指定的转换无效
若改为: int dayfine = Convert.ToInt32(Session["dfine"]);出错提示为:字符串格式不正确
如果不进行类型转换,会提示*不能作用于两种不同类型
--------------------编程问答-------------------- int dayfine = Convert.ToInt32(Session["dfine"].ToString());????
--------------------编程问答-------------------- int dayfine = Convert.ToInt32(Session["dfine"])应写成
int dayfine = Convert.ToInt32(Session["dfine"].ToString()); --------------------编程问答-------------------- Session["dfine"]中的内容不是整数类型,导致转换失败 --------------------编程问答-------------------- int dayfine =int.parse(Session["dfine"].ToString());不行吗? --------------------编程问答-------------------- int dayfine = Int32.Parse(Session["dfine"].ToString()); --------------------编程问答-------------------- 这些方法都试了,仍出现以上错误 --------------------编程问答-------------------- 直接: (int)Session["dfine"] --------------------编程问答-------------------- 你看看吧,不是转换的问题,是数据本身就不能转换为int型,调试一下,看看Session["dfine"]中究竟有没有值,是什么样的值。
--------------------编程问答-------------------- Session["dfine"]里面的数据是float,为什么转换成int?
float dayfine = Convert.ToSingle(Session["dfine"]);
int abc = Convert.ToInt32(dayfine); --------------------编程问答-------------------- 因为dayfine要和前面的天数相乘,天数daynum就是整型啊,如果不转换会提示错误,我试试toopoolin的方法看行不行
--------------------编程问答-------------------- 谢谢,toopoolin 给我的提示
农好了,谢谢大家帮忙了
补充:.NET技术 , ASP.NET