473,805 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Missing Rows in a DataBind()

Hey all,

Have a strange one here, and being still fairly new to .NET isn't helping me
understand it.

I am having a problem where a DataReader doesn't return all the rows when I
try to use a method from a separate class file that returns a DataReader,
where when I code the DataReader in the .aspx file it does. Below are the
details and code of what I am runnign into. I appreciate any help/insight
you can provide.

I am working on a fairly simple app that allow a user to add/update/delete
meeting minutes and agendas. I have a database (SQL Server 2000) storing
some simple info, and a directory the Mins/Agnd PDF file is uploaded to. I
created a little web user control that uses a DataGrid and DropDown listbox.
I bind each one separately, and use stored procedures to query the DB.

Originally I did the following function, called in the page's OnLoad method
only if Page.IsPostback was false:

Private Sub BindYearDropDow n()
Dim con As New
SqlConnection(C onfigurationSet tings.AppSettin gs("SQLServer_B OS"))
Dim cmd As SqlCommand = New SqlCommand("spA gendas_GetAllAg endaYears", con)
lstAgendas.Data Source = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
lstAgendas.Data TextField = "Year"
lstAgendas.Data ValueField = "Year"
lstAgendas.Data Bind()
End Sub

(My S-Proc is a basic "SELECT DISTINCT vYear FROM MyTable ORDER BY vYear
DESC")

All this does is populate a DropDown list with the distinct years in the DB
(2000 through 2004 in my case). I decided to use a DataReader beacuase I am
not doing two-way communication, and also the DR runs much faster due to
lesser overhead requirements. Well, as I was building this app, I started
to have more and more methods needing to talk to the DB. Well, I wanted to
create a single function that I could just call and it would return a set of
records that I could rummage through or bind to data sources. This way I
could just reuse the same method over and over, and if anything in my DB
connection changed, I didn't have to worry about finding all the location
throughout the app and missing some change.

So, I created the following function in a "business objects" class file:

Public Function GetADataReader( ByVal sSQL As String) As SqlDataReader
Dim con As New
SqlConnection(C onfigurationSet tings.AppSettin gs("SQLServer_B OS"))
Dim cmd As SqlCommand = New SqlCommand(sSQL , con)
Dim dr As SqlDataReader
Try
con.Open()
dr = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
dr.Read()
Return dr
Catch ex As Exception
Throw
End Try
End Function

And then I altered my binding method back in my control to the following
(BoardBO is my "business objects" class file):

Private Sub BindYearDropDow n()

lstAgendas.Data Source =
BoardBO.GetADat aReader("spAgen das_GetAllAgend aYears")
lstAgendas.Data TextField = "Year"
lstAgendas.Data ValueField = "Year"
lstAgendas.Data Bind()

End Sub

And now we get to my question/problem. When I did it the first way, I got
all five years (2004 through 2000). Yet when I do it the second way, I only
get 4 years (2003 through 2000). I debugged the code by adding a breakpoint
on the GetADataReader method and stepping through it one line at a time.
After the dr.Read() call, in the command window I typed "?dr(0)" and it gave
me "2004", yet once the DataBind() method of the BindYearDropDow n method is
called, "2004" is nowhere to be seen. Huh? I don't get this.

Wondering if it was something about a "remote" DataReader method, I also
created a "remote" DataSet method to do the exact same thing, but using a
DataSet object vice a SQLDataReader. That function is :

Public Function GetADataSet(ByV al sSQL As String) As DataSet
Dim con As String =
ConfigurationSe ttings.AppSetti ngs("SQLServer_ BOS")
Dim da As New SqlDataAdapter( sSQL, con)
Dim ds As New DataSet
Try
da.Fill(ds)
Return ds
Catch ex As Exception
Throw
End Try
End Function

And then I changed my dropdown list data bind method to:

Private Sub BindYearDropDow n()

lstAgendas.Data Source =
BoardBO.GetADat aSet("spAgendas _GetAllAgendaYe ars").Tables(0 )
lstAgendas.Data TextField = "Year"
lstAgendas.Data ValueField = "Year"
lstAgendas.Data Bind()

End Sub

Low and behold, I got all five years once again (2004 - 2000)!

What is it about doing this with a DataSet instead of a DataReader that
gives me all my data? Why is the DataReader method loosing the first row in
the returned records? I don't understand this.

If you can help me to understand what is happening, I sure would appreciate
it. Thanks!!

-- Andrew
Nov 18 '05 #1
0 1429

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3641
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user maintenance of data. I use separate logic to delete a row. Everything works just fine. BUT I would like to add a button to (for example) the DataGrid header, which when pressed will add a new row to the datagrid. This should then allow the user to enter information into text boxes (in some...
3
4889
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
4
1477
by: trebor | last post by:
Most of this code comes from or follows the walkthrough (Using a DataGrid Web Control to Read and Write Data). The problem is that as soon as you click the Edit link, the data rows disappear, leaving you with just the header row. I've figured out what's happening, but can't figure out why. Following along with the debugger, as soon as you click Edit, execution goes to Page_Load, and at that point, before it even checks postback, the...
2
1576
by: Andrew | last post by:
Hey all, Have a strange one here, and being still fairly new to .NET isn't helping me understand it. I am having a problem where a DataReader doesn't return all the rows when I try to use a method from a separate class file that returns a DataReader, where when I code the DataReader in the .aspx file it does. Below are the details and code of what I am runnign into. I appreciate any help/insight you can provide.
3
3879
by: Andy Fish | last post by:
Hi, when there are no rows to display in my asp.net datagrid control, I'd like to put in a message saying "No results" or some such Ideally I'd like the message to be centred across all the columns, but I'd settle for the message appearing in a single cell, or header/footer line. maybe there's a way to make the whole control invisible when there are no rows in the data source.
4
1584
by: Ed | last post by:
I have a new requisite from a client that wants to give the user the option to about a databind based on number of returned rows. if ds.rows.count < 1000 then dg.databind() etc. They want a message box to popup and let the user choose to abort the operation or let them continue with the long databind.
0
1701
by: bryanp10 | last post by:
I have a DataList on my page which contains multiple DropDownLists. My page defaults to showing six rows in the DataList. I want the ability to dynamically add rows if needed. Right now I'm just using an empty table with X rows, and adding a new row to the table that the DataList is bound to. However, I can't figure out a way to do this and at the same time maintain the current viewstate of the existing controls. When I call...
17
3037
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). I have a dataset as follows (1 week to keep it short): Employee 1 - Date 1 Employee 1 - Date 2
1
7314
by: tucson | last post by:
I have a gridview that has a blank row with 2 input fields: file to upload, and a description of the file. They click Add and a new row is added, Remove and the row is removed. The problem is: When a new row is added, I loop through the existing gridview rows, store the data in a dataset, and rebind. In debug mode, I see the values I entered but when I rebind, it's not displayed in the gridview. Here's the code aspx code: ...
8
8951
by: =?Utf-8?B?Q2hyaXMgSGFsY3Jvdw==?= | last post by:
Hi there I've successfully added some .NET validation controls to a page (using <asp:RequiredFieldValidator ...), however when I try to set the 'display' property to 'dynamic', my page then throws up the following error in the browser: CS1061: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'Web' and no extension method 'Web' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10617
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10370
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.