Add only new items from listbox to SQL - sql

i have a listbox that users can add Countries Names into it anytime.
now i need to only add new data to check if the data is new then insert it to sql.
my code not work:
For Each i As String In listbox1.Items
Dim sql = "select * From Countries where CountryName=N'" & i & "'"
Dim adp As New SqlClient.SqlDataAdapter(sql, SQlconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
Dim dr = dt.NewRow
dr!CountryName = i
dt.Rows.Add(dr)
Dim cmd As New SqlClient.SqlCommandBuilder(adp)
adp.Update(dt)
End If
Next

If you're loading the existing names from the database in the first place then there's no need for you to check anything. Simply query the database to populate a DataTable and bind that to the ListBox. When the user adds a new country, add it to the DataTable and it will automatically show up in the ListBox. When it's time to save, just use the same data adapter to save the DataTable and only the new records will be saved.

Related

Search by datagridview1 column1 values

i using this code to search by textbox to get the related data for the ID
Dim connetionString As String
Dim cnn As SqlConnection
connetionString="DataSource=IP;InitialCatalog=DB;UserID=sa;Password=password"
cnn = New SqlConnection(connetionString)
Dim com As String = "SELECT * FROM Table WHERE ID= '" & TextBox1.Text & "'"
Dim Adpt As New SqlDataAdapter(com, cnn)
Dim ds As New DataSet()
Adpt.Fill(ds, "Table")
DataGridView1.DataSource = ds.Tables(0)
What iam trying to do, i need to do the same code but with datagridview column1
Explaining:
i've already add button to upload data to datagridview the data include ID's
i need to search by those ID's in bulk, that included to the datagridview then post the income data to another datagridview,
Thanks in advance.
First, I think you should correct your existing code to use SQL Parameters.
Dim com As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com.Parameters.Add("#Id", SqlDbType.VarChar).Value = TextBox1.Text
Dim Adpt As New SqlDataAdapter(com)
Now to use the value in the first row, first column
Dim strId As String = DataGridView1.Rows(0).Cells(0).Value.ToString
Dim com2 As New SqlCommand("SELECT * FROM Table WHERE ID= #Id;", cnn)
com2.Parameters.Add("#Id", SqlDbType.VarChar).Value = strId
Dim Adpt2 As New SqlDataAdapter(com2)
I had to guess at the datatype. Please check your database to get the correct type. I guessed it was some type of string since you had it enclosed in single quotes.

Dynamically add listbox columns based on columns in an Access Database?

I would like to do a populate a column in my listbox for each field in my Access Database.
Right now I have to manually add the field:
QUERBOX.Columns.Add("Requestor Name", 200, HorizontalAlignment.Left)
How can I adapt my code below to automatically add columns each time I run the sub?
Dim queryString As String = "SELECT * FROM Table1;"
Dim connection As OleDbConnection
Dim command As OleDbCommand
Dim data_reader As OleDbDataReader
querbox.clear
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\apt.accdb")
connection.Open()
command = New OleDbCommand(queryString, connection)
data_reader = Command.ExecuteReader
If data_reader.HasRows Then
While data_reader.Read
Dim newitem As New ListViewItem()
newitem.Text = data_reader.GetValue(0) 'first column
newitem.SubItems.Add(data_reader.GetValue(1)) 'second column
QUERBOX.Items.Add(newitem)
End While
End If
As Plutonix suggested in comments, a DataGridView would probably be best suited, but for the sake of answering the question here's how I've done something similar:
connection.Open()
'' Fill a DataTable with all of the Column schema for the given table of 'Table1'
Dim schemaTable As DataTable = connection.GetSchema("Columns", New String() {Nothing, Nothing, "Table1", Nothing})
'' Iterate through each column in the Schema Table
For i = 0 To schemaTable.Rows.Count - 1 Step 1
'' Declare a new item for the list
Dim newItem As New ListViewItem()
newItem.Text = schemaTable.Rows(i)!COLUMN_NAME.ToString()
newItem.SubItems.Add(schemaTable.Rows(i)!COLUMN_NAME.ToString()
'' Add new item to the interface
QUERBOX.Items.Add(newItem)
Next
connection.Close()
This has helped me in projects where the user is familiar with the database structure and may need to select the fieldname as part of the business logic. For example, allowing someone with little database knowledge to standardize the records within a given field.

clear all records from data set in vb.net

i am working on vb.net windows form..in my button click i have given code like this:
Dim rpt As New DelivaryPerformance
Dim ds As New DataSet
Dim da As New SqlDataAdapter
Dim rpt As New DelParkingtype
Dim locid As Integer = RecordID("Locid", "Location_tbl", "LocName", CmbLocations.Text)
Dim cmdstatus As New SqlCommand("IBS_DelivaryStaus", con.connect)
cmdstatus.CommandType = CommandType.StoredProcedure
cmdstatus.Parameters.Add("#locid", SqlDbType.Int).Value = locid
da.SelectCommand = cmdstatus
da.Fill(ds)
If (ds.Tables(0).Rows.Count > 0) Then
rpt.SetDataSource(ds.Tables(0))
' CrystalReportViewer1.ReportSource = rpt
End If
while i am clicking each time in my button click,,i want to clear all records from my data set,,how i can clear my all records from my dataset
If you want to clear all rows from the tables contained in a dataset you could use
ds.Clear()
MSDN reference
DataSet.Clear
This however, removes the records, not the tables schema. If you want to get rid of the tables also then you need to use
ds.Tables.Clear()
However, in the context of your code, the dataset is created as new, and so it is already empty until you fill it with the appropriate command, so it is not clear in which context you need to empty it.

Copy full row from a table's database

I'm using VB.Net and I have a database contains two tables "Personne" and "PersonneCopy" ( Same schema as Personne [4 columns = N°, FirstName, LastName, B_Day])
I load the first table in a Datagridview all it's fine with that, here's the code:
Dim Connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source= .\BD.mdb")
Dim SqlRequete As String = "Select * From Personne"
Dim Adapter As New OleDbDataAdapter(SqlRequete, Connection)
Dim DataSet As New DataSet
Adapter.Fill(DataSet, "Tb1")
Dim DataTable As New DataTable
DataTable = DataSet.Tables("Tb1")
DataGridView1.DataSource = DataTable
Is there any way to add the full row that was selected from DataGridView1 to the table "PersonneCopy"?
And by the way I tried this code with an other Datagridview to get the index of the row selected and it works.
Dim Index As Integer = DataGridView1.Item(0, DataGridView1.CurrentRow.Index).Value
Dim SqlRequete As String = "Select * From Personne where N°=" & Index.ToString
Dim Adapter As New OleDbDataAdapter(SqlRequete, Connection)
Dim Ds As New DataSet
Adapter.Fill(Ds, "Tb")
DataGridView2.DataSource = Ds.Tables(0)
Try like this
Dim drCopy as DataRow
Dim tbCopy as Datatable
tbCopy = .... 'PersonneCopy table
drCopy=tbCopy.NewRow()
For i as integer=0 to drCopy.columns.count-1
drCopy(i)=Ds.Tables(0).rows(0).items(i) '---------> or you can change rows(0)
Next
tbCopy.Rows.Add(drCopy)

VB.NET 2010 to connect to ms access database

Can someone show me how to connect vb.net 2010 to ms access database to get the data and display it in vb.net application that I am doing right now. My project is that I am doing dictionary application with vb.net so every time i put new word in search box, I want the vb.net to get the definition from ms access and display it in the application.
And code snippet would be great or tutorial
'Grabs data from a table and posts it into a ListView
Dim Table_ As String = "Table1"
Dim query As String = "SELECT * FROM " & Table_
Dim MDBConnString_ As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TestDatabase.mdb;"
Dim ds As New DataSet
Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString_)
cnn.Open()
Dim cmd As New OleDbCommand(query, cnn)
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, Table_)
cnn.Close()
Dim t1 As DataTable = ds.Tables(Table_)
Dim row As DataRow
Dim Item(2) As String
For Each row In t1.Rows
Item(0) = row(0)
Item(1) = row(1)
Dim NextListItem As New ListViewItem(Item)
ListView1.Items.Add(NextListItem)
Next