我在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);
DateTime Date = reader.GetDateTime(0);
抛出了异常。 - crsMC