Trying to get this bit of code to enter a player name fill it in the datarow then update it in the DB. It currentley updates in the datarow but I can not get the code right to update the DB. Could you please help cheers.
Using da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Team1", con)
da.Fill(ds, "BPAWA")
txtFirstName.Text = ds.Tables("BPAWA").Rows(0).Item(1)
T1P1 = InputBox("Name of Player 1")
ds.Tables("BPAWA").Rows(0).Item(1) = T1P1
ds.Tables("BPAWA").AcceptChanges()
ds.AcceptChanges()
da.Update(ds, "BPAWA")
MsgBox("Player 1 Added Successfully")
End Using
You OleDbDataAdapter is not linked to an actual command neither you seem to have created the adapter in your code. See an example here
Missing a line like this
Dim da As OleDbDataAdapter = new OleDbDataAdapter(selectCommand, connection);
so I could rewrite your code in this way (Avoiding the global variables)
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
Dim dbProvider As String = "PROVIDER=microsoft.ace.oledb.12.0;"
Dim dbSource As String = "Data Source = C:\BP_Table_Project.accdb"
Dim ds as DataSet = new DataSet()
Using con As OleDbConnection = new OleDbConnection()
con.ConnectionString = dbProvider & dbSource
con.Open()
MsgBox("Connection With Database Established", 0, "Database Connection")
Using da as OleDbDataAdapter = new OleDbDataAdapter("SELECT * FROM Team1", con)
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(da)
da.Fill(ds, "BPAWA")
......
da.Update(ds, "BPAWA")
MsgBox("Player 1 Added Successfully")
End Using
MsgBox("Connection With Database Closed", 0, "Database Connection")
End Using
End Sub
Before da.Update(ds, "BPAWA")
Add:
ds.Tables("BPAWA").AcceptChanges()
ds.AcceptChanges()
Related
I have problem with my code which fills multi tables into my dataset. It loads all contents contained in tables of my database to only one table in dataset. My code is shown below. How to load those tables from database into a dataset , that has the same number of tables and contents.
Private Sub Filldataset()
Private cnn As OleDbConnection
Private dt As New DataTable
Private da As New OleDbDataAdapter
Private cmd As New OleDbCommand
Private ds As New DataSet
Dim tblrestrictions As String() = New String() {Nothing, Nothing, Nothing, "TABLE"}
Dim userTables As DataTable = Nothing
userTables = cnn.GetSchema("Tables", tblrestrictions)
Dim i As Integer
For i = 1 To userTables.Rows.Count - 1 Step 1
cnn = New OleDbConnection(Str)
cnn.Open()
cmd = cnn.CreateCommand
cmd.CommandText = "select * from" & " " & userTables.Rows(i)(2).ToString
dt.Clear()
da.SelectCommand = cmd
da.Fill(dt)
da.Fill(ds)
Next
cnn.Close()
MessageBox.Show(ds.Tables.Count)
End Sub
Connections can be created elsewhere but should not be opened or closed until directly before an directly after you use them. You will have to adjust this code for an Oledb application.
Private Sub GetData()
cn.Open()
Dim dt As DataTable = cn.GetSchema("Tables")
cn.Close()
Dim ds As New DataSet
Dim row As DataRow
For Each row In dt.Rows
Dim strTableName As String = row(2).ToString
Dim strSQL As String = "Select * From " & strTableName
Dim cmd As New SqlCommand(strSQL, cn)
Dim da As New SqlDataAdapter
da.SelectCommand = cmd
da.Fill(ds, strTableName)
Next
Debug.Print(ds.Tables.Count.ToString)
End Sub
I scoped several variables locally that you will want to scope to the class like the dataset
hi I'm trying to insert a row to my database but I still get this error message (see below):
I used the methode using to be sure that the connection will be closed.
below the script for the insert button :
Private Sub MTB_Insert_Click(sender As Object, e As EventArgs) Handles MTB_Insert.Click
cmd = New SQLite.SQLiteCommand
Using con As New SQLite.SQLiteConnection(constr)
Try
'con.ConnectionString = constr
con.Open()
cmd.Connection = con
cmd.CommandText = "Insert Into Material_Type (Material_Type,Material_Type_Description) values(#Type,#Description)"
cmd.Parameters.AddWithValue("#Type", MTTB_Material_Type.Text)
cmd.Parameters.AddWithValue("#Description", MTTB_Material_Description.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Successful Added Data")
'Calling function load data
loadDatadg1()
'con.Dispose()
'con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Sub
other parts of my code :
Public Class F_Update
Dim con As SQLite.SQLiteConnection
Dim cmd As SQLite.SQLiteCommand
Dim da As SQLite.SQLiteDataAdapter
Dim constr As String = "Data source =Database1.db"
Public Sub loadDatadg1()
Using con As New SQLite.SQLiteConnection(constr)
'con = New SQLite.SQLiteConnection(constr)
con.Open()
Dim str As String = "select * from Material_type"
da = New SQLite.SQLiteDataAdapter(str, con)
Dim ds As New DataSet
da.Fill(ds, "Tb.Material_Type")
dg1.DataSource = ds.Tables("Tb.Material_Type")
'da.Dispose()
'con.Close()
End Using
End Sub
Private Sub F_Update_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Using con As New SQLite.SQLiteConnection(constr)
' con = New SQLite.SQLiteConnection(constr)
con.Open()
'MsgBox("OK")
'Calling the function Load form
loadDatadg1()
End Using
End Sub
another part :
Private Sub B_login_Click(sender As Object, e As EventArgs) Handles B_login.Click
Dim connectionString As String = "Data source =Database1.db"
Dim mSQL As String = "select User_Name,User_Password from User where (User_ID = 1 and User_Name = '" & T_User_Name.Text & "' and User_Password = '" & T_User_Password.Text & "')"
Dim dt As DataTable = Nothing
Dim ds As DataSet
Dim rd As SQLiteDataReader
Using con As New SQLiteConnection(connectionString)
Try
con.Open()
Dim cmd As New SQLiteCommand(mSQL, con)
cmd.ExecuteNonQuery()
rd = cmd.ExecuteReader
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
If rd.HasRows Then
F_Update.Show()
Me.Close()
Else
MessageBox.Show("Invalid User Name or Password")
End If
'con.Close()
End Using
End Sub
End Class
can some one help me please ? Thanks in advance
I'm trying to populate a combobox with data from SQL Server. This is my code so far. There are asterisks around the errors. Also, ignore the comments.
Private Sub frmOriginal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim connetionString As String = Nothing
Dim sqlcon As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter()
Dim ds As New DataSet()
Dim i As Integer = 0
Dim sql As String = Nothing
connetionString = "Data Source = RENEE\SQLEXPRESS;Initial Catalog=Stocks;Integrated Security = True"
sql = "select * from TickerSymbol"
sqlcon = New SqlConnection(connetionString)
Try
sqlcon.Open()
command = New SqlCommand(sql, sqlcon)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
sqlcon.Close()
cboID.DataSource = ds.Tables(0)
cboID.ValueMember = "TickerSymbol"
cboID.DisplayMember = "TickerSymbol"
Catch ex As Exception
'MessageBox.Show("Can not open connection ! ")'
End Try
End Sub
Private Sub cboID_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboID.SelectedIndexChanged
Dim dr As SqlDataReader
Dim command As New SqlCommand *(queryString, connection)*
Dim dataReader As SqlDataReader = command.ExecuteReader()
Dim sqlcon As SqlConnection
Dim cmd As SqlCommand
sqlcon = New SqlConnection
sqlcon.ConnectionString = "Data Source = RENEE\SQLEXPRESS;Initial Catalog=Stocks;Integrated Security = True"
Try
sqlcon.Open()
cmd = New SqlCommand
cmd.CommandText = " select * from TickerSymbol where TickerSymbol = '" & cboID.Text & "'"
cmd = New SqlCommand(cmd.CommandText, sqlcon)
dr = cmd.ExecuteReader
While dr.Read()
'TxtID.Text = dr.GetInt32(0)'
'TxtSN.Text = dr.GetString(1)'
'TxtGender.Text = dr.GetString(2)'
'TxtPhone.Text = dr.GetInt32(3)'
'TxtAdrress.Text = dr.GetString(4)'
lblCompanyName.Text = dataReader.GetString(1)
lblPurchasePrice.Text = dataReader.GetSqlMoney(2)
lblQtyPurchased.Text = dataReader.GetInt32(3)
lblPurchaseDate.Text = dataReader.GetDateTime(4)
End While
sqlcon.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
sqlcon.Dispose()
End Sub
Please use parameterized queries as this will format values properly e.g. apostrophes in text will escape properly with parameters while without you must handle them, dates will be formatted properly too. Code is much cleaner also.
Example, syntax for Framework 3.5 and higher. If a connection string is used more than once then consider placing it in a private variable or under My.Settings under project properties.
Using cn As New SqlConnection With {.ConnectionString = "Data Source = RENEE\SQLEXPRESS;Initial Catalog=Stocks;Integrated Security = True"}
Using cmd As New SqlCommand With {.Connection = cn, .CommandText = "select * from TickerSymbol where TickerSymbol = #TickerSymbol"}
cmd.Parameters.AddWithValue("#TickerSymbol", cboID.Text)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
'
'
'
End While
End If
End Using
End Using
I am having issues returning data from a MS SQL database.
The code is returning 'System.data.datarowview' instead of the results of my query. The code for the sub is:
Public Sub newquery(query As String)
Dim SQLConn As SqlConnection = New SqlConnection
Dim SqlCommand As New SqlCommand
SQLConn.ConnectionString = "Data Source=.\testing;Initial Catalog=eurostyle;Integrated Security=SSPI;"
SqlCommand = New SqlCommand(query, SQLConn)
Try
SQLConn.Open()
sqlDA = New SqlDataAdapter(SqlCommand)
sqlDataset = New DataSet
sqlDA.Fill(sqlDataset)
SQLConn.Close()
listbox1.DataContext = sqlDataset
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
I am new to WPF and I'm sure that is only something trivial.
Any help is greatly appreciated!
DataGrid view would be more appropriate to display results from database.
Try this:
Public Sub newquery(query As String)
Dim SQLConn As SqlConnection = New SqlConnection
Dim SqlComm As New SqlCommand
Dim dbDataSet As New DataTable
Dim bSource As New BindingSource
Dim sqlDA As New SqlDataAdapter
SQLConn.ConnectionString = "Data Source=.\testing;Initial Catalog=eurostyle;Integrated Security=SSPI;"
Try
SQLConn.Open()
SqlComm = SQLConn.CreateCommand
SqlComm.CommandText = query
SqlComm = New SqlCommand(zapytanie, myConn)
sqlDA.SelectCommand = SqlComm
sqlDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
DataGridView.DataSource = bSource
sqlDA.Update(dbDataSet)
SQLConn.Close()
Catch ex As SqlException
MessageBox.Show("Query Error: " & ex.Message)
End Try
I have accessed a database in VB.NET using the following code:
Public Class Form1
Private Sub btnLoad_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLoad.Click
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = AddressBook.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Address Book")
MsgBox("Database Open")
con.Close()
MsgBox("Database Closed")
txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)
txtSurname.Text = ds.Tables("AddressBook").Rows(0).Item(2)
End Sub
End Class
However in line "txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)", it gives me an exception saying that an instance of an object should be created. I am not understanding what exactly the problem is. How can I create an instance, and of WHAT should I create an instance?
You used a space in
da.Fill(ds, "Address Book")
But no space in
txtFirstName.Text = ds.Tables("AddressBook").Rows(0).Item(1)
For the name of the table. Therefore a table with name "AddressBook" does not exist.