OLEDB - select Count not working - vb.net

Find below what I've done so far, but unfortunately it's not working.
Private BS as New BindingSource
Dim ds As New DataSet
' Make the Connection
Using con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = Database1.mdb")
con.Open()
Dim Sql = "SELECT COUNT ([Eventname]) FROM Eventproposal"
Dim da = New OleDb.OleDbDataAdapter(Sql, con)
da.Fill(ds, "Eventproposal")
' Set the Binding Source
bs.DataSource = ds.Tables("Eventproposal")
con.Close()
End Using
TextBox1.DataBindings.Add("Text", bs, "")

Couple things, you should end all SQL Commands to MS Access with ;
Dim Sql = "SELECT COUNT ([Eventname]) FROM Eventproposal;"
And you did not name your column which will give you an error when you attempt to access it by name.
Dim Sql = "SELECT COUNT ([Eventname]) AS Eventname FROM Eventproposal;"
I believe it will give it a name but not what your thinking. Lastly, when you do your binding, you will have to reference the name of the field in the table.
TextBox1.DataBindings.Add("Text", bs, "Eventname")

Related

Data type mismatch in criteria expression vb.net

I am using VB.Net connecting to MS Access database. I think SQL syntax is wrong because before i add WHERE clause, it works. When i add WHERE clause, i got error ' data type mismatch in criteria expression'. Any help would be appreciated, thank you.
Here is my code
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where [OperationID] ='" & TextBoxOpID4.Text & "'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
Additional info: textboxOpID4 is enabled false
make something like this
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where [OperationID] =" & TextBoxOpID4.Text , myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
I quite use this kind of query when the where clause is a number because if you do put ' in a number column the output would be string which produces the error for example the ColumnID = 5 your code is like this ColumnID = '5' which is wrong especially if it is an AutoNumber field so do not insert a datatype number inside ' so it will not become a string

VB.NET SQLClient Table Names are ambiguous

Here is some simple sample code that demonstrates how I am getting results back from SQL Server:
Dim dtbTable As System.Data.DataTable
Dim sdaDataAdapter As New System.Data.SqlClient.SqlDataAdapter()
sdaDataAdapter = New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM A; SELECT * FROM B", Connection)
dtbTable = New System.Data.DataTable()
dtbTable.Locale = System.Globalization.CultureInfo.InvariantCulture
sdaDataAdapter.Fill(dtbTable)
sdaDataAdapter = Nothing
objCommandBuilder = Nothing
In .Net, table A will come out TABLE and Table B will come out TABLE1... I want them to hold their real table names.
How do I get the real table names of the tables that come in from the SQL Query ? All the column names are correct, but the tables come out TABLE, TABLE1, TABLE2... I obviously know how to set them in my code or parse the table names and set them in code... I am wondering why .Net looses the table names in the first place and if there is a way for them to not be turned into ambiguous names.
Why would the writers of the SQLClient drivers not think we care about table names?
Any ideas from anyone on a way to get around this without having to set Table Name manually?
Here are two approaches - map the tables to DataTables and load the DataSet one table at a time w/desired name.
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Dim Connection As New SqlConnection(OISConnectString)
Connection.Open()
Dim da As New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM FIS..FIS_Log; SELECT * FROM FIS..FIS_Log_CA", Connection)
Dim ds As New DataSet
da.Fill(ds)
Debug.Print(ds.Tables(0).TableName) ' Table
Debug.Print(ds.Tables(1).TableName) ' Table1
Dim dtLog As DataTable = ds.Tables(0)
Dim dtLog_CA As DataTable = ds.Tables(1)
' work with DataTables by name
Stop
'reset
ds = Nothing
da = Nothing
ds = New DataSet()
da = New SqlDataAdapter("SELECT * FROM FIS..FIS_Log", Connection)
da.Fill(ds, "FIS_Log")
da = New SqlDataAdapter("SELECT * FROM FIS..FIS_Log_CA", Connection)
da.Fill(ds, "FIS_Log_CA")
Debug.Print(ds.Tables(0).TableName) ' Table
Debug.Print(ds.Tables(1).TableName) ' Table1
dtLog = ds.Tables("FIS_Log")
dtLog_CA = ds.Tables("FIS_Log")
Connection.Close()
End Sub
You might find TableAdapterManager useful depending on your platforms

Update Access database records by column, row known

