473,796 Members | 2,798 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
2 1575
You call .Read before you return the erader - which advances the reader to
the first row. I assume your problem is always missing the first row from
the result set? Well, this is why.

"Andrew" <An********@hot mail.com> wrote in message
news:eK******** *****@TK2MSFTNG P12.phx.gbl...
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 #2
Damn, I took all that time and effort to write up the message and you go an
solve it in three lines. I guess .Net is more productive. :) Thanks for
the help, it was right on the money!

-- Andrew
"Marina" <so*****@nospam .com> wrote in message
news:ux******** ******@tk2msftn gp13.phx.gbl...
You call .Read before you return the erader - which advances the reader to
the first row. I assume your problem is always missing the first row from
the result set? Well, this is why.

"Andrew" <An********@hot mail.com> wrote in message
news:eK******** *****@TK2MSFTNG P12.phx.gbl...
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 #3

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

Similar topics

3
4886
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
1476
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
257
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
3876
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
1583
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
1699
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
3036
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
8949
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
9535
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
10467
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
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
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
7558
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
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4130
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
2931
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.