Exception OLEDB in vb.net when connecting with MS Access - sql

When i type
"dbProvider = "provider=microsoft.ace.oledb.12.0;"
dbSource = "data source = d:/data1.accdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "select * from table 1"
da = New OleDb.OleDbDataAdapter(sql, con)
MsgBox("database is now open")
da.Fill(ds, "ohr id/phr id")
con.Close()
MsgBox("database is now closed")"
However when i type the following code with a change in the sql query. It shows OLEDB Exception occurs.
"dbProvider = "provider=microsoft.ace.oledb.12.0;"
dbSource = "data source = d:/data1.accdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "select column 1 from table 1"
da = New OleDb.OleDbDataAdapter(sql, con)
MsgBox("database is now open")
da.Fill(ds, "ohr id/phr id")
con.Close()
MsgBox("database is now closed")"
Please tell me what is going wrong?. Thanks in advance
P.S.:the code waroks fine till MsgBox("database is now open")...the problem appears only in da.Fill(ds,"...")

Related

Saving data to access not working, despite no errors

Hey I'm trying to use this code to save to my access database and although there are no errors and it says data saved, my access database doesn't receive the new data. What's wrong with it?
Private Sub savenew()
Dim conn As New OleDbConnection
conn = New OleDbConnection
dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbsource = "Data Source = FULL YUGIOH ACCESS DATABASE.accdb;"
conn.ConnectionString = dbprovider & dbsource
Dim reader As OleDbDataReader
Dim command As OleDbCommand
Try
conn.Open()
Dim query As String
query = "insert into item(name) values ('" & TextBox4.Text & "')"
command = New OleDbCommand(query, conn)
reader = command.ExecuteReader
MsgBox("data saved")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
(table in my database is called item, and column is called name.)

need help connecting ms access to vb.net

I need help my program don't the use connection string that i want instead it find the access file on this folder and display error
Could not find file 'C:\Users\user\Documents\Visual Studio 2008\Projects\Patientt\Patientt\bin\Debug\db_hospital.accdb'.
here is my code
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\db_hospital.accdb"
con.Open()
sql = "SELECT * FROM tblPatients"
ds.Clear()
da = New OleDbDataAdapter(sql, con)
da.Fill(ds)
Me.DataGridView1.DataSource = ds.Tables(0)
con.Close()
Put your access file in the debug folder and change the connection string like this
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\db_hospital.accdb"

VB.NET SQL Statement

