For each connection in an array called ALLconn, I would like to compare it to my sql table. If exist, then add to my listview. Here is my code below, but it does not seem to work:
Dim LoginFilter As Object
Dim SelCurrAllSessions As SqlClient.SqlCommand = New SqlClient.SqlCommand("Select * from CurrAllSessions", LFcnn)
SelCurrAllSessions.CommandType = CommandType.Text
LoginFilter = SelCurrAllSessions.ExecuteReader
For Each conn In AllConn
While LoginFilter.Read()
If conn.UserName.ToString() = LoginFilter.Item(0) Then
ListBox1.Items.Add(LoginFilter.Item(0))
End If
End While
Next
Well you need to change the order of the loops
While LoginFilter.Read()
For Each conn In AllConn
If conn.UserName.ToString() = LoginFilter.Item(0).ToString Then
ListBox1.Items.Add(LoginFilter.Item(0).ToString)
Exit For
End If
Next
End While
This is necessary because in your original code, the internal while run till the end of the data loaded from the database then, when you try to check the next conn, you cannot reposition the reader at the start of the data loaded by the database.
It's the other way around, use Contains to check if the string is contained in the collection, then you can add it to the ListBox:
Using LoginFilter = SelCurrAllSessions.ExecuteReader()
While LoginFilter.Read()
Dim connection = LoginFilter.GetString(0)
If AllConn.Contains(connection) Then
ListBox1.Items.Add(connection)
End If
End While
End Using
Actually, your Question is uncompleted but still as per my understanding your trying to read result of SqlCommand.ExecuteReader(). When you read that Result it will reading from first to last and that is only one time you can read. if you try to read again it will show up an error because no more content to read from an object Loginfilter.
So, Either you can store that reading into an array and continue with your Foreach and while logic with newly created array or you can do reading from LoginFilter and compare into forech connections in AllConn array or List.
Please feel free to response me if you need more explaination, I will send you in C# version.
Related
so I'm using SQLite in a VB.net project with a populated database. I'm using the Microsoft.Data.Sqlite.Core and System.Data.SQLite NuGet package libraries. So the problem presents when I'm trying to get the result of a query. At first the SQLiteDataReader gets the response and all the elements of the desired table. I know this cause in the debugger I have a breakpoint after the setting the object and when I check the parameters of the SQLiteDataReader object the Result View shows all the elements of my table, but as soon as I remove the mouse from the object and check it again the Result View turns out empty, without even resuming with the next line of code. Does anyone know if its a known bug or something cause Ive used this exact method of querying a table in another project and it works.
The code:
Public Function RunQuery(com As String)
If CheckConnection() Then
command.CommandText = com
Dim response As SQLiteDataReader
response = command.ExecuteReader
Dim len As Integer = response.StepCount
Dim col As Integer = response.FieldCount
Dim resp(len, col) As Object
For i = 0 To col - 1
Using response
response.Read()
For j = 0 To len - 1
resp(i, j) = response.GetValue(j)
Next
End Using
Next
Debugger with populated result view
Debugger with empty result view
edit: added the for loop to show that its not only on the debugger that the result view is empty. When using response.Read() it throws an exception "System.InvalidOperationException: 'No current row'"
As I have told you in the comment, a DataReader derived class is a forward only retrieval object. This means that when you reach the end of the records returned by the query, that object is not capable to restart from the beginning.
So if you force the debugger to enumerate the view the reader reaches the end of the results and a second attempt to show the content of the reader fails.
The other part of your problem is caused by a misunderstanding on how to work on the reader. You should loop over the Read result not using a StepCount property. This property as far as I know, is not standard and other data providers don't support it. Probably it is present in the SQLite Provider because for them it is relatively easy to count the number of records while other providers don't attempt do calculate that value for performance reasons.
However, there are other ways to read from the reader. One of them is to fill a DataTable object with its Load method that conveniently take a DataReader
Dim data As DataTable = New DataTable()
Using response
data.Load(response)
End Using
' Now you have a datatable filled with your data.
' No need to have a dedicated bidimensional array
A DataTable is like an array where you have Rows and Columns instead of indexes to iterate over.
After going over multiple questions/answers on Stackoverflow and other boards I'm still lost on why I can't update an Access database from a datatable. I'm trying to take data from a datatable and insert that data into an Access table if it is blank, and replace the table if it already has data. I can successfully replace the table, but the data from the datatable does not get added.
However, the method which I'm using does not appear to work. My datatable comes from a bound datagridsource and the Access layer is called like this:
ConnectedDB.UpdateTable(DBTable, bsDataSource.DataSource)
Where ConnectedDB is the Access Layer class, DBTable is the string containing the Access table name, and bsDataSource is the bound data. As you can see, I passed the .Datasource to turn it into a datatable.
Here is the original (pre-Jan 29th) section of my work to add the datatable back into the Access table:
Public Function UpdateTable(strTable As String, dgDataTable As DataTable) As Boolean
Dim DS As New DataSet
dgDataTable.TableName = strTable
DS.Tables.Add(dgDataTable)
Using OpenCon = New OleDb.OleDbConnection(strConnectionString)
Using DataAdapter As New OleDbDataAdapter("SELECT * FROM " & strTable, OpenCon)
Dim DBcmd As OleDbCommandBuilder = New OleDbCommandBuilder(DataAdapter)
DBcmd.QuotePrefix = "["
DBcmd.QuoteSuffix = "]"
DataAdapter.UpdateCommand = DBcmd.GetUpdateCommand()
Try
OpenCon.Open()
DataAdapter.Fill(DS.Tables(strTable))
If DataAdapter.Update(DS.Tables(strTable)) > 0 Then
Return True
Else Return False
End If
Catch exo As Exception
MessageBox.Show(exo.Message)
Return False
End Try
End Using
End Using
End Function
My function tries to update an existing Access table with the name represented as strTable with the information in the datatable, dgDataTable from a datagridview. Each run hits the update check > 0 and returns a false which means syntax wise it should be working (i.e. no error messages). I have traced the table and it has all the data it should have (so the information is getting passed correctly from the grid through the update commands). I was playing with applying it in a dataset but I'm not sure I really need that.
I was tracing the variables through the update method and I think I found out why it won't update but I'm not sure what to do about it. The query it comes up with is like this:
UPDATE [RtoC] SET [R] = ?, [C] = ?, [Type] = ?, [Unknown] = ? WHERE (([R] = ?) AND ([C] = ?) AND ([Type] = ?) AND ((? = 1 AND [Unknown] IS NULL) OR ([Unknown] = ?)))
The Access table name is RtoC with fields R, C, Type, and unknown.
I'm thinking the "?" are not getting filled in causing the query to just not apply data back to Access. I'm not sure though how to set those items.
EDIT 1/29/20: I used the code changes I and jmcihinney document below and it does insert the lines into the Access table. This edit alters the question to be more specific about what I'm am trying to do, and how the datatable is created. Hopefully this clears up some wording on my part and provides some basis for the alteration of the row state.
The issue is that the Fill method of that data adapter calls AcceptChanges on the DataTable after populating it, thus there are no changes to save when you call Update.
That call to Fill shouldn't be there anyway though, because you don't want to retrieve any data, just save changes. You've got a whole lot of pointless code there. It should look more like this:
Public Function UpdateTable(strTable As String, dgDataTable As DataTable) As Boolean
Using DataAdapter As New OleDbDataAdapter("SELECT * FROM " & strTable, strConnectionString)
Dim DBcmd As OleDbCommandBuilder = New OleDbCommandBuilder(DataAdapter)
DBcmd.QuotePrefix = "["
DBcmd.QuoteSuffix = "]"
Try
Return DataAdapter.Update(dgDataTable) > 0
Catch exo As Exception
MessageBox.Show(exo.Message)
Return False
End Try
End Using
End Function
I took another tack at manipulating the database and in looking that up, I found the answer provided by jmcilhinney back in 2014! [Bulk Insert From DataTable to Access Database
In a for each loop across the rows of my datatable I set this:
row.SetAdded()
If I was filling I would have done something like:
DataAdapter.AcceptChangesDuringFill = True
Before the Fill command.
Unless this method has changed or there is a better way, I'll mark the link as the answer.
Thanks jmcilhinney....twice!
I need to Sort all DataTables in mt Dataset. I have tried using DefaultView, It's sorting my datatable but after the loop the datatable looks same without sorting.
This is what i tried:
For Each Dt As DataTable In AlbumListDs.Tables
Dt.DefaultView.Sort = "ImageData Asc"
Dt = DataTable.DefaultView.ToTable
Dt.AcceptChanges()
AlbumListDs.AcceptChanges()
Next
Please correct me if i did anything wrong.
The changes that you made to the DataTable when inside the loop are local to the element returned by the Iterator of the For Each.
MSDN says
Modifying Collection Elements. The Current property of the enumerator
object is ReadOnly (Visual Basic), and it returns a local copy of each
collection element. This means that you cannot modify the elements
themselves in a For Each...Next loop. Any modification you make
affects only the local copy from Current and is not reflected back
into the underlying collection.
So, when you recreate the DataTable with
Dt = DataTable.DefaultView.ToTable
the new Dt instance is not the same instance contained in the DataSet. And so your changes are lost at the same moment when you loop over another DataTable element.
This is in striking contrast on what you can do in C# where an attempt to change the iterator instance is immediately caught by the compiler and signaled as an error at compile time
Perhaps you could just change the DefaultView sort expression and leave the DataTable in its original order (Surely it will be better for your memory usage). When you need to loop in an ordered way, just use the DataView
For Each drv As DataRowView in DataTable.DefaultView
Console.WriteLine(drv("YourField").ToString())
Next
Or use a normal for...loop (BUT IN BACKWARD direction)
For x as Integer = AlbumListDs.Tables.Count - 1 To 0 Step -1
Dim dt = AlbumListDs.Tables(x)
dt.DefaultView.Sort = "ImageData Asc"
AlbumListDs.Tables.RemoveAt(x)
AlbumListDs.Tables.Add(dt.DefaultView.ToTable)
Next
AlbumListDs.AcceptChanges
Notice that you need to remove the previous table from the collection (Tables is readonly) and then add the new one. This is safer if you loop backward from the end of the collection to the first element to avoid possible indexing errors
I was able to achieve it in a different way. It might not be the best solution but works for what I needed to do.
objReturnDS.Tables(1).DefaultView.Sort = "RowID asc"
objReturnDS.Tables(1).DefaultView.ToTable(True)
Dim sortedTable = objReturnDS.Tables(1).DefaultView.ToTable(True)
objReturnDS.Tables(1).Clear()
objReturnDS.Tables(1).Merge(sortedTable)
I have a Dynamic Data LINQ to SQL ASP.Net Website in VB.NET, and am having a little trouble with Sorting of my GridView and a Search routine I have implemented. On Page_Load, the GridView is sorted by a field (Departments.department) in ASC order. However, when I perform a search using the code below, I get an error
The Data Source Does Not Support Sorting.
I'm assuming the problem comes when the Page_Load event tries to sort the data after a Search is made, because of the DataSource/ID.
Dim button = DirectCast(sender, Button)
If button.ID = btnMultiColumnSearchClear.ID Then
txbMultiColumnSearch.Text = [String].Empty
Else
Using Data As New wcPhonesDataContext()
Dim EmployeeNameString As String = txbMultiColumnSearch.Text
Dim SearchResults = Data.Employees.Where(Function(Employees) Employees.Employee.Contains(EmployeeNameString))
GridView1.DataSourceID = ""
GridView1.DataSource = SearchResults
GridView1.DataBind()
End Using
End If
SOLVED, but now I have a new problem, here is the code I used to solve this issue...
Dim button = DirectCast(sender, Button)
If button.ID = btnMultiColumnSearchClear.ID Then
txbMultiColumnSearch.Text = [String].Empty
Else
Using Data As New wcPhonesDataContext()
Dim EmployeeNameString As String = txbMultiColumnSearch.Text
Dim SearchResults = Data.Employees.Where(Function(Employees) Employees.Employee.Contains(EmployeeNameString))
GridView1.Sort("", SortDirection.Ascending)
GridView1.DataSourceID = ""
GridView1.DataSource = SearchResults
GridView1.DataBind()
End Using
End If
I have created a new error though. It occurs if I perform a second search without going BACK to the Employees table.
'GridView1' fired event Sorting which wasn't handled.
If you use a SqlDataSource and connect the gridview to the data source with a datasourceid, then sorting is done for you magically. You don't have to do anything to support it.
But if set the datasource to some object that you have created in code, sorting does NOT magically happen for you. When the user clicks on a column head, this fires an OnSorting event. You have to write code to handle the event. Typically this mean regenerating the data in the desired order, or regenerating the data and then sorting it.
For example, if you generate the data with a SQL query, I sometimes create a function that runs the SQL query and returns a DataSet. This function takes the sort field as a parameter, which it pastes into the SQL query. Then for the initial display call this function passing in the default sort order, and for the OnSorting call this function passing in the desired sort field.
I would like to fill a datatable with results from a SQL select statment but using a transaction. The reason that I am using a transaction is because I have a list of names (as a datatable), and I want to iterate through the list of names and select the database rows where the name = the name on the list. There are 500,000 names in the database and I only want to retreive the relevant rows. I have the code for the procedure as I think it should look like (untested) BUT I dont know HOW to place the data into a datatable .... so Im missing something where I declare the datatable and the 'fill' of that table , could someone help with this ? Or suggest how else I can get the information out of the batabase without looking up each name individually.
Using connection As New SQLite.SQLiteConnection(R2WconectionString)
connection.Open()
Dim sqliteTran As SQLite.SQLiteTransaction = connection.BeginTransaction()
Try
oMainQueryR = "SELECT NameID, Address, Ocupation FROM Employees Where Name= :Name"
Dim cmdSQLite As SQLite.SQLiteCommand = connection.CreateCommand()
With cmdSQLite
.CommandType = CommandType.Text
.CommandText = oMainQueryR
.Parameters.Add(":Name", SqlDbType.VarChar)
End With
'Prevent duplicate selects by using a dictionary
Dim NameInalready As New Dictionary(Of String, String)
For Each row As DataRow In TheLIST.Rows
If NameInalready.ContainsKey(row.Item("Name")) Then
Else
NameInalready.Add(row.Item("Name"), "")
cmdSQLite.Parameters(":Name").Value = row.Item("Name")
cmdSQLite.ExecuteNonQuery()
End If
Next
sqliteTran.Commit()
Catch ex As Exception
End Try
End Using
First, you don't need a transaction because you aren't updating the database.
Second, depending on the possible number of Names in TheLIST, it might be worthwhile for you to change the name selector to IN (i.e. SELECT * FROM Employees WHERE Name IN ('name1', 'name2'). However, if you expect more than about 10, this is probably not worth trouble.
Finally, you need to create a new DataTable to hold the results. Then you need to create a DataAdapter passing cmdSqlLite as the constructor parameter. And finally, replace your ExecuteNonQuery with DataAdapter.Fill(DataTable).
For example (after Dim cmdSQLite):
Dim oDataTable As New DataTable("Employees")
Dim oAdapter As New SqliteDataAdapter(cmdSQLite)
and replacing the ExecuteNonQuery line with:
oAdapter.Fill(oDataTable)
I will qualify this code by saying it may need some tweaks. I only work with class objects and collections, so my preference would have actually been to load a collection of Employee class instances.
I would have done that by replacing ExecuteNonQuery with ExecuteReader and then the loading the read data into a new class instance. This type of approach resolves various issues with serializing the data across service boundaries (i.e. Xml for web services) and also lets you embed business logic, if needed, into the classes.