BUGReport: An error occurred while binding an empty dataset to the datagrid with template columns.

Source: Internet
Author: User

An error occurred while binding an empty dataset to the datagrid with template columns: one of the reasons why the GridView is better than the DataGrid

BUG Description: error occurs when the datagrid binds an empty dataset with a template column, while the GridView in the same code asp.net 2.0 does not (it is difficult for MS to recognize this, therefore, the datagrid is also abandoned by 2.0, but the gridview inherits from different base classes, which makes it hard for me to change the three-party universal paging control of oracle, sqlserver, and access)

The Bug exists in asp.net 1.1 asp.net 2.0

Test Tool: vs.net 2003, vs.net 2005, vs.net 2005 sp1, SQLserver

Test process:
Setp1:
// No bound column, automatically generated
WebForm1.aspx:
<Form id = "Form1" method = "post" runat = "server">
<Asp: DataGrid ID = maid = server>
</Asp: DataGrid>
</Form>
WebForm1.cs:
Private void Page_Load (object sender, System. EventArgs e)
{
SqlDataReader Reader2 = DBHelper ("select 26 age, 'test' test where 1 = 0 ");
DataGrid1.DataSource = Reader2;
DataGrid1.DataBind ();
Reader2.Close ();
}

SqlDataReader DBHelper (string SQL)
{
SqlConnection conn = new SqlConnection ("server = localhost; uid = sa; pwd =; database = Northwind ");
SqlCommand cmd = new SqlCommand (SQL, conn );
Conn. Open ();
SqlDataReader Reader = cmd. ExecuteReader (CommandBehavior. CloseConnection );
Return Reader;
}
The application is successfully executed, and the DataGrid is not displayed.

 

Setp2:
// Bind a column, which is automatically generated
WebForm1.aspx:
<Form id = "Form1" method = "post" runat = "server">
<Asp: DataGrid ID = maid = server>
<Columns>
<Asp: TemplateColumn>
<ItemTemplate>
<Asp: CheckBox ID = ck1 runat = server/>
</ItemTemplate>
</Asp: TemplateColumn>
</Columns>
</Asp: DataGrid>

</Form>
WebForm1.cs:
Code unchanged

An error occurred while executing the program. The prompt is displayed when performing the DataGrid1.DataBind () operation.
Invalid attempt to Read when reader is closed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System. InvalidOperationException: Invalid attempt to Read when reader is closed.

Similarly, in asp.net 2.0, the GridView with the template column is used (only the ID of the GridView is also named DataGrid1). The background code remains unchanged and the execution is successful.
<Asp: GridView ID = DataGrid1 runat = server>
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<Asp: CheckBox ID = ck1 runat = server/>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>

Postscript: Today I wanted to go to http://www.mscommunity.com/communitywish/ online submitted BUG, the results found that open is the Chinese Ministry of the pavilion, I do not know is MS of this website closed or be maliciously snatched note, or there is a problem with esserz's DNS resolution. I submitted a question about oracleclient connection before and wrote it in the blog. Later, I found an article on the Internet by a foreigner who wrote this module. I confirmed that I did not pay attention to it at the time of writing and provided a hotfix. If this is a bug, it would be 2nd. Pai_^ 0, but now in Africa, basically nothing can be accessed, the blog garden cannot be accessed, the csdn can not be accessed, and only sohu's blog can be written.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.