【问题标题】:Specified cast is not valid?指定的演员表无效?
【发布时间】:2019-05-26 21:59:10
【问题描述】:

我在 ASP.net 中创建了一个表,我想在页面加载后使用来自数据库的信息填充该表。我收到指定演员表无效的错误。我究竟做错了什么?这是我的代码

public string getData()
{
        string htmlStr = "";

        SqlConnection conn = new SqlConnection(connString);
        SqlCommand command = conn.CreateCommand();
        command.CommandText = "SELECT * from INFO";
        conn.Open();
        SqlDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            DateTime Date = reader.GetDateTime(0);
            DateTime Time = reader.GetDateTime(1);
            htmlStr += "<tr><td>" + Date + "</td><td>"  + Time + "</td></tr>";                  
        }

        conn.Close();

        return htmlStr;
}

<table style="width:100%">
                <caption>INFO</caption>
                <tr>
                    <td> Date </td>
                    <td> Time </td>
                </tr>
                    <%=getData()%>
                </table>

这是我的错误:

上面的代码在这一行抛出了异常:

DateTime Date = reader.GetDateTime(0);

【问题讨论】:

  • @Grant 这行DateTime Date = reader.GetDateTime(0); 抛出异常
  • 好的,我现在可以超过日期,但现在抛出时间异常?
  • time(7) 是数据类型

标签: c# asp.net


【解决方案1】:

来自您的评论:

DateTime Date = reader.GetDateTime(0); 这一行抛出了异常

第一列不是有效的日期时间。最有可能的是,您的表中有多个列,并且您正在通过运行以下查询来检索它们全部

SELECT * from INFO

将其替换为仅检索您感兴趣的两列的查询:

SELECT YOUR_DATE_COLUMN, YOUR_TIME_COLUMN from INFO

然后尝试再次读取值:

var Date = reader.GetDateTime(0);
var Time = reader.GetTimeSpan(1);  // equivalent to time(7) from your database

或者:

var Date = Convert.ToDateTime(reader["YOUR_DATE_COLUMN"]);
var Time = (TimeSpan)reader["YOUR_TIME_COLUMN"];

【讨论】:

  • 我要再试一次,但它不再抛出日期异常
  • 这在我将时间从 DateTime 更改为 TimeSpan 时起作用。非常感谢。我现在唯一的问题,而且不是那么大。但是,对于日期,它会显示日期,然后显示 00:00:00
  • 这就是答案,您选择了错误的列,如果您的表有一个标识表,那么您最有可能选择 0 的 id。始终使用 reader["columnname"] 而不是数字来指定列名,因为表结构总是会发生变化。
  • @notc1 摆脱 00:00:00 使用 Date.ToShortDateString()
  • 感谢@GrantWinney 非常有帮助!
【解决方案2】:

htmlStr 是字符串,那么您需要将DateTime 变量转换为string

while (reader.Read())
                {
                    DateTime Date = reader.GetDateTime(0);
                    DateTime Time = reader.GetDateTime(1);
                    htmlStr += "<tr><td>" + Date.ToString() + "</td><td>"  + 
                    Time.ToString() + "</td></tr>";                  
                }

【讨论】:

  • @notc1 提到什么是getData()
【解决方案3】:

试试这个:

public void LoadData()
        {
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Stocks;Integrated Security=True;Pooling=False");
            SqlDataAdapter sda = new SqlDataAdapter("Select * From [Stocks].[dbo].[product]", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            DataGridView1.Rows.Clear();

        foreach (DataRow item in dt.Rows)
        {
            int n = DataGridView1.Rows.Add();
            DataGridView1.Rows[n].Cells[0].Value = item["ProductCode"].ToString();
            DataGridView1.Rows[n].Cells[1].Value = item["Productname"].ToString();
            DataGridView1.Rows[n].Cells[2].Value = item["qty"].ToString();                
            if ((bool)item["productstatus"])
            {
                DataGridView1.Rows[n].Cells[3].Value = "Active";
            }
            else
            {
                DataGridView1.Rows[n].Cells[3].Value = "Deactive";
            }

【讨论】:

  • 这个问题已经有一个已接受的答案,您的答案将添加到已接受的答案中吗?有什么区别?你应该在你的答案中解释,而不是只把你的代码作为答案。
猜你喜欢
  • 2018-02-06
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多