473,761 Members | 8,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to update data adapters?

Hi,

I have a large table. Normally, the user will need to see only the data
from the past two years, but sometimes, they will need to go back further.

I have a form with a datagrid, a DateTimePicker and a command button.
What I'd like is when the user selects a new date and clicks the button,
the grid would display data from the value in the datapicker through today.

I have it working, but only if I select everything in the data adapter,
then I fill a dataset with all the records (this takes 20-30 seconds),
then I create a dataview that is the datasource for the datagrid.

When the user picks a new date, I get the value and use the dataview's
rowfilter to update the range displayed in the datagrid.

This works well, but it is slow and I read here that it is best to work
with small sets of data and then update when necessary.

There must be a better way to somehow update the data adapter and then
update the dataset, dataview and datagrid but I can't seem to get it.

Can someone please give me some ideas to test out?

Thanks

Michael
Nov 21 '05 #1
9 3562
Micheal:

When the user selects a new date range, are you requerying for data that you
already have? If so, you are eating up a lot of resources for no benefit.
So if the date range is smaller for instance, or includes data that you
already have, you can just change your SQL Statement to get the new data
that you want and then just fill the existing datatable. Each time, you can
just use the Adapter to fill rows that you don't have. Since you are
binding to a dataview, you can filter there. So if you had rows 1-10000 and
needed 5000-15000.. .you could just querty the db for 10001 to 15000 filling
the datatable that's already filled, This will give you a table with 15000
records. Now in your grid, you can specify the rowfilter of the dataview to
show only what you want so you don't have to requery. If for instance the
next selection was rows 1-3000 you wouldn't even need to go to the db again.

Does this sound like a viable solution?

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Michael" <no****@aol.com > wrote in message
news:eQ******** *****@TK2MSFTNG P10.phx.gbl...
Hi,

I have a large table. Normally, the user will need to see only the data
from the past two years, but sometimes, they will need to go back further.

I have a form with a datagrid, a DateTimePicker and a command button.
What I'd like is when the user selects a new date and clicks the button,
the grid would display data from the value in the datapicker through
today.

I have it working, but only if I select everything in the data adapter,
then I fill a dataset with all the records (this takes 20-30 seconds),
then I create a dataview that is the datasource for the datagrid.

When the user picks a new date, I get the value and use the dataview's
rowfilter to update the range displayed in the datagrid.

This works well, but it is slow and I read here that it is best to work
with small sets of data and then update when necessary.

There must be a better way to somehow update the data adapter and then
update the dataset, dataview and datagrid but I can't seem to get it.

Can someone please give me some ideas to test out?

Thanks

Michael

Nov 21 '05 #2
Michael,

It are users so you can not force them to do something except if you tell
that you will not return more than let say a page full of data from your
datagrid let say 30 rows.

You can do that using the Select Top with a Where clause

http://msdn.microsoft.com/library/de...a-ses_9sfo.asp

When you can than use the data from your last selected row for the next
Where, than you can do paging through your database using buttons by storing
the proper data for that (by instance the data from the first row everytime)

Another method when you cannot get the exact starting points is of course
much easier by doing this paging date by date.

Just some idea's I hope they help,

Cor
Nov 21 '05 #3
Bill,

Does this sound like a viable solution?

--


Bill,

I was reading full interest your solutions. However I see some problems. How
do you recoginize the gabs. If you give a "when" it will load all the rows
newly so you have to merge those. I don't think that it than will be a
solution however creates more overhead.

However, the biggest disadvantage from your solution is for me, that you
don't know if the already retrieved data in the datatable is complete.

Or do I oversee something?

Cor
Nov 21 '05 #4
Hi Ryan,

I like your solution, but I'm not sure how to implement 'changing you
sql command' part. I'm not sure how to use the data adapter and dataset
to update what is being displayed in the datagrid.

I thought I'd have to requery everything with the new ranges. You mean
I can add more rows to what is already in the dataset?

This is what I am doing now...
.....
sqlConn = New sqlConnection(" ...")
sqlCmd = New SqlCommand("Sel ect * from SALES where date>'2002/1/1'",
sqlConn)
daSales = New SqlDataAdapter( sqlCmd)
daSales.Fill(ds , "SALES")
dvSales = New DataView(ds.Tab les("SALES"))
dgSales.DataSou rce = dvSales
.....

'User selects a new date range below
d = DateTimePicker1 .Value.Date 'gets date user selected
s = CStr(d)
daSales.SelectC ommand.CommandT ext = "select * from SALES where date > " & s
daSales.Fill(ds , "SALES")
dvSales = New DataView(ds.Tab les("SALES"))
dgSales.DataSou rce = dvSales

'Note this doesn't work :(
What does work is having this initial data adapter query ALL the rows,
then later use the dataview's rowfilter to narrow the range. The only
problem is that initial query of gettting ALL the rows can take 20-30sec
and it will only get worse as they enter more data. I thought it best
to limit the default to 2yrs of data and then if needed, I can requery
and get more rows, but I am not sure how to do this.

Thanks for you idea.

Michael
W.G. Ryan eMVP wrote:
Micheal:

When the user selects a new date range, are you requerying for data that you
already have? If so, you are eating up a lot of resources for no benefit.
So if the date range is smaller for instance, or includes data that you
already have, you can just change your SQL Statement to get the new data
that you want and then just fill the existing datatable. Each time, you can
just use the Adapter to fill rows that you don't have. Since you are
binding to a dataview, you can filter there. So if you had rows 1-10000 and
needed 5000-15000.. .you could just querty the db for 10001 to 15000 filling
the datatable that's already filled, This will give you a table with 15000
records. Now in your grid, you can specify the rowfilter of the dataview to
show only what you want so you don't have to requery. If for instance the
next selection was rows 1-3000 you wouldn't even need to go to the db again.

Does this sound like a viable solution?

Nov 21 '05 #5
Hi Cor,

Yes, that gives me a few ideas to try out. My only problem is I am not
sure how to re-query a dataset. I thought I would update the sqlcommand
in the data adapter and then refill the dataset. I tried this but the
grid doesn't update.

Am I missing some refresh() method somewhere?

If someone could please show me the steps (data adapter -> dataset ->
dataview -> datagrid) I'd really appreciate it.

What I do now is: (in the form load event)
1) define a data adapter, where clause with the last two years data
2) data adapter fill method fills a dataset
3) create a dataview using the above dataset
4) have the datagrid's datasource set to the dataview above

This works, but I don't know how to update (or re-query) the rows I need

Thanks

Richard

Cor Ligthert wrote:
Michael,

It are users so you can not force them to do something except if you tell
that you will not return more than let say a page full of data from your
datagrid let say 30 rows.

You can do that using the Select Top with a Where clause

http://msdn.microsoft.com/library/de...a-ses_9sfo.asp

When you can than use the data from your last selected row for the next
Where, than you can do paging through your database using buttons by storing
the proper data for that (by instance the data from the first row everytime)

Another method when you cannot get the exact starting points is of course
much easier by doing this paging date by date.

Just some idea's I hope they help,

Cor

Nov 21 '05 #6
Michael,
What I do now is: (in the form load event)


This sound strange for me, I assumed that a datatimepicker event did do
this.
And than clear the dataset everytime in advance.

And than another event with a button to let the user page back in future
until now.

Cor

Nov 21 '05 #7
Hi Cor,

Sorry for the mixup. I didn't want to cut and paste a lot of code.

I simplified my form so that it has just 3 controls:
- DateTimePicker (used to specify range of rows to show in datagrid)
- Button (when clicked, I'd like the datagrid rows updated)
- DataGrid (initially set with 2 years rows)

At the top of the form (below the 'Windows Form Designer generated
code'), I declared some public variables:

Dim sqlConn As New SqlConnection
Dim daSales As New SqlDataAdapter
Dim ds As New DataSet
Dim sqlCmd As New SqlCommand
Dim dvSales As New DataView

Then in the form load event, I defined the SqlConnection, the Data
Adapter (with select choosing the last two year's rows), the DataSet,
the DataView and then I set the DataGrid's source to the DataView.

sqlConn = New SqlConnection(" server=...")
sqlCmd = New SqlCommand("Sel ect * from SALES where date>'2003/1/1'",
sqlConn)
daSales = New SqlDataAdapter( sqlCmd)
daSales.Fill(ds , "SALES")
dvSales = New DataView(ds.Tab les("SALES"))
dgSales.DataSou rce = dvSales
Later in the code, I have a button_click event that gets the value of a
DateTimePicker control that the user changes. It is here that I am
having trouble re-querying. If I use a DataView.rowfil ter in this
event, it works fine, but I must have the data adapter select ALL (that
takes a long time) defined in the form load event.

' Button click event

Dim d As Date
Dim s As String
Dim strSql As String

d = DateTimePicker1 .Value.Date
s = CStr(d)

' The commented lines below don't work?
'strSql = daSales.SelectC ommand.CommandT ext
'daSales.Select Command.Command Text = "select * from SALES where date > " & s
'daSales.Fill(d s, "SALES")
'dvSales = New DataView(ds.Tab les("SALES"))
'dgSales.DataSo urce = dvSales

dvSales.RowFilt er = "date> '" & s & "'" '<- this works
I hope this makes it clearer what I am trying to do.

Richard
Cor Ligthert wrote:
Michael,

What I do now is: (in the form load event)


This sound strange for me, I assumed that a datatimepicker event did do
this.
And than clear the dataset everytime in advance.

And than another event with a button to let the user page back in future
until now.

Cor

Nov 21 '05 #8
Michael,

I made a sample how I would probably do your problem. I have not completly
tested yet however I thought that it was working as I expected and otherwise
it gives a good idea how you could do it.

\\\needs a datetimepicker, a button and a datagrid and the NorthWind
database
Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Me.DateTimePick er1.Value = New DateTime(1996, 7, 1)
End Sub
Private Sub DateTimePicker1 _ValueChanged(B yVal sender _
As System.Object, ByVal e As System.EventArg s) Handles
DateTimePicker1 .ValueChanged
FillGrid(DateTi mePicker1.Value )
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
FillGrid(Nothin g)
End Sub
Private Sub FillGrid(ByVal dt As DateTime)
Dim conn As New SqlClient.SqlCo nnection("Serve r=(Local);" & _
"DataBase=North wind; Integrated Security=SSPI")
Static lastDate As DateTime = New DateTime(1996, 7, 1)
Dim ds As New DataSet
If dt = Nothing Then
dt = lastDate.AddDay s(1)
End If
Dim da As New SqlClient.SqlDa taAdapter _
("Select * from Orders Where orderdate = @orderdate", conn)
da.SelectComman d.Parameters.Ad d("@orderdate ", CDate(dt))
da.Fill(ds)
Do While ds.Tables(0).Ro ws.Count = 0
dt = dt.AddDays(1)
da.SelectComman d.Parameters("@ orderdate").Val ue = dt
da.Fill(ds)
If dt.Equals(New DateTime(1998, 6, 30)) Then
Exit Do
End If
Loop
DataGrid1.DataS ource = Nothing
DataGrid1.DataS ource = ds.Tables(0)
lastDate = dt
End Sub
///
I hope this helps a little bit?

Cor
Nov 21 '05 #9
Hi Cor,

Thanks so much for taking the time to show me how to do it. This helps
A LOT!

Your example also showed me how to do other things on my list such at
public functions and parameter passing to SQL select statements.

I copied your code into a new example and it finally changes the grid!
I will slowly go over everything and this will definitely teach me what
I need to know.

Thanks again!

Michael
Cor Ligthert wrote:
Michael,

I made a sample how I would probably do your problem. I have not completly
tested yet however I thought that it was working as I expected and otherwise
it gives a good idea how you could do it.

\\\needs a datetimepicker, a button and a datagrid and the NorthWind
database
Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Me.DateTimePick er1.Value = New DateTime(1996, 7, 1)
End Sub
Private Sub DateTimePicker1 _ValueChanged(B yVal sender _
As System.Object, ByVal e As System.EventArg s) Handles
DateTimePicker1 .ValueChanged
FillGrid(DateTi mePicker1.Value )
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click
FillGrid(Nothin g)
End Sub
Private Sub FillGrid(ByVal dt As DateTime)
Dim conn As New SqlClient.SqlCo nnection("Serve r=(Local);" & _
"DataBase=North wind; Integrated Security=SSPI")
Static lastDate As DateTime = New DateTime(1996, 7, 1)
Dim ds As New DataSet
If dt = Nothing Then
dt = lastDate.AddDay s(1)
End If
Dim da As New SqlClient.SqlDa taAdapter _
("Select * from Orders Where orderdate = @orderdate", conn)
da.SelectComman d.Parameters.Ad d("@orderdate ", CDate(dt))
da.Fill(ds)
Do While ds.Tables(0).Ro ws.Count = 0
dt = dt.AddDays(1)
da.SelectComman d.Parameters("@ orderdate").Val ue = dt
da.Fill(ds)
If dt.Equals(New DateTime(1998, 6, 30)) Then
Exit Do
End If
Loop
DataGrid1.DataS ource = Nothing
DataGrid1.DataS ource = ds.Tables(0)
lastDate = dt
End Sub
///
I hope this helps a little bit?

Cor

Nov 21 '05 #10

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

Similar topics

3
2415
by: Paulb1us | last post by:
I want to update records from a csv file. I do this in a button click: //Create Adapters da = new OdbcDataAdapter("Select * FROM test.csv", conn); //Fill a data table da.Fill(dt);
1
2994
by: Wing | last post by:
Hi all, I have created 2 tables in sql database and join these 2 tables before assign the result to the dataset, and display the result in datagrid. Everything is fine up to this point. The problem come up when I want to delete one of rows in datagrid and update the change to the corresponding table. The error msg show up "Dynamic SQL generation is not supported against multiple base tables."
3
1318
by: gl | last post by:
I'm currently planning out a project and I wanted to make sure it's easy to do something (or at least possible). Basically we're going to have a grid reporting off of dataset (with a relative dataadapter). My question revolves around the dataadapter's update ability. First of all the dataset will contain multiple tables. Now when the Update method is called, there has to be special logic. Basically, if the user has changed the text of a...
3
1630
by: Simon | last post by:
Hi everyone, I have a small problem regarding a wizard that I'm making on my website. The wizard is obviously a series of pages that take values from the user. My question is: - Should I store all the values entered until the last page and then update the database, or should I do it as I go.
7
3364
by: Jean Christophe Avard | last post by:
Hi! I have a dataset that retreive all the item information from the database. I need to be able to edit them, in the dataset and in the database. I have this code, could anyone tell me if I'm in the good way, cause I build that code from a book example, but nothing works. I HAVE THIS CODE TO SAVE THE CHANGE OF THE DATASET dstPlant.Tables("plant").Rows(intEditRow)("latin_name") = "KLHJJKJKJKHHHHHH"
1
419
by: Adam | last post by:
This is kind of a tough question, but, I'm working on a program now and using sql data adapters with sql server to generate data tables. I'm updating the tables and then using the dataadapter.update method to send the table back. The problem is, the tables have about 30 fields and I'm only changing 2 (it's a holding file). Can I make an update statement that will only send back
5
2599
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
2
2723
by: Simon | last post by:
Hi all, Is it somehow possible to do do batched updates with table adapters in the same way you can with data adapters? I expected the methods to be much the same but apparently not. If it isn't possible can anyone tell me how to do batched updates on a strongly typed datatable? Thanks
0
943
by: Miro | last post by:
vb2005 express (Im trying to figure out how to "Update databases manually" ) I created a screen, and have created a button. I have a file that has 3 fields: StaffID -Primary Key Edit_User -VarChar(20) 'Just a name field So here is what I have in my button, I get no Exception errors, but im not quite sure where / what to debug to see why the update does not do anything. Here im just trying to set
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10115
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
8780
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
7332
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
6609
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
5229
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...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
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.