The Visual Basic sample code below stores the retrieved data into the myVariable variable. However, it does not get stored into the ds "myTable" table.
The original data is coming from a SQL DB, which is stored into a Datatable. The Datatable shows the data in the form of a CheckedListBox (the checkedlistbox shows two forms of data: UCSNumber, UCSDesc
Dim ds As New DS_UsersStatus()
Dim myVariable = LstBranch.CheckedItems.Cast(Of DataRowView).Select(Function(o) ds.Tables("myTable").Rows.Add(o.Item("UcsNumber"), o.Item("ucsDesc")))
Related
I'm trying to filter rows of datagridview based on textBox value. I want to remove all rows which don't have values like in column NAZIV. I'm new in visual basic.
Name of datagridView is dvgIQ
I've tried this but it's not working.
Sub filter
If textBox1.Text.Length>=3 Then
For i As Integer = dvgIQ.Count-1 To 0 Step -1
If Not dvgIQ.Rows(i).Cells(4).Value.ToString(textBox1.Text.ToLower) Then
dvgIQ.Rows.RemoveAt(i)
End If
Next i
Else If textBox1.Text.Length>0 And textBox1.Text.Length<3 Then
MsgBox("warning")
End If
End Sub
Thanks in advance
You should start by populating a DataTable with your data. If the data comes from a database then use a data adapter and call its Fill method or else use a data reader and call Load on the DataTable. If the data is not from a database then you can build and populate the DataTable manually.
Next, bind the Datatable to a BindingSource that you added to the form in the designer. Finally, bind the BindingSource to your grid:
BindingSource1.DataSource = myDataTable
DataGridView1.DataSource = BindingSource1
To filter the grid, you simply set the Filter property of the BindingSource, e.g.
BindingSource1.Filter = $"SomeColumn LIKE '{TextBox1.Text}%'"
That will exclude all rows where SomeColumn does not start with the text in TextBox1. You should read the documentation for the Filter property and follow the specified link to learn what syntax is supported by ADO.NET. It is a small subset of SQL.
I have a Microsoft Access database containing three tables - "Class 1", "Class 2" and "Class 3". In each table, there are three columns: "ID", "Name" and "Score". In Visual Basic, I have two variables - name and score. I would like to add the name and score on a new row in the database, using datasets. I looked at the MSDN tutorial, but I don't really understand it:
https://msdn.microsoft.com/en-us/library/5ycd1034.aspx
Dim newCustomersRow As NorthwindDataSet.CustomersRow
newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
newCustomersRow.CustomerID = "ALFKI"
newCustomersRow.CompanyName = "Alfreds Futterkiste"
NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)
Where does the "NorthwindDataSet1" come from? I understand where "NorthwinDataSet" comes from = that is the name of the data set created via the data source tab.
I am not sure how to add the record to the table. I started like this:
Dim newScoreRow As scoresDataSet.Class_1Row
I am not sure how to write the next line of code because I don't understand the second line of code in the MSDN documentation.
After that second line of code has been written, I understand that I write something like:
newScoreRow.Name = name
newScoreRow.Score = Str(Score)
In summary, where does the "NorthwindDataSet1" come from? How would I finish my code?
NorthwindDataSet1 is an instance of a Typed DataSet called NorthWindDataSet. Take a look at these MSDN Pages Working with Datasets in Visual Studio and How to: Create a Typed Dataset
Following the second link I posted above I created a new DataSet called scoresDataSet and I added a table called Class_1 and under the table I created the columns ID, Name, and Scorejust like you had in your question.
The Typed DataSet Class file that was generated for me is way to large to post here on SO, but here is the code that I used to add a new row to the Class_1 table in the DataSet. The addNewRowButton is just a button on the form. I also added two textboxes on the form called nameTextBox and scoreTextbox.
Private ScoresDataSet1 As New scoresDataSet()
Private Sub addNewRowButton_Click(sender As Object, e As EventArgs) Handles addNewRowButton.Click
Dim name As String = nameTextBox.Text
Dim score As String = scoreTextbox.Text
Dim newScoreRow As scoresDataSet.Class_1Row
newScoreRow = ScoresDataSet1.Class_1.NewClass_1Row
newScoreRow.Name = name
newScoreRow.Score = score
ScoresDataSet1.Class_1.Rows.Add(newScoreRow)
End Sub
The trick here is that your data actually exists in an Access Database so rather than manually creating an DataSet like I did, you'll want to Add a Data Source to your project using the Data Source Configuration Wizard and choosing Microsoft Access Database File.
In visual Studio 2012, just go to:
Project
Add New Data Source...
Database
Dataset
New Connection...
Change...
And choose the appropriate option and fill out the other fields in those windows. Once you've completed that and you build your project you should have a new DataSet object and associated TableAdapters in the toolbox under your application's Components from the form designer.
Once you add a copy of your newly created DataSet and associated TableAadapter to your form you can use the TableAdapters to populate data into your dataset from the access database and you can use the TableAdapters to store data into your access database.
I use below code in try catch block but it gives exception"'table' argument cannot be null. Parameter name: table" .My table name is caste and that table two columns are there srno and castename .But it say that my table has no data.Memory table is a datatable.
Dim Dset As New DataSet()
Dset = New DataSet()
Dset.Tables.Add(MemoryTable)
DataGridView1.DataSource = Dset.Tables("caste")
I tried data connect with database with using datasource but it gives service pack 1 error
'One is to use data binding on your TextBox controls and assigning the same DataSource.but its gives error
You don't show the definition of MemoryTable, but you do say it's a data table. If it's an object of type System.Data.DataTable, then it will have a property called TableName.
When you access a DataTable in a DataSet with a string index value, the value you are passing is the table's TableName property. So Dset.Tables("caste") is looking for a DataTable whose TableName property is set to "caste". If it can't find one, it will return Null. That looks like what's happening.
So set MemoryTable.TableName to "caste" and the error may go away.
I assume that MemoryTable actually has rows in it? If not, that may be a reason why you're getting the message about your table having no data.
So your code should look something like this:
Dim Dset As New DataSet() ' You don't have to do a separate assignment to Dset
' if you use New in the declaration, so we can omit that line.'
MemoryTable.TableName = "caste"
Dset.Tables.Add(MemoryTable)
DataGridView1.DataSource = Dset.Tables("caste")
And, actually, you can use MemoryTable as your data source without having to add it to a DataSet, unless you need to for some other reason.
DataGridView1.DataSource = MemoryTable
I hope this helps.
Im using VB.net and I have a variable that contains a string which is the name of another variable. I want to access that variable based off of that string. The scenario goes like this
I am doing a log DataTable with variable name "LogChanges"
Now I have a DataTable variable name "dtbEmployee"
the value "dtbEmployee" will be stored in DataTable "LogChanges" as string
Now
while i loop through the "LogChanges" DataTable and get the "dtbEmployee" in string. Is there a way to use this string to point back to the "dtbEmployee" DataTable
Something like this
For Each dr As DataRow In LogChanges.Select("", "DtbId")
If dr.Item("RecordSeQ") > 0 Then
Dim tempDataTable As DataTable
tempDataTable = "Convert dr.Item("dtbNAme")" to DataTable code
End If
Next
Note: RecordSEQ is just a counter to see how many records have been modified for this table
You would need to store the DataTable against that name somewhere, e.g. the Tables collection of a DataSet or a Dictionary. If the variable is a field and you have the object that has the correct DataTable assigned to that field then you could use Reflection but that would be cumbersome at best. The variable is not the DataTable so you have to some actual relationship to use to get the DataTable via the use of the name.
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.