This is what I've got so far :
Dim myCONN As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\Baza.mdb")
Dim cmd1 = New OleDbCommand("SELECT ID FROM Baza WHERE NAZIV=#XXNAZIV")
cmd1.Parameters.AddWithValue("#XXNAZIV", TextBox2.Text)
cmd1.Connection = myCONN
myCONN.Open()
Dim result = cmd1.ExecuteReader()
While (result.Read())
Dim rowx As Integer = GetTextOrEmpty(result("ID"))
End While
I've found the row (rowx) in which I would like to change values in 20 corresponding columns (namesID : NAZIV, SIFRA,...). Data is already presented in textboxes (textbox1...), but I don't know how to finish this code with UPDATE and how to insert changed values back to Access.
Dim cmdText As String = "UPDATE Baza SET NAZIV=#XXNAZIV Where ID=SomeId"
Using con = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = h:\Baza.mdb")
Using cmd = new OleDbCommand(cmdText, con)
con.Open()
cmd.Parameters.AddWithValue("#XXNAZIV",TextBox2.Text)
cmd.ExecuteNonQuery()
End Using
End Using
This should help you to solve your problem, of course you will have to pass ID parameter to query also.
Reference

Saving record with dataset

I want to save a record in the database using a dataset, but my data is not committing into my database.
My code can be viewed below:
Dim mydataset1 As New MyDataSet
Dim row As DataRow = mydataset1.Tables("testtable").NewRow()
With row
.Item("name") = "Segun Omotayo"
.Item("address") = "Abuja"
End With
mydataset1.Tables("testtable").Rows.Add(row)
Any help will be appreciated
A DataSet/DataTable is a offline/in-memory representation of your database. If you want to update the database, you need to use a DataAdapter.
For example (assuming you're using MS-Sql-Server):
Public Function UpdateDataSet(dataSet As DataSet) As Int32
Using con = New SqlConnection(My.Settings.SqlConnection)
Dim sql = "INSERT INTO TUser(Name,Address)VALUES(#Name,#Address)"
Using cmd = New SqlCommand(sql, con)
cmd.Parameters.Add(New SqlParameter("#Name", SqlDbType.VarChar))
cmd.Parameters.Add(New SqlParameter("#Address", SqlDbType.VarChar))
Using da = New SqlDataAdapter()
da.InsertCommand = cmd
con.Open()
Dim rowCount = da.Update(dataSet)
Return rowCount
End Using
End Using
End Using
End Function
I could be rusty here since its a long time since I wrote any VB.NET or used data adapters/datasets/datatables but I think if you decide to take that route you would need code like this:
Dim connection As New SqlConnection("#####YourConnectionString#####")
connection.Open()
Dim adapter As New SqlDataAdapter("SELECT * FROM testtable", connection)
' For the line below to work, you must have a primary key field in "testtable"
Dim builder As New SqlCommandBuilder(adapter)
Dim testtable As New DataTable("testtable")
adapter.Fill(testtable)
Dim row As DataRow = testtable.NewRow()
With row
.Item("name") = "Segun Omotayo"
.Item("address") = "Abuja"
End With
testtable.Rows.Add(row)
adapter.Update(testtable)
connection.Close()

How to determine the id of a data in vb.net?

I'm currently working on an ms access database in vb.net.
And I need a code that can determine which data I am updating in the database. Because when I try to update the data, the previous data is being cloned and it will generate two data(the updated and the previous data), and the program will also generate a random id number for the updated data, which is not good.
Here is my code for the update button:
'update
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("GH").Rows(INC).Item(1) = TextBox13.Text
ds.Tables("GH").Rows(INC).Item(2) = TextBox14.Text
ds.Tables("GH").Rows(INC).Item(3) = TextBox15.Text
da.Update(ds, "GH")
MsgBox("Data updated")
My code for form load:
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\ACCESS DATABASE\search.mdb"
con.Open()
sql = "SELECT * FROM GH"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "GH")
con.Close()
I'm using system.data.oledb namespace
Here are my declarations:
Dim cmd As OleDbCommand
Dim cn As OleDbConnection
Dim dr As OleDbDataReader
Dim ds As New DataSet
Dim con As New OleDb.OleDbConnection
Dim da As OleDb.OleDbDataAdapter
Your question is not clear to me at all, but if you want to find the last ID of an insert, you would usually check the results of "SELECT ##IDENTITY" immediately after the record is inserted.