I am relatively new to programming in VB.
Currently, I have a login page that works fine. After successful login, my user is directed to another form which connects back into an MS Access table and presents data on the screen for them.
I take the logged in user and equal it to a label on the secondary form (after they login). Basically I want to have a SQL select statement for if the Username column in my access db equals that label, then to pull back the data for that unique user. Here is my current code:
lblUsername.Text = login.txtUsername.Text
DbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
DbSource = "Data Source = C:\loginprogram\studentclasslist.mdb"
con.ConnectionString = DbProvider & DbSource
con.Open()
strSQL = "SELECT * FROM Table1 WHERE Username = '" + Label1.Text + "'""
da = New OleDb.OleDbDataAdapter(strSQL, con)
da.Fill(ds, "studentclasslist")
con.Close()
It doesn't work. When I take the where statement out and just do "Select * From Table1" it works fine, but doesn't give me a unique logged in user, which is my goal.
Please help!
You should store your login user id in the session when user logged in successfully.
//In your login page
Session["myUser"] = txtUsername.Text
Then, you can use the user id from Session anywhere in your application.
//In your other page.
Dim Cmd As OleDbCommand = connection.CreateCommand()
Cmd.CommandText = "SELECT * FROM Table1 WHERE Username=?"
Cmd.Parameters.Add("#Username",OleDbType.Text,50).Value = Session["myUser"]
Dim da As New OleDbDataAdapter(Cmd)
Dim dt As New DataTable()
da.Fill(dt)
It din't work because of, misused double quotation.
May be Something like this
lblUsername.Text = login.txtUsername.Text
DbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
DbSource = "Data Source = C:\loginprogram\studentclasslist.mdb"
con.ConnectionString = DbProvider & DbSource
con.Open()
strSQL = "SELECT * FROM Table1 WHERE Username = '" & Label1.Text & "'"
da = New OleDb.OleDbDataAdapter(strSQL, con)
da.Fill(ds, "studentclasslist")
con.Close()
But this will leads you to SQL injection attacks. use parameterized queries to treat input as values in your SQL queries.
lblUsername.Text = login.txtUsername.Text
DbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
DbSource = "Data Source = C:\loginprogram\studentclasslist.mdb"
con.ConnectionString = DbProvider & DbSource
con.Open()
strSQL = "SELECT * FROM Table1 WHERE Username = #UserName"
da = New OleDb.OleDbDataAdapter(strSQL, con)
da.SelectCommand.Parameters.Add("#UserName",SqlDbType.nvarchar,50).Value = Label1.Text
da.Fill(ds, "studentclasslist")
con.Close()
To prevent from SQL injection as parameters as already been widely suggested. Additionally, you could use Using for you connection to dispose right away the connection after it is being used like:
Using con As New OleDbConnection(DbProvider & DbSource)
con.Open()
strSQL = "SELECT * FROM Table1 WHERE Username = #UserName"
da = New OleDb.OleDbDataAdapter(strSQL, con)
da.SelectCommand.Parameters.Add("#UserName",SqlDbType.nvarchar,50).Value = Label1.Text
da.Fill(ds, "studentclasslist")
End Using
Secondly, you can put the data on a DataTable
Dim dt as new DataTable
dt = ds.Tables(0)
And assuming that you have one record match you could check if there are records by checking Records.Count and if there is save it to a DataRow and then Textboxes.
If (dt.Rows.Count > 0) Then
Dim dr as DataRow = dt.Rows(0)
'Field names and Textbox names below are just mine
txtUserName.Text = dr.Item("Username")
txtPassword.Text = dr.Item("Password")
txtUserlevel.Text = dr.Item("UserLevel")
End If
So, all in all it would probably look like this:
lblUsername.Text = login.txtUsername.Text
DbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
DbSource = "Data Source = C:\loginprogram\studentclasslist.mdb"
Using con As New OleDbConnection(DbProvider & DbSource)
Dim dt as new DataTable
con.Open()
strSQL = "SELECT * FROM Table1 WHERE Username = #UserName"
da = New OleDb.OleDbDataAdapter(strSQL, con)
da.SelectCommand.Parameters.Add("#UserName",SqlDbType.nvarchar,50).Value = Label1.Text
da.Fill(ds, "studentclasslist")
dt = ds.Tables(0)
If (dt.Rows.Count > 0) Then
Dim dr as DataRow = dt.Rows(0)
'Field names and Textbox names below are just mine
txtUserName.Text = dr.Item("Username")
txtPassword.Text = dr.Item("Password")
txtUserlevel.Text = dr.Item("UserLevel")
End If
End Using
thanks everyone. it was the double quote. works now but next is making it sql injection proof which i will learn how to use parameterized queries to prevent against.

OLEDB Connection won't connect to Access 2013 database

I am trying to use the OLEDB connection to load my database to my Visual Basic program. However, I am receiving this error:"Could not find installable ISAM."
I am using Microsoft Access Database 2013. As far as I'm aware, 12.0 is the correct version.
This is my code:
Dim con As New OleDb.OleDbConnection
Dim databaseprovider As String
Dim dblocation As String
databaseprovider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
dblocation = "Date source = C:\HotelBookingDatabase.accdb"
con.ConnectionString = databaseprovider & dblocation
con.Open()
MsgBox("open")
con.Close()
MsgBox("closed")
Edit - I have changed "Data source..." to "Data Source..." and installed 2007 Office System drivers, but that hasn't helped my cause.
Edit #2 - Looked at the code above again today. I figured out the problem. Instead of "Data Source", my code has "DATE Source". Oops. Updated code, which works:
Dim con As New OleDb.OleDbConnection
Dim dbprovider As String
Dim dbsource As String
dbprovider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
dbsource = "Data Source = C:\HotelBookingDatabase.accdb;"
con.ConnectionString = dbprovider & dbsource
con.Open()
MsgBox("ok")
con.Close()
MsgBox("bye")
I have sorted out the problem.
I changed my code to this:
Dim con As New OleDbConnection("Provider=MICROSOFT.ACE.OLEDB.12.0; Data Source=C:\HotelBookingDatabase.accdb")
con.Open()
MsgBox("ok")
con.Close()
MsgBox("bye")

How to call a query from MS Access 2010 (.accdb) within VB.Net 2010?

I have this code that populates a datagridview with data from ms access:
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim Sql As String
Sql = "SELECT * FROM myTable WHERE case_no=?"
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.accdb;Persist Security Info=True;Jet OLEDB:Database Password=dbadmin2010"
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(Sql, con)
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Case Info")
DataGridView1.DataSource = ds.Tables("Case Info")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I had just finished creating a query from design view within MS Access itself, is there a way to call that query and retrieve the results to my datagridview?
Just use the query name and set the command type, for example, in addition to what you already have, you can use the following notes:
Try
con.ConnectionString = enter_connection_string_here
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "NameOfQuery"
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
da.Fill(ds, "Case Info")