读取器关闭时无效尝试调用读取。 [英] Invalid attempt to call read when reader is closed.

查看:78
本文介绍了读取器关闭时无效尝试调用读取。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void getdata()

{





string str =select address,company from供应商,其中id = 1;







SqlDataReader dr = DB.ExecuteReader(str); <如果(dr.Read())

{
/>
txtAddress.Text = dr [address]。ToString();



txtCompany.Text = dr [company]。ToString( );





dr.Close();





}

}

我发现此错误

读取器关闭时无效尝试调用Read。



我尝试过:



读取器关闭时无效尝试呼叫读取。

public void getdata()
{


string str = "select address,company from supplier where id=1";



SqlDataReader dr= DB.ExecuteReader(str);


if (dr.Read())
{

txtAddress.Text = dr["address"].ToString();

txtCompany.Text = dr["company"].ToString();


dr.Close();


}
}
and i find this error
Invalid attempt to call Read when reader is closed.

What I have tried:

Invalid attempt to call Read when reader is closed.

推荐答案

使用调试器查看错误发生的位置。

Use the debugger to see where the error occurs.
Quote:

读取器关闭时无效尝试调用Read。

Invalid attempt to call Read when reader is closed.



从错误消息中,您调用


From the error message, you call

dr.Read()

但是没有打开阅读器。





调试器允许你逐行跟踪执行,检查变量,你会发现它有一个点,它停止做你期望的。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

but the reader is not opened.


The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


这篇关于读取器关闭时无效尝试调用读取。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