string 编号 = gvBookReturn.DataKeys[e.NewSelectedIndex].Value.ToString();
DateTime returnDate = Convert.ToDateTime(gvBookReturn.Rows[e.NewSelectedIndex].Cells[4].Text);
DateTime todayDate = DateTime.Now.Date;
TimeSpan ts = todayDate - returnDate;
int daysDate = ts.Days;
该字符串未被识别为有效的 DateTime。有一个从索引 2 处开始的未知字。源错误:  
行 77: //获取当前选择图书编号
行 78: string 编号 = gvBookReturn.DataKeys[e.NewSelectedIndex].Value.ToString();
行 79: DateTime returnDate = Convert.ToDateTime(gvBookReturn.Rows[e.NewSelectedIndex].Cells[4].Text);
行 80: DateTime todayDate = DateTime.Now.Date;
行 81: TimeSpan ts = todayDate - returnDate;
错误行79行。我想从SQL数据库的一个表里读取一个日期类型的数据给returnDate,再读取电脑现在的时间给todayDate,然后两个时间相减,相减后取天数给daysDate。
行 79: DateTime returnDate = Convert.ToDateTime(gvBookReturn.Rows[e.NewSelectedIndex].Cells[4].Text);
断点调试结果:
> App_Web_dklito-y.dll!Default.gvBookReturn_SelectedIndexChanging(object sender = {System.Web.UI.WebControls.GridView}, System.Web.UI.WebControls.GridViewSelectEventArgs e = {System.Web.UI.WebControls.GridViewSelectEventArgs}) 行 79 C#

解决方案 »

  1.   

    断点调试,监测gvBookReturn.Rows[e.NewSelectedIndex].Cells[4].Text 看看值为多少?值为多少?
      

  2.   

    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 个人信息 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strconn = ConfigurationManager.AppSettings["connStr"];
            SqlConnection conn = new SqlConnection(strconn);
            string strsql = "";
            string a = Session["用户名"].ToString();
            conn.Open();
            strsql = "select * from reader where 学号='" + a + "'";
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(strsql, conn);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("没有记录");
            }
            else
            {
                Label1.Text = ds.Tables[0].Rows[0]["学号"].ToString();
                Label2.Text = ds.Tables[0].Rows[0]["姓名"].ToString();
                Label3.Text = ds.Tables[0].Rows[0]["政治面貌"].ToString();
                Label4.Text = ds.Tables[0].Rows[0]["性别"].ToString();
                Label5.Text = ds.Tables[0].Rows[0]["专业班级"].ToString();
                Label6.Text = DateTime.Parse(ds.Tables[0].Rows[0]["入学时间"].ToString()).ToShortDateString();
                Label7.Text = ds.Tables[0].Rows[0]["手机号码"].ToString();
                Label8.Text = ds.Tables[0].Rows[0]["虚拟网"].ToString();
                TextBox1.Text = ds.Tables[0].Rows[0]["QQ"].ToString();
                TextBox2.Text = ds.Tables[0].Rows[0]["dzyx"].ToString();
                TextBox3.Text = ds.Tables[0].Rows[0]["bz"].ToString();
            }
            conn.Close();    }    protected void Button3_Click(object sender, EventArgs e)
        {
            int i = 0;
            try
            {
                string a = Session["用户名"].ToString();
                SqlConnection con = new SqlConnection("server=.;user id=sa;pwd=;DataBase=book");
                con.Open();
                string UpateSql = "Update reader SET dzyx='" + (TextBox2.Text).Trim() + "',QQ='" + (TextBox1.Text).Trim() + "',bz='" + (TextBox3.Text).Trim() + "' where 学号='" + a + "'";
                SqlCommand com = new SqlCommand(UpateSql, con);
                i = com.ExecuteNonQuery(); //接收一下值,如果大于0 就是更新成功了;            if (i > 0)
                {
                    Response.Write("<script language=javascript>alert('修改成功!')</script>");
                }
                else
                {
                    Response.Write("<script language=javascript>alert('修改失败!')</script>");
                }        }
            catch
            {
                Response.Write("<script language=javascript>alert('修改失败!')</script>");
            }我把这个CS文件的所有代码都贴上来吧。想实现的是登录之后,根据用户名读取该用户的信息,用户可以修改自己的QQ、电子邮箱和备注。
      

  3.   

    在eTime returnDate = Convert.ToDateTime(gvBookReturn.Rows[e.NewSelectedIndex].Cells[4].Text);
    这行设置断点,然后调试
    ctrl+d, w弹出监测窗口,然后输入gvBookReturn.Rows[e.NewSelectedIndex].Cells[4].Text 回车即可看到它的值
      

  4.   

    单步调试跟踪查看gvBookReturn.Rows[e.NewSelectedIndex].Cells[4].Text;
    设置断点,鼠标移动到该位置查看值
    DateTime.Tryparse
      

  5.   

    1:时间 
    时间格式:H:mm:ss 
    2:日期 
    短日期格式:yyyy-MM-dd 
    日期分割符:- 
    长日期格式:yyyy'年'M'月'd'日'