指定的转换无效?

17

我在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); 抛出了异常。 - crsMC
好的,我现在能够通过日期了,但是它现在抛出了时间异常? - crsMC
time(7) 是数据类型。 - crsMC
3个回答

12

从你的评论中:

这一行 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"];

我要再试一次,但它不再抛出日期异常了。 - crsMC
一旦我将Time从DateTime更改为TimeSpan,这个问题就解决了。非常感谢。现在我唯一的问题是关于日期,它会显示日期,然后是00:00:00。虽然不是很大的问题。 - crsMC
2
这是答案,你选择了错误的列,如果你的表有一个自增列,那么你选择的id很可能是0。始终使用reader["columnname"]来指定列名,而不是数字,因为表结构总是可能发生变化。 - prospector
1
使用Date.ToShortDateString()来消除00:00:00,请使用@notc1。 - prospector
谢谢@GrantWinney非常有帮助! - crsMC
@GrantWinney 那很有用,但是有没有办法按照以下格式格式化日期:dd/mm/yyyy? - sam

0

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() 是什么。 - Dgan

0

试试这个:

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";
            }

1
这个问题已经有一个被接受的答案了,你的回答会为被接受的答案增加什么?有什么区别吗?你应该在回答中解释,而不仅仅是把你的代码作为答案。 - Nima Derakhshanjan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接