Unspecified Error in OleDB.oleDbConnection
-
I get the unspecified error while connecting to the database. The Code i wrote for a class is as follows : Imports System.Data.SqlClient Public Class Conn ---------------------------------------- Dim con1 As OleDb.OleDbConnection Dim com1 As OleDb.OleDbCommand Dim da As OleDb.OleDbDataAdapter Dim ds As New DataSet Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & My.Application.Info.DirectoryPath & "\" & "MyDatabase.mdb'" ---------------------------------------- Public Function ExecuteNonQuery(ByVal SqlQuery As String) As String con1 = New OleDb.OleDbConnection(str) com1 = New OleDb.OleDbCommand(SqlQuery, con1) con1.Open() Try Return com1.ExecuteNonQuery Catch End Try End Function ----------------------------------------- I get the error as 'Unspecified error' at the line con1.open() I could not fugure out what is the problem ? could anybody help me out. Thanks in advance.
Sekhar :)
-
I get the unspecified error while connecting to the database. The Code i wrote for a class is as follows : Imports System.Data.SqlClient Public Class Conn ---------------------------------------- Dim con1 As OleDb.OleDbConnection Dim com1 As OleDb.OleDbCommand Dim da As OleDb.OleDbDataAdapter Dim ds As New DataSet Dim str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & My.Application.Info.DirectoryPath & "\" & "MyDatabase.mdb'" ---------------------------------------- Public Function ExecuteNonQuery(ByVal SqlQuery As String) As String con1 = New OleDb.OleDbConnection(str) com1 = New OleDb.OleDbCommand(SqlQuery, con1) con1.Open() Try Return com1.ExecuteNonQuery Catch End Try End Function ----------------------------------------- I get the error as 'Unspecified error' at the line con1.open() I could not fugure out what is the problem ? could anybody help me out. Thanks in advance.
Sekhar :)
If you put the con1.Open() statement in the try block, perhaps you can get a better error message. Otherwise it could be a number of things. Are you sure your database is in the correct location in your file system? Are you sure your SqlQuery is a good SQL statement? You may also want to put some breakpoints in the code and run in debug...make sure that the values being passed into the function are what you are expecting. Hope this helps.
-
If you put the con1.Open() statement in the try block, perhaps you can get a better error message. Otherwise it could be a number of things. Are you sure your database is in the correct location in your file system? Are you sure your SqlQuery is a good SQL statement? You may also want to put some breakpoints in the code and run in debug...make sure that the values being passed into the function are what you are expecting. Hope this helps.
Thanks Kschuler for your response. I have tried this function from one Form and it is working fine , but when I am calling this from another form then it is showing me the error. Can you tell what might be the problem ? I had also put the con1.open() statement inside a try catch block but that is also showing me the same error message. Please Help.
Sekhar :)
-
Thanks Kschuler for your response. I have tried this function from one Form and it is working fine , but when I am calling this from another form then it is showing me the error. Can you tell what might be the problem ? I had also put the con1.open() statement inside a try catch block but that is also showing me the same error message. Please Help.
Sekhar :)
Maybe your connection is already open. You should replace
con1.Open()
with this:If con1.State <> ConnectionState.Open Then con1.Open() End If
Also, you should put a Finally block on your try catch, and close the connection after you run an ExecuteNonQuery or an ExecuteScalar. You can use something similar to how you open it, like this:If con1.State <> ConnectionState.Closed Then con1.Close() End If
Some other things I noticed in your code...you're importing sqlclient, but you are not using it, so you probably don't need to import it (unless you are using it in code that you didn't post) Also, you declared a dataadapter object, but you are not using that either (again, unless you just didn't post it)...you also might want to look at disposing objects when you are done with them. For example, once you've used the command object at the end of you function, you could dispose it. Hope this helps.