尝试无效时,没有数据present读取数据 [英] Invalid attempt to read data when no data is present
问题描述
我在C#[建成之前,我有我的present工作,创造者走了],这是罚款,直至上周SQL Server数据库。在第一页 clerk_search.aspx
它搜索人回发到一个DataGrid这很好的SQL Server。
有一个ASP 图片
按钮的点击,并前进到下一个页面,输入与有关客户装领域为客户光临原因后回来。对于一些人下页填充,别人没有。在SQL语句中使用支票查询分析器了罚款,我不明白。我不认为它的读者,因为别人都在精细记录,以及其他客户都在SQL中的行present并可以查询就好了。所有被贴在下面,我不是精明与编码,请协助。
System.InvalidOperationException:无效尝试当没有数据是present读
SqlDataReader的reader2 = cmd.ExecuteReader();结果
reader2.Read();
[InvalidOperationException异常:无效尝试当没有数据是present阅读]
块引用>下面是实际的:....
clerk_create.aspx.cs
公共部分类clerk_create:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
如果(Request.Cookies时[我们] == NULL)
{
的Response.Write(对不起,您没有访问该页面,请参阅
数据系统)。
到Response.End();
} 如果(!的IsPostBack)
{
使用(SqlConnection的连接=新的SqlConnection
(WebConfigurationManager.ConnectionStrings [walkin2]。的ConnectionString))
{
文本框txtsct =(文本框)页previousPage.FindControl(Txtsct);
Txtsct.Text = txtsct.Text;
TXT文本框=(文本框)页previousPage.FindControl(Txtssn);
Txtssn.Text =+ txt.Text;
connection.Open();
字符串strsql2 =SELECT dbo.table_name.SSN,
dbo.table_name.LAST_NAME,
dbo.table_name.FIRST_NAME,dbo.table_name.MIDDLE_INITIAL,
dbo.table_name.COMPONENT_ code,dbo.table_name。preSENT_ code从
dbo.table_name INNER JOIN dbo.table_name ON dbo.table_name.SSN ='+
Txtssn.Text +');
CMD的SqlCommand =新的SqlCommand(strsql2,连接);
SqlDataReader的reader2 = cmd.ExecuteReader();
reader2.Read();
LblLName.Text =+ reader2 [LAST_NAME];
LblFName.Text =+ reader2 [FIRST_NAME];
}
}
}
...
}
解决方案您应该检查
读
方法的返回值。如果阅读方法的返回值
假
则没有更多的数据。在这种情况下,你不应该读阅读器的数据。这样做将导致此异常。如果你确定,你会得到一个记录试试这个
如果(reader2.Read())
{
LblLName.Text =+ reader2 [LAST_NAME];
LblFName.Text =+ reader2 [FIRST_NAME];
}一般
的DataReader
作为下面的,因为它可以包含的记录数而(reader2.Read())
{
//使用reader2消耗读者[列]等
}I have a SQL Server database in C# [built before I got to my present job, creator is gone], and it was fine until last week. On the first page
clerk_search.aspx
it searches SQL Server for people and posts back to a datagrid that's fine.There is a ASP
Image
button that's clicked on and it goes forward to the next page, to enter the reason the for the customers visit with loaded fields about the customer that post back. For some persons the next page populates, for others it does not. The SQL statement used checks out fine in query analyzer, I don't understand. I don't think its the reader because others are logged in fine, and the other customers are present in the rows in SQL and can be queried just fine. All is posted below, I am not savvy with coding, please assist.System.InvalidOperationException: Invalid attempt to read when no data is present.
SqlDataReader reader2 = cmd.ExecuteReader();
reader2.Read();[InvalidOperationException: Invalid attempt to read when no data is present.]
Here is the actual: ....
clerk_create.aspx.cs
public partial class clerk_create : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["us"] == null) { Response.Write("Sorry, you do not have access to this page. Please see data systems."); Response.End(); } if (!IsPostBack) { using (SqlConnection connection = new SqlConnection (WebConfigurationManager.ConnectionStrings["walkin2"].ConnectionString)) { TextBox txtsct = (TextBox)Page.PreviousPage.FindControl("Txtsct"); Txtsct.Text = txtsct.Text; TextBox txt = (TextBox)Page.PreviousPage.FindControl("Txtssn"); Txtssn.Text = "" + txt.Text; connection.Open(); string strsql2 = "SELECT dbo.table_name.SSN, dbo.table_name.LAST_NAME, dbo.table_name.FIRST_NAME, dbo.table_name.MIDDLE_INITIAL, dbo.table_name.COMPONENT_CODE, dbo.table_name.PRESENT_CODE FROM dbo.table_name INNER JOIN dbo.table_name ON dbo.table_name.SSN = '" + Txtssn.Text + "')"; SqlCommand cmd = new SqlCommand(strsql2, connection); SqlDataReader reader2 = cmd.ExecuteReader(); reader2.Read(); LblLName.Text = "" + reader2["LAST_NAME"]; LblFName.Text = "" + reader2["FIRST_NAME"]; } } } ... }
解决方案You should check the return value of
Read
method.If read method returns
false
then there is no more data. In that case you should not read the data from reader. Doing so will cause this exception.If you're sure that you'll get only one record try this
if(reader2.Read()) { LblLName.Text = "" + reader2["LAST_NAME"]; LblFName.Text = "" + reader2["FIRST_NAME"]; }
usually
DataReader
is used as below, since it can contain number of recordswhile (reader2.Read()) { //Consume reader using reader2["Column"] etc }
这篇关于尝试无效时,没有数据present读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!