How do I resolve "ODBC-connection fail" - vb.net

My Program is "Searching Employee" Use VB web application in Visual Studio 2008
and I use Database in Microsoft Access
I program it in my computer it work 100%. But When I move the project to other computer for upload to server, it have error
ODBC--connection to 'SQL ServerWDT02418\SQLEXPRESS' failed.
WDT02418 is my Computer's name. But I run the project in other computer.
I use the Database connection like this
Function CountEmpData(ByVal EN As String, ByVal Area As String, ByVal Product_type As String) As Integer
Dim CounterEmpData As Integer
Try
Dim ole As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\PROJECT_WD\e-OJT_Project6\Database\Employee.mdb")
ole.Open()
Dim sqlCommand As String = "SELECT COUNT(*) AS 'Counter' FROM Employee WHERE (Emp_ID LIKE '%" + EN + "%') AND (Area LIKE '%" + Area + "%') AND (Product_Type LIKE '%" + Product_type + "%')"
Dim myCommand As New OleDbCommand(sqlCommand, ole)
Dim myReader As OleDbDataReader = myCommand.ExecuteReader()
While myReader.Read()
CounterEmpData = myReader.GetInt32(0)
End While
myReader.Close()
ole.Close()
Catch ex As Exception
End Try
Return CounterEmpData
End Function
It alert error at line
Dim myReader As OleDbDataReader = myCommand.ExecuteReader()
Someone please help me!!

There's nothing computer-specific in the code above, apart from the location of the Access file, but the absence of that wouldn't produce the error above. So I think the problem must lie in your .mdb database, and I suspect the issue is that it contains tables that are linked to a SQL Server which only code running on your machine can get to.
I would troubleshoot this by using the Microsoft Access UI to open the access database on one of the other computers, opening the Employee table and seeing if you still get an error. If you do, you'll know that it's the Access database that's at fault rather than your code. I would guess at this point that remote connections are not set up on SQL Server Express (see here to find out how to enable remote connections), or some firewall rule on your machine is preventing other machines from connecting to the instance of SQL Server Express, so I'd open Windows Firewall and allow inbound traffic on the port that SQL Server Express is listening on, or (better) move the SQL Server from your local machine to a dedicated development server.

Related

connection to Pervasive database slow

This has been an on going issue at my place of work. Previously, we had a co-op programming student write us some applications using C# which ran well on his laptop, but once the applications were put to use on different computers, the connection to the pervasive database because painfully slow.
Now I am writing a simple VB.net application which need to query the Pervasive database, and I'm running into the same issue.
here's my code:
'PSQL variables
Dim myPsqlConnection As PsqlConnection = New PsqlConnection("ServerName=FILESERVER;ServerDSN=SAGE2")
Dim queryString As String
queryString = "SELECT NAME FROM CUSTOMER"
Dim MyCommand As New PsqlCommand(queryString, myPsqlConnection)
Dim MyReader As PsqlDataReader
Dim tempCustname As String
Try 'open conncetion to Pervasive DB
myPsqlConnection.Open()
Catch ex As Exception
MsgBox("COULD NOT OPEN A CONNECTION TO THE DATABASE" & vbCrLf & ex.Message)
Exit Sub
End Try
Try 'execute Pervasive query
MyReader = MyCommand.ExecuteReader
Catch ex As Exception
MsgBox("QUERY ERROR" & vbCrLf & ex.Message)
Exit Sub
End Try
If MyReader.HasRows = False Then
MsgBox("NO RESULTS FOUND")
Exit Sub
End If
While (MyReader.Read)
tempCustname = MyReader("NAME").ToString()
Customers.Items.Add(tempCustname)
End While
MyReader.Close()
myPsqlConnection.Close()
I stepped through the program and it take about 30 seconds to get past the line where it opens the connection.
As for the specs (I'll do my best):
My computer is quite powerful (16 gb or ram, 6 core AMD processor at 3.0 Ghz per core, windows 7 home 64 bit)
Pervasive.Data.SqlClient Version 3.2
Compiling with Microsoft Visual Basic 2010 Express
There are very few computers on this network (about 7), and I have no such trouble with MS Access databases. Our server is new 2 years ago, and out network has been upgraded to a GB connection. I should note I know very little about databases in general, let alone connecting with VB. I'm not really the best person to be doing this, but I'm still the most qualified person in our small company.
I've got it working now. The solution was to first install the database in the pervasive control center. First by selecting "new database" from the main window of the PCC and filling in the database name, location, username and password.
Next I changed the connection from pervasive ADO to OLEDB. My connection string now looks like this:
Dim myPsqlConnection As OleDbConnection = New OleDbConnection("Provider=PervasiveOLEDB;Data Source=DatabaseName;Location=FILESERVER")
Thanks to all everyone for your help!

Vb.Net Detect if application server is down

I have developed a .Net application that I want to share to users through shared file in the LAN that is managed through a Domain server. When the server is up and running its fine but when the server is shut down an error comes. I have access to sql server database in that server. When server is shut down it gives a fatal error and application stops. How to detect the error of server being down?
I am connecting to the server through the following code:
Dim sql As String
Dim daCourseMaster As SqlDataAdapter
Try
sqlConn = New SqlConnection(connString)
sqlConn.Open()
sql = " ... "
daCourseMaster = New SqlDataAdapter(sql, sqlConn)
daCourseMaster.Fill(dsDataset, "table")
dvRegister = New DataView
dvRegister = dsDataset.Tables("courseRegisters").DefaultView
dgvCourseRegister.DataSource = dvRegister
Catch ex As Exception
MsgBox(Err.Description)
End Try
Now there might be different reasons why user can not login to the server: either the server name is wrong or user name or password is wrong or thebserver might be shut down! How to differentiate between these reasons to let the user know what might be the reason to failed login?
Use this code before the code that do the sharing (if the code in a Sub):
If Not My.Computer.Network.Ping(" You Server Name or Server IP") Then Exit Sub
or (if the code in a Function):
If Not My.Computer.Network.Ping(" You Server Name or Server IP") Then Exit Function
Lets say, you need to query your database. When you open connection and then when you execute your query, use Try-Catch Block and intercept SqlException. It has different codes, so you can test it for connectivity -related codes
Try
Using conn as new SqlConnection(...)
conn.Open()
....
End Using
Catch (connEx as SqlException)
' sql - related error handling
Catch (ex as Exception)
' here handle all other exception
End Try
this way, you can let user know that connection has lost and "try again later" or something like this

Setting Up SqlConnection string in vb.net for a local database created through visual studio 2010

I have been searching for a couple hours, and found several questions, but none of them really explained this in a way I can understand.
I'm programming a game similar to Rock Paper Sissors, except with many more selections, and the possiblity of a tie. I had originally hardcoded all of the possible outcomes, then decided to try a database so I can learn and practice sql as well.
Problem is, I can't figure out for the life of me how to connect to my local database, now that I have set it up and filled it through Visual Studio 2010. I can connect to it through Server Explorer just fine, and I can see it in Solution Explorer. I've tried several things, along the lines of:
Private sqlConn As New SqlConnection("Data Source=(local)|DataDirectory|\Outcomes.sdf;database=Outcomes;Integrated Security=true")
Private sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS; Integrated Security=true")
Dim sqlConn As SqlConnection
sqlConn = New SqlConnection("DataSource=..\..\Outcomes.sdf")
I am relatively new to sql, but know enough through tinkering to build a sql statement and get me the result I want. But I've never connected to a database before. I've looked on MSDN and tried several things I saw on there (everything that looked like what I needed, really) but it still hasn't worked.
If I can connect, I already have my statement set, and have tested it through the database itself. Any help would be wonderful, especially if it's explained in a way I can understand it and use it for later.
Also, if it helps and isn't noticed through my tried code, my db name is Outcomes. I don't know if that is needed or will help, but just in case.
Please visit connection strings here...
It also would have been helpful to know what type of DBMS you are using as well. I noticed you have an .sdf database file, not a DBMS (For ex: MySql, SQL or Oracle). Therefore, your connection string is really going to depend on what you want.
Your issue is here by the way...
Private sqlConn As New SqlConnection("Data Source=(local)|DataDirectory|\Outcomes.sdf;database=Outcomes;Integrated Security=true")
*You cant use the SqlConnection you have because its not supported with the use of .sdf files.
Instead you have to use: System.Data.SqlServerCe 'This is for compact edition
If you would like to know more about this please see here.
Kendra,
Here are the logical Steps you will need to follow to access the database programmatically:
Note: I'm assumming you have the proper SQLExpress | SQL Server Database setup whether local or remote the methods below are identical except for the connection string information.
1) Import the Sql AdoNet Namespace so you can use the proper SQL Server Client Objects & Methods;
a) Imports System.Data.SqlClient
2) Establish a Connection to the database with the ADO Connection Object:
' Create your ADO Connection Object:
Private myConn As SqlConnection
myConn = New SqlConnection("Initial Catalog=OutComes;" & _
"Data Source=localhost;Integrated Security=SSPI;")
Note: This connection string uses integrated security from your windows machine. you could also use standard security where you would need to enter your username and password credentials. Its your choice.
3) Setup Your ADO Command Object to Define your data retrieval query:
'Create a Command object.
Private myCmd As SqlCommand
myCmd = myConn.CreateCommand
myCmd.CommandText = "SELECT FirstName, LastName FROM Employees"
'Open the connection.
myConn.Open()
Note: Subsitute CommandText string for your actual query based upon your own database schema.
4) Read, Fetch, Display Data using the SQLDataReader Object:
Private results As String
Private myReader As SqlDataReader
myReader = myCmd.ExecuteReader()
'Traverse the DataSet and Display in GUi for Example:
Do While myReader.Read()
results = results & myReader.GetString(0) & vbTab & _
myReader.GetString(1) & vbLf
Loop
'Display results.
MsgBox(results)
5) Gracefully Close all Objects Used:
' Close the reader and the database connection.
myReader.Close()
myConn.Close()
Note - You'll need to consult microsoft for further connection string formats, since I don't have enough info. But this should clarify the actual big picture steps for you.
Regards,
Scott

Errors With SQLite and VB.net when reading database

I seem to be receiving random error messages when trying to read queries from a SQLite DB stored on a network drive. On my development machine, I rarely ever get an error, but users are reporting random errors such as:
Unable to open database. File is encrypted or is not a database
Database disk image is malformed
Or it just doesn't return any data.
My code looks like such:
Private Sub LoadStoreCalls()
Dim tmpID As String
Dim QryString As String
Dim SQLconnect As New SQLite.SQLiteConnection()
SQLconnect.ConnectionString = "Data Source=" & SpiceWorksPath & ";New=False;Compress=True;Read Only=True;"
Try
'Open a connection to the database
SQLconnect.Open()
'Get StoreCode
tmpID = Mid(StoreCode, 2) & "-" & StoreName
QryString = "SELECT id, summary, status, c_location, c_store_device FROM tickets WHERE status = 'open' AND c_location = '" & tmpID & "'"
Dim ExQry As New SQLiteCommand(QryString, SQLconnect)
ExQry.CommandType = CommandType.Text
Dim da As New SQLiteDataAdapter(ExQry)
dasSpice.Clear()
da.Fill(dasSpice, "Calls")
SQLconnect.Close()
Catch ex As Exception
If SQLconnect.State = ConnectionState.Open Then SQLconnect.Close()
MsgBox(ex.Message)
End Try
End Sub
The problem is that my application relies on this data being returned to populate additional entries of a datagridview control, and because I cannot replicate this error on my development machine using debug, I cannot find where the fault is occurring.
If the user gets one of these errors, they usually keep trying to run the query and eventually it will work. Or they just exit my application and go back in and then it seems to work for a while. The errors are random and not always from running the same query.
I'm assuming it's due to an issue talking to an SQLite DB on a shared drive, but I can't find any information regarding setting timeouts. I also can't work out how to 'catch' the error because I can't replicate it myself. I have tried adding logging details to the Catch event handler, but it simply just returns me the error message (above) in the logs.
Any help would be greatly appreciated.
Thanks
After many hours of troubleshooting and researching I found that SQLite does not play well with remote connections. Not only was it causing errors in my application, it was also throwing errors in the parent application.
My alternative was to write an application to query the database that ran locally on the SQLite machine. This fixed all of my issues.
For anyone interested...

Obtaining Databases from SQL Server 2008 using vb in VS 2012

I am fairly new to asp.net but have knowledge is vb. I am building an web application in visual studio 2012 using vb. I have a drop down list that is populated with two 2008 SQL Servers. There is another dropdown that will need to populated with a list of available databases on the selected server. I have a button that once clicked and the server is selected it will do a query and pull the list of databases
I have specified the server name in a globalvariables class and then imported it into the form I am using.
I am thinking that will need to do a IF statement that will need to say, If server1 is selected the open a connection and perform a query on the databases.
Here is my code that I have but its throwing an overload resolution failed because no accesible 'Open' accepts this number of arguments error:
Protected Sub getdb_Click(sender As Object, e As EventArgs) Handles getdb.Click
Dim objConnection As OleDbConnection
objConnection = CreateObject("OleDbConnection")
If DropDownList1.Text = globalvariables.servername1 Then
objConnection.Open("Provider=SQLOLEDB; Data Source=" & "globalvariables.servername1" & ";" & _
"Trusted_Connection=Yes; Initial Catalog=master")
End If
Please help me in getting the correct code as I have tried a number of items off of this site and nothing seems to work.
You use the New keyword to instantiate your objects
Dim serverName = DropDownList1.Text
Dim connectionString = "Provider=SQLOLEDB; Data Source=" & serverName & ";" & _
"Trusted_Connection=Yes; Initial Catalog=master"
Dim objConnection = new OleDbConnection(connectionString)
objConnection.Open()
and pass the instructions on how to open the database using a connectionString.
However, because you are using a SqlServer database why don't you use the specific classes provided for Sql Server?
SqlConnection, SqlCommand, SqlDataAdapter and so on....