gpt4 book ai didi

c# - ExecuteReader- System.Data.dll 中出现类型为 'System.InvalidOperationException' 的异常,但未在用户代码中处理

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:07 25 4
gpt4 key购买 nike

每次尝试运行此特定网页时,我都会收到上述错误。我正在添加一些代码和数据库结构的图像(作为链接给出)。在错误的附加信息行中,它表示在数据表为空时尝试读取数据。当我回去检查表数据时,有数据存在。请帮忙!

SqlConnection con = new SqlConnection(@"ConnectionString");
protected void Page_Load(object sender, EventArgs e)
{
string checkdu = "select Top 1 Hour, Minute, Hour1, Minute1 from Clockin";
SqlCommand cmddu = new SqlCommand(checkdu, con);
con.Open();
SqlDataReader dru = cmddu.ExecuteReader();
int td1 = (Convert.ToInt16(dru["Hour"]) * 60) + (Convert.ToInt16(dru["Minute"])); //error shown here[enter image description here][1]
int td2 = (Convert.ToInt16(dru["Hour1"]) * 60) + (Convert.ToInt16(dru["Minute1"]));

最佳答案

你必须使用 SqlDataReader.Read使读者前进到下一条(第一条)记录:

using(var dru = cmddu.ExecuteReader())
{
if(dru.Read())
{
int td1 = (Convert.ToInt16(dru["Hour"]) * 60) + (Convert.ToInt16(dru["Minute"])); //error shown here[enter image description here][1]
int td2 = (Convert.ToInt16(dru["Hour1"]) * 60) + (Convert.ToInt16(dru["Minute1"]));
}
}

Remarks :

The default position of the SqlDataReader is before the first record. Therefore, you must call Read to begin accessing any data.

关于c# - ExecuteReader- System.Data.dll 中出现类型为 'System.InvalidOperationException' 的异常,但未在用户代码中处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36080257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com