Error: Not a valid file name ( OleDbException ) - exception

I have a simple Access database, which contains one table. Here is it
For my Button Load event I have this code
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
MsgBox("Opened")
con.Close()
And on con.Open() line I am getting this exception . And I can not understand what is the problem. Maybe the name "con" was the problem, but I changed it to "c" or "con1" but the same exception occurs. Can't understand the reason. Thanks for any solution

I don't think Provider is required here, as you already have it in the source, change...
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"
con.ConnectionString = dbProvider & dbSource
to...
dbSource = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=‪E:\addressBook\AddressBook.mdb"
con.ConnectionString = dbSource
Take a look at ConnectionStrings.
A better solution is to add the file to the App_Data folder instead of referencing the file from a local drive. Add the connection string to the config...
<connectionStrings>
<add name="AccessConnection"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|dbName"
providerName="System.Data.OleDb" />
</connectionStrings>

put the # before the connection string. it works for me!

I also faced the same problem:
but solution is:
you must give a correct path of your access database, and don't forget the access file name with its extension (your database and .accdb).
I am sure it will be all correct...

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\user\Documents\k1.accdb"
conn.ConnectionString = dbprovider
conn.Open()

Related

Add Multiple Databases in VB.NET

Is it possible to connect multiple databases to a program?
I added a database, but it was always through the "Data Source Config Wizard". My application deals with one main database, but I need to load information from other databases and add it to the main one (the user will select which database through the OpenFileDialog).
Public Class Form1
Private Sub TblTestBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TblTestBindingNavigatorSaveItem.Click
Me.Validate()
Me.TblTestBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TestdbDataSet)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TestdbDataSet.tblTest' table. You can move, or remove it, as needed.
Me.TblTestTableAdapter.Fill(Me.TestdbDataSet.tblTest)
End Sub
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
Dim DBConnection As New OleDb.OleDbConnection
'''' Dim con As New OleDb.OleDbConnection
Dim dbProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;"
'Dim dbSource As String = opnFile.ShowDialog()
opnFile.ShowDialog()
Dim ex As String = opnFile.FileName
MessageBox.Show(ex)
Dim dbSource As String = "DataSource=" & ex
MessageBox.Show(dbSource, "Data Source String", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Dim DBDataSet As New DataSet
'''' Dim ds As New DataSet
Dim DBDataAdapter As OleDb.OleDbDataAdapter
'''' Dim da As OleDb.OleDbDataAdapter
DBConnection.ConnectionString = dbProvider & dbSource
'DBDataAdapter = (sql, DBConnection)
End Sub
End Class
Also, can someone explain to me the link in DataAdapter, DataConnection, and the DataSet? From what I see, the DataConnection is just the link that will connect the application to the database. The DataSet is your actual data from the database. So what is the DataAdapter for?
Thanks for any help!
You can add several databases using the Database wizard. You will be missing the binding navigator but you can get this easily by creating a temporary form in your project add the database to this then just copy over the binding navigator before deleting the temp form.
See this thread Display data from access using dataset The use the following.
Try something like this after you load your table into a Dataset
For Each row1 In (From dr As DataRow In yourDataSet.Tables(0).Rows Select dr Where dr("Column_Name").ToString.StartsWith(yourstring)
somestring = row1("Column_Name").ToString

InvalidArgument=Value of '11209485' is not valid for 'index'. Parameter name: index error when running SQL query in VB.NET

I keep getting this error "Failure to communicate InvalidArgument=Value of '11209485' is not valid for 'index'. Parameter name: index" when I'm trying to retrieve card numbers from a database and put them in a combo box so that the user can pick their card number in VB.NET 2012. The 11209485 is the first card number in the database, so I assume the connection is fine, but I don't understand this error at all.
I'd be grateful for any help on the matter. Thanks!
Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class Form1
Dim dbCon As MySqlConnection
Dim strQuery As String = ""
Dim SQLcmd As MySqlCommand
Dim DataReader As MySqlDataReader
' load application Form
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Prepare connection and query
Try
dbCon = New MySqlConnection("Server=localhost;Database=***;Uid=***;Pwd=***")
strQuery = "SELECT CardNumber " &
"FROM Account"
SQLcmd = New MySqlCommand(strQuery, dbCon)
'Open the connection
dbCon.Open()
' create database reader to read information from database
DataReader = SQLcmd.ExecuteReader
' fill ComboBox with account numbers
While DataReader.Read
cboAccountNumbers = cboAccountNumbers.Items(DataReader("CardNumber"))
End While
'Close the connection
DataReader.Close()
dbCon.Close()
Catch ex As Exception
MsgBox("Failure to communicate" & vbCrLf & vbCrLf & ex.Message)
End Try
End Sub
End Class
The error is in this line:
cboAccountNumbers = cboAccountNumbers.Items(DataReader("CardNumber"))
You are attempting to read the 11209485th item in the combo box and there aren't that many items. Try this instead:
cboAccountNumbers.Items.Add(DataReader("CardNumber"))

Host x is not allowed to connect to this mysql server

I am trying to make a simple registration & activation system using my mysql server and vb.net and I am using the code below:
Imports MySql.Data.MySqlClient
Public Class ActivateMe
Dim MysqlConn As MySqlConnection
Dim myAdapter As New MySqlDataAdapter
Dim myData As MySqlDataReader
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MysqlConn = New MySqlConnection()
Try
MysqlConn.Open()
Dim checkUsername As String = "SELECT COUNT(*) FROM users WHERE verif=#p1 and username=#p2"
Dim insertData As String = "INSERT INTO users(hasVerif) VALUES(#p3)"
Using MysqlConn = New MySqlConnection(mysqlconntxt4reg)
Using myCommand = New MySqlCommand(checkUsername, MysqlConn)
MysqlConn.Open()
myCommand.Parameters.AddWithValue("#p1", TextBox1.Text)
myCommand.Parameters.AddWithValue("#p2", currentRegUser)
Dim result = myCommand.ExecuteScalar()
If result IsNot Nothing AndAlso Convert.ToInt32(result) > 0 Then
Using myCommand2 = New MySqlCommand(insertData, MysqlConn)
myCommand.Parameters.AddWithValue("#p3", 1)
myCommand2.ExecuteNonQuery()
MsgBox("Successfully Activated! You May Now Login!", MsgBoxStyle.Information, "Success")
Me.Close()
End Using
Else
MsgBox("Invalid Activation Code", MsgBoxStyle.Critical, "Error")
End If
End Using
End Using
MysqlConn.Close()
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & vbNewLine & vbNewLine & myerror.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
End Class
The program sends the activation code to the email successfully and uploads the activation code to the database for checking but when I am on the activation form and enter the code, it says:
Host (my pc name) is not allowed to connect to this mysql server
In the different forms, the server accepts the connection and allows access to the database so I don't see why it would be different here...
Please send me help!
Thanks
rodit
I'm not very familiar with MySQL under .NET but I believe you do something a bit odd here. First you open a connection without a connection string (I assume that uses default parameters)
MysqlConn = New MySqlConnection()
Try
MysqlConn.Open()
and then you open another connection using an explicit string
Using MysqlConn = New MySqlConnection(mysqlconntxt4reg)
Using myCommand = New MySqlCommand(checkUsername, MysqlConn)
MysqlConn.Open()
Notice it's the same MysqlConn variable (with different objects).
So the MySqlException you're catching could be coming from any one of them. You can use that exception to find out which of the open calls actually triggered the exception (I assume it's first one).
You should probably also cleanup code and keep only one open call.
Andrei

Connecting to a Online MySQL Database using VB.Net

I've searched around and haven't been able to find anything along the lines of doing this.
You need to install Connector/Net, which will give you a full ADO.Net provider for MySql you can use. Be warned that this is GPL software, meaning if you distribute it as part of a commercial product you must also distribute your source code. It's an open legal question, but last I heard most web sites are okay with this, because you are not distributing your server code. Desktop apps may have an issue, though.
Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient
Public Class LoginForm1
Dim MySQLConnection As MySqlConnection
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
Private Sub Cancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
End
End Sub
Private Sub OK_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
MySQLConnection = New MySqlConnection
MySQLConnection.ConnectionString = "server=db4free.net;Port=3306; User ID=db4freeusername; password=db4freepassword; database=nameofyourdatabase"
MySQLConnection.Open()
Dim MyAdapter As New MySqlDataAdapter
Dim SqlQuary = "SELECT * From nameofthetable WHERE Username='" & UsernameTextBox.Text & "' AND password = '" & PasswordTextBox.Text & "';"
Dim Command As New MySqlCommand
Command.Connection = MySQLConnection
Command.CommandText = SqlQuary
MyAdapter.SelectCommand = Command
Dim Mydata As MySqlDataReader
Mydata = Command.ExecuteReader
If Mydata.HasRows = 0 Then
MsgBox("Error During Login:Please Enter Valid Data")
Else
Form1.Show()
Me.Hide()
End If
End Sub
End Class
First of all you need to install MySQL connector for .NET.
Imports MySql.Data.MySqlClient
Dim myConnection As MySqlConnection = New MySqlConnection()
Dim myConnectionString As String = "Server=SERVERNAME;Database=DATABASE;Uid=root;Pwd=password;"
myConnection.ConnectionString = myConnectionString
myConnection.Open()
//execute queries, etc
myConnection.Close()
I use C#:
const String ConnectionString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Port=3306;Database=test;User=root;Password=;Option=3;";
OdbcConnection conn = new OdbcConnection(ConnectionString);
conn.Open();
OdbcCommand command = new OdbcCommand();
command.CommandType = CommandType.StoredProcedure;
command.Connection = conn;
command.CommandText = "insert into search (tempsearchKey, state, suburb) values ('" + tempsearchKey+"','"+state+"','"+suburb+"')";
command.ExecuteNonQuery();
command.Cancel();
install odbc driver from mysql website
and convert this to VB.NET,
Maybe this link could help:
http://dev.mysql.com/tech-resources/articles/ebonat-load-and-search-mysql-data-using-vbnet-2005.html
Install MySQL connector for .NET, and APACHE, also install XAMPP so you could use phpMyAdmin

SQL and VISUAL BASIC 2008 Queries

How can I write a sql query that takes information from a database, and then put in the text in a label? I'm not really sure how to do this.
MSDN has lots of examples of getting data via ADO.NET. E.g. http://msdn.microsoft.com/library/dw70f090.
You will need to adjust the connection and command types (and the connection string) to be correct for My SQL. If you have ODBC drivers for My SQL then you can follow the ODBC example with just a change of connection string.
For using MySQL with .NET I'd recommend you this tutorial, and for your problem specially part 6, about reading the data with a MySQLDataReader.
An (almost working) sample by copy&paste from there with some changes:
Private Sub getData()
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myReader As MySqlDataReader
Dim SQL As String
SQL = "SELECT LabelContent FROM myTable"
conn.ConnectionString = myConnString ' your connection string here'
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myReader = myCommand.ExecuteReader
' loop through all records'
While myReader.Read
Dim myLabelValue as String
myLabelValue = myReader.GetString(myReader.GetOrdinal("LabelContent"))
' ... do something with the value, e.g. assign to label '
End While
Catch myerror As MySqlException
MsgBox("There was an error reading from the database: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Error connecting to the database: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
For selecting one column to label from ms access 2007 database just follow this step
Create ms access database for example i make name "test.accdb" and make 1 column for example column name is "ColumnName" and one table with name "Table1"
save it on whatever folder
open vb 2008 and make one form
import adodb on first writing
write this code inside class
Sub lihat()
Dim str As String = "select * from Table1"
Dim cn As New OleDb.OleDbConnection
Dim com As New OleDb.OleDbCommand
Dim adp As OleDb.OleDbDataReader
With cn
.ConnectionString = "Provider=Microsoft.ace.oledb.12.0;data source=test.accdb;persist security info=false"
.Open()
End With
With com
.Connection = cn
.CommandText = str
End With
adp = com.ExecuteReader
adp.Read()
Label1.Text = adp(1)
cn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lihat()
End Sub
number 1 on adp(1) is number of column on Table1.