do I need to remove datatable before filling it again with a different query? - vb.net

I am filling a datatable called 'Reports', then clearing it and filling it with a different query. The problem is, when I fill it the second time, the datatable is not filled out correctly. There are extra columns and it is not in the same order as my query. I am using the dataset visualizer to see the contents of the datatable while i am debugging. Is there something I need to be doing to the datatable other than
ds.table("Reports").clear
before i fill the table again? I figured out if I remove the table completely before I fill it again, then the table is filled out right the second time.
Current code structure goes something like this when filling the table the second time:
ds.tables("Reports").clear
reportSQL = "select field1, field2 from table"
daReports = New NpgsqlDataAdapter(reportSQL, con)
daReports.Fill(ds, "Reports")
At this point, the table is filled out incorrectly. It only works when i add this before i fill the table:
ds.tables.remove("Reports")
Is this the right way to go about filling a datatable multiple times with different queries?

ds.table("Reports").clear only clears rows collection from the datatable (similar to ds.table("Reports").Rows.clear). In addition to that you need to clear columns collection:
ds.table("Reports").Columns.Clear()
This will get rid of extra columns

Related

VB.net add DataTable rows to DataGridView

I've tried to find a solution to this, but can't seem to find anything that quite matches my goal here. The answers I've found are either C# or are wanting the data to go from the DGV to the DT.
I have a datatable, which is built out using an SQL query. In this data table, I have a column "Lab ID" that I want to use as a search index. On my front end, there is a requirement for the Lab ID to be supplied. When the Lab ID is entered, the data table should be searched, and the resulting row (entire row) should be added to the datagridview. Each scan should add a row and not overwrite. I have specified the DGV columns at form load but would like it to grab the column names from the table if possible.
Here is my current front end - noting that the left datagridview is only temporary to confirm the data is coming out of SQL and references the same datatable I am wanting to use for the right DGV. The user will pick the dates, enter the Lab ID, then click Check. If the Lab ID checks out, it should be added.
The other thing I'll need to able to do is compare the results in the data table with what is in that datagridview - if an item in the table is missing in the datagridview, I need to be able to retrieve the missing Lab IDs (don't need the whole row for this one) and add those missing Lab IDs to a messagebox.
I don't have any code at this point because I've not found anything that works, with the exception of this:
dgScanned.Rows.Add(GetResults.Rows)
Which results in the string 'System.Data.DataRowCollection' being added to the 'Request ID' column.

VB.Net dataset rows count always returning 0

I created a table adapter for a stored procedure I have, which returns a row from a table. After adding it to the dataset, I try to loop through the row count of this table to retrieve the data but it keeps returning no rows. If I try to preview the data in the dataset designer, i get the row normally but when i try it in code i get nothing
For intI As Integer = 0 To Me.Ds1.SP_Get_Data_Communication_Parameters.Rows.Count - 1
Dim IP As String = Ds1.SP_Get_Data_Communication_Parameters.Rows(intI)("Remote_IP_address")
Next
A tableadapter is a device that moves data in either direction between a database and a datatable.
A datatable is part of a dataset (a dataset is a collection of datatable)., and is a client side representation of (some or all) of a database table.
To work with database data you use a tableadapter to transfer it from the database table to the datatable. You work with it, maybe edit it and maybe save it back to the database
From what you've described it sounds like you're not actually using a tableadapter to fill the datatable before inspecting it for data. The dataset designer is just a visual representation of the tableadapter and associated datatable classes; it doesn't mean that the database data is automatically available in your program
You'll need to have a code like:
Dim ta As New YourDatasetNameTableAdapters.SP_Get_Data_Communication_ParametersTableAdapter()
ta.Fill(Me.Ds1.SP_Get_Data_Communication_Parameters, any, parameters, the, sproc, needs, here)
Then you can look through the datatable and see the data the TA downloaded
Edit footnote:
If you make changes to the rows, like
For Each ro in Ds1.SP_Get_Data_Communication_Parameters
ro.FirstName = "John"
Next ro
Then you can send the changes back to the db using the Update method of the table adapter
at.Update(Ds1.SP_Get_Data_Communication_Parameters)
Update will run all different kinds of query, not just UPDATE. Newly added rows will be INSERT. Deleted rows will be DELETEd. Microsoft should really have called it SaveChanges

Saving DataTable to MS Access Table

Background information:
I have a DataGridView, I can load a table from MS Access into it and/or also add data. It is not databound (and I don't want t).
What I did previously was to delete the data from MS Access table before saving new data. But I would like to do it the proper way (+ there will be less chance of losing data).
Issue:
When I do
Adapter.Update(DataTable) 'OleDb.OleDbDataAdapter
the data simply gets added (inserted) at the end, like when you add rows to datagridview.
What I want to do is overwrite the data in MS Access table, so that it looks identically to newly saved data (rows that can be updated get updated, new ones added, removed ones deleted)
In other words, if I'm saving 6 rows, I want to see 6 rows in database, not more not less.
I also tried loading the data, then changing it, and saving it back, but the result is the same. Eg if I load 5 rows, and save 5 rows, I end up with 10 rows in it after that save.
Adapter = New OleDb.OleDbDataAdapter("SELECT * FROM " & DB_TableName, DB_Connection)
Dim TempDataTable As New DataTable
BotDB_Adapter.Fill(TempDataTable )
'edit data here
Adapter.Update(TempDataTable)
TLDR : How do I save a DataTable to MS Access table so that it overwrites data already in it.
It was due to row state being "added", the only way to fix it is altering individual row states to proper states.
I do not see other way to solve this so I'll close it as there aren't any better answers.

Handling multiple layers of Data in DataTables?

I want to use DataTables to store multiple "layers" of data.
Some Procedure computes means, absolute values and percentages for me. Theese three layers have excactly identical ordered rows and columns, but the values change.
When I delete or insert a row, similar operations must be made to the other two tables as well. Has somebody a good Idea?
Further explanation:
Dim absTable as DataTable
Dim meanTable as DataTable
Dim percTable as DataTable
abstable.rows(2).delete
How can I guarantee, that this operation is executed on the other two tables as well?
You might want use a DataSet instead of multiple DataTable. Because, in DataSet, you can manipulate multiple DataTable using the .Tables(Index|TableName) property witch assure you to do operations on the right table. You need an higher hierarchical object to do what you want.
So if you want to delete a row in all tables, just loop on the desired tables and call the delete method:
Dim aDataSet as New DataSet
Dim absTable as New DataTable
Dim meanTable as New DataTable
Dim percTable as New DataTable
aTable.Tables.Add(absTable) aTable.Tables.Add(meanTable) aTable.Tables.Add(percTable)
For each table as DataTable in aDataSet.Tables
table.rows(2).delete
Next
You can also have cross Tables references to ensure or manipulate or do whatever you want on it.
aDataSet.Tables(0) = aDataSet.Tables(1) 'only if the talbes 0 and 1 have same columns
Hope this is what you asked for.
see http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx for more details on DatsSet.

Display Headers in Infragistics ULTRAGRID when no data in table

I am fetching data in from an SQL table using a DataSet in VB.Net. When there is data in table, it displays the data properly in the grid, but when there is no data in the table, it only shows the UltraGrid's basic view.
How can I display the column names of the table as headings of the UltraGrid even when there is no data in Table?
Thanks for the reply, but I think the problem that JD is having is a bit different from mine - in my application the data got fetched properly from SQL Server. My problem is that when there is no data in the table, I want to display the columns of the table as the headings of the grid with 0 rows. This is not happening.
It just shows a message box saying that no data is found, and the UltraGrid shows as it does by default in the application.
This is discussed in this Infragistics forum thread.
Do you know what the headers of the columns will be or is it dynamic based on the data in the table? If you know beforehand you can create the columns with the appropriate headers in an empty dataset and assign that to the grids datasource.
I notice this same behavior when I manually create a datatable and assign it as the grid's datasource. If the data table is empty, all the column header info that was previously set on the grid is lost. My solution to this was to never actually give it an empty table, if I have no rows in my table, at least have all the columns defined.
DataTable table = new DataTable("fooTable");
table.Columns.Add("fooCol1", typeof(long));
table.Columns.Add("fooCol2", typeof(string));
table.Columns.Add("fooCol3", typeof(bool));
myUltraGrid.DataSource = table;
By never setting the grid to an empty table, you keep your header info.