C# 指定的强制转换无效?

C# 指定的强制转换无效?,c#,asp.net,C#,Asp.net,我在ASP.net中创建了一个表,我想在加载页面后用数据库中的信息填充该表。我收到一个错误,指定的强制转换无效。我做错了什么?这是我的密码 public string getData() { string htmlStr = ""; SqlConnection conn = new SqlConnection(connString); SqlCommand command = conn.CreateCommand(); comma

我在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;
}

htmlStr
是字符串,那么您需要
Date
Time
变量来
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>";                  
                }
while(reader.Read())
{
DateTime日期=reader.GetDateTime(0);
DateTime=reader.GetDateTime(1);
htmlStr+=“”+Date.ToString()++”
Time.ToString()+“”;
}
根据您的评论:

此行
DateTime Date=reader.GetDateTime(0)正在引发异常

第一列不是有效的DateTime。最可能的情况是,您的表中有多个列,您正在通过运行以下查询来检索所有列:

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
或:

试试这个:

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

@授予此行
DateTime Date=reader.GetDateTime(0)正在抛出异常OK,我现在可以通过日期,但它现在正在抛出时间异常?时间(7)是datatype@notc1提到什么是
getData()
我将再试一次,但它不再抛出日期异常。一旦我将时间从DateTime更改为TimeSpan,它就起作用了。非常感谢。我现在唯一的问题,也不是什么大问题。但是,对于日期,它显示日期,然后显示00:00:00。这是答案,您选择了错误的列,如果您的表具有标识表,则您选择的id很可能为0。始终使用
reader[“columnname”]
而不是数字来指定列名,因为表结构总是会发生更改。@notc1要摆脱00:00:00,请使用
Date.toSortDateString()
谢谢@grantWiney非常有帮助!这个问题已经有了一个公认的答案,你的答案会给公认的答案增加什么?有什么区别?你应该在你的答案中解释,而不是仅仅把你的代码作为答案。
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"];
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";
            }