I have a program to display queries to a SQL db. I type my query in a
textbox and click a button and the results display in a datagrid. I
could use either dataset or datatable to read the data in then I make
the datagrid.dataso urce = myds.Tables(0) or mydt. Now what I want is to
be able to change the query and click the button again and get the new
results. I can use clear() to clear the data from a ds/dt but I need
the structure gone too. How can I do this? Here is sample code using dt.
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim Sql2000DataAdap ter As New
System.Data.Sql Client.SqlDataA dapter(TextBox2 .Text, TextBox1.Text)
mydt.Clear()
Try
Sql2000DataAdap ter.Fill(mydt)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = mydt
End Sub
Private Sub DataGridView1_C ellContentClick (ByVal sender As
System.Object, ByVal e As
System.Windows. Forms.DataGridV iewCellEventArg s) Handles
DataGridView1.C ellContentClick
TextBox3.Text = DataGridView1.C urrentCell.Valu e
End Sub 8 11061
Why not just create a new DataTable....
Replace the line "mydt.clear " with ....
mydt = New DataTable
hopefully you are not holding more then the 1 reference to the datatable.
--
Terry
"cj" wrote:
I have a program to display queries to a SQL db. I type my query in a
textbox and click a button and the results display in a datagrid. I
could use either dataset or datatable to read the data in then I make
the datagrid.dataso urce = myds.Tables(0) or mydt. Now what I want is to
be able to change the query and click the button again and get the new
results. I can use clear() to clear the data from a ds/dt but I need
the structure gone too. How can I do this? Here is sample code using dt.
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim Sql2000DataAdap ter As New
System.Data.Sql Client.SqlDataA dapter(TextBox2 .Text, TextBox1.Text)
mydt.Clear()
Try
Sql2000DataAdap ter.Fill(mydt)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = mydt
End Sub
Private Sub DataGridView1_C ellContentClick (ByVal sender As
System.Object, ByVal e As
System.Windows. Forms.DataGridV iewCellEventArg s) Handles
DataGridView1.C ellContentClick
TextBox3.Text = DataGridView1.C urrentCell.Valu e
End Sub
I'll show my ignorance here but what happens to the old datatable? Is
it cleared out of memory when a new one is created with the same name
(mydt)?
Terry wrote:
Why not just create a new DataTable....
Replace the line "mydt.clear " with ....
mydt = New DataTable
hopefully you are not holding more then the 1 reference to the datatable.
well I suppose you could clear it first, that may get rid of some of the
memory usage sooner. The reason that I memtioned not haveing any other
references to the table, was so that it could be garbage collected at some
point in time. And if you don't, it will. Remember that mydt is just a
reference to an object. A 'pointer' to the DataTable. Probably the best
thing to do would be to 'Dispose' of the DataTable first.
If mydt IsNot Nothing Then
mydt.Dispose()
End If
mydt = New DataTable
--
Terry
"cj" wrote:
I'll show my ignorance here but what happens to the old datatable? Is
it cleared out of memory when a new one is created with the same name
(mydt)?
Terry wrote:
Why not just create a new DataTable....
Replace the line "mydt.clear " with ....
mydt = New DataTable
hopefully you are not holding more then the 1 reference to the datatable.
yes, that did it. Thanks!
Terry wrote:
should be a way to get it to 'forget' about the colums it had before - maybe
set its datasource to 'Nothing' first?
It sounds like from what your saying as soon as mydt=new datatable runs
the second time the pointer is changed to a new datatable and the old
datatable is just left waiting on garbage collection. That's ok if
that's correct. Am I getting this right?
Terry wrote:
well I suppose you could clear it first, that may get rid of some of the
memory usage sooner. The reason that I memtioned not haveing any other
references to the table, was so that it could be garbage collected at some
point in time. And if you don't, it will. Remember that mydt is just a
reference to an object. A 'pointer' to the DataTable. Probably the best
thing to do would be to 'Dispose' of the DataTable first.
If mydt IsNot Nothing Then
mydt.Dispose()
End If
mydt = New DataTable
You got it.
--
Terry
"cj" wrote:
It sounds like from what your saying as soon as mydt=new datatable runs
the second time the pointer is changed to a new datatable and the old
datatable is just left waiting on garbage collection. That's ok if
that's correct. Am I getting this right?
Terry wrote:
well I suppose you could clear it first, that may get rid of some of the
memory usage sooner. The reason that I memtioned not haveing any other
references to the table, was so that it could be garbage collected at some
point in time. And if you don't, it will. Remember that mydt is just a
reference to an object. A 'pointer' to the DataTable. Probably the best
thing to do would be to 'Dispose' of the DataTable first.
If mydt IsNot Nothing Then
mydt.Dispose()
End If
mydt = New DataTable
Hi Cj,
Alternatively, you can clear the columns in the DataTable to remove the
existing schema in the DataTable. To do this, call the Clear method on the
Columns property of the DataTable. For example:
mydt.Columns.Cl ear()
Actually, when the AutoGenerateCol umns property of a DataGridView is set to
true, if you set the DataSource property of the DataGridView to a data
source, e.g. a DataTable, DataGridView will generate columns according to
the data columns in the DataTable automatically.
At this time, if you clear the data columns in the DataTable or set the
DataSource property of the DataGridView to Nothing, the columns in the
DataGridView which were populated by the DataGridView automatically will be
removed automatically as well.
On the other hand, if you set another DataTable, which has different schema
from the previous DataTable, as the data source of the DataGridView,
DataGridView will check the existing columns in it and then generate
columns for those data columns in the new DataTable which have no
corresponding columns in the DataGridView. This is why you see the columns
in the DataGridView are "last_name, first_name, address, group" after you
run "select * from addr_table where group = 'A'".
So the complete solution to your problem is to call the Clear method on the
DataTable first and then call the Clear method on the Columns property of
the DataTable. The following is a sample:
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim Sql2000DataAdap ter As New
System.Data.Sql Client.SqlDataA dapter(TextBox2 .Text, TextBox1.Text)
mydt.Clear()
mydt.Columns.Cl ear()
Try
Sql2000DataAdap ter.Fill(mydt)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = mydt
End Sub
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Linda,
Thanks for your excellent description of what takes place when using a
datatable and datagrid like this. It makes sense to me now. I can see
how there are several ways to solve the problem I was seeing but I think
your suggestion is the best.
Linda Liu[MSFT] wrote:
Hi Cj,
Alternatively, you can clear the columns in the DataTable to remove the
existing schema in the DataTable. To do this, call the Clear method on the
Columns property of the DataTable. For example:
mydt.Columns.Cl ear()
Actually, when the AutoGenerateCol umns property of a DataGridView is set to
true, if you set the DataSource property of the DataGridView to a data
source, e.g. a DataTable, DataGridView will generate columns according to
the data columns in the DataTable automatically.
At this time, if you clear the data columns in the DataTable or set the
DataSource property of the DataGridView to Nothing, the columns in the
DataGridView which were populated by the DataGridView automatically will be
removed automatically as well.
On the other hand, if you set another DataTable, which has different schema
from the previous DataTable, as the data source of the DataGridView,
DataGridView will check the existing columns in it and then generate
columns for those data columns in the new DataTable which have no
corresponding columns in the DataGridView. This is why you see the columns
in the DataGridView are "last_name, first_name, address, group" after you
run "select * from addr_table where group = 'A'".
So the complete solution to your problem is to call the Clear method on the
DataTable first and then call the Clear method on the Columns property of
the DataTable. The following is a sample:
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim Sql2000DataAdap ter As New
System.Data.Sql Client.SqlDataA dapter(TextBox2 .Text, TextBox1.Text)
mydt.Clear()
mydt.Columns.Cl ear()
Try
Sql2000DataAdap ter.Fill(mydt)
Catch ex As Exception
MessageBox.Show ("Error: " & ex.Message)
Exit Sub
End Try
DataGridView1.D ataSource = mydt
End Sub
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Frnak McKenney |
last post by:
I'm using an in-core DataSet as an image of my application's
'database' (a multi-table Access97 mdb file). Updates are made to
the DataTables within the DataSet via forms with bound TextBoxes,
then written to the database... or at least that's what's supposed
to be happening.
Unfortunately, I've discovered that while it appears that when I
create a new record/row I'm successfully updating the Access
database, once the Update is...
|
by: Bill C. |
last post by:
Hi,
I've got a simple console app that just reads an XML file into a DataSet
then prints out a description of each table in the DataSet, including
column names and row values for each column. I'm getting some strange
results depending the input XML file I use. I was wondering if somebody
could help me understand what is going on or point me to a good reference.
The code for my program looks like this:
|
by: JIM.H. |
last post by:
Hello,
Can I send a dataset as a parameter into stored procedure and import data to
a table in the stored procedure?
Thanks,
Jim.
|
by: cindy |
last post by:
Get data into datatable, add to dataset dsSearch "
Get data into datatable, add to dataset dsSearch
Using In-Memory SQL Engine join the tables and select the filenames from the
join, add to dataset dsSearch
CODE ON
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3994&lngWId=10
using SQL connection get data from view on sql server, add to dataset dsSearch
|
by: Roy Lawson |
last post by:
I am having no problems connecting to a DB, creating a DataAdapter,
and creating a dataset...and connecting to the data. Using the
builtin data objects to do all this.
My only problem now is navigating through the data. I can get the
data into a datagrid without any problems, but I want the data to show
up in textboxes and use some sort of move next, move previous, move
last, etc (like in VB6) command to navigate the data (using...
| |
by: amber |
last post by:
Hello,
What is the best method of clearing a datagrid?
I'm having issues with it not 'resetting' when I want to repopulate it.
I usually design my datagrids using the .net toolbar/properties, but had to
create this one programmatically (so I could add comboboxes).
I've never had this problem before, but now, whenever I try to repopulate my
dataset, it keeps my original data, and adds the column headers as new rows.
It seems to me that I...
|
by: dauphian |
last post by:
Hello,
I am new to .net and am trying to build a report application that
queries 4 different tables based on a id, and I need to return them in
the same table for easy viewing.
Basically, I have one querie that grabs all of the id's I need for the
other 4 queries, but I am not sure how to get them into a DataTable or
DataSet, or if that is the best way to do this. Seperately the queries
all work with no problems.
|
by: Tom |
last post by:
I have a dataTable being returned from my datalayet, I need to convert it to
a dataSet so I can do some data manipulation to it prior to populating my
datagrid. How can I convert the datatable to a dataset?
|
by: Ken Fine |
last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets
and DataTables should be able to answer fairly easily. The basic question is
how I can efficiently match data from one dataset to data in a second
dataset, using a common key. I will first describe the problem in words and
then I will show my code, which has most of the solution done already.
I have built an ASP.NET that queries an Index Server and returns a...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |