Error when connecting to postgres using npgsql - vb.net

I'm working on test code to connect to PostgreSQL using npgsql library this is code sample:
Dim conn As Npgsql.NpgsqlConnection = New Npgsql.NpgsqlConnection("Server=localhost;Database=Fox;User Id=postgres;")
conn.Open()
Dim cmd As Npgsql.NpgsqlCommand
cmd = New Npgsql.NpgsqlCommand("select userName from users where id = 1")
'etc
when running my code an exception is raised (in line conn.open() ):
An unhandled exception of type 'System.ArgumentNullException' occurred in Npgsql.dll
Additional information: String reference not set to an instance of a String.
Where is the problem?

Related

VB.NET. How to get data from sdf database?

We are developing windows form app. We have local database which is sdf file. We added reference to our project: SQLServerCe.dll
We are using this code:
Dim strDataSource As String
strDataSource = "Data Source = C:\Users\JOE\Desktop\Class work (7)\Class work\BookIt\BookIt.DBEntity\Library.sdf"
Dim conn As New SqlCeConnection
conn.ConnectionString = strDataSource & ";Password='<password>'"
Dim selectCmd As SqlCeCommand = conn.CreateCommand
selectCmd.CommandText = "SELECT author, title FROM book"
Dim adp As New SqlCeDataAdapter(selectCmd)
Dim ds As New DataSet
' Note: Fill will leave the connection in its original state;
' In this case, the connection was closed so it will be left closed
adp.Fill(ds)
tbAuthor.Text = ds.Tables(0).Rows(0)(0).ToString()
tbTitle.Text = ds.Tables(0).Rows(0)(1).ToString()
This code is not working. This line is giving error:
adp.Fill(ds)
Error: An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
What we are doing wrong? How to solve this problem?
Open the database connection before you create your command object, ie conn.Open()

oracle connection open error

I use QTP/UFT for my application testing.
I had to access my oracle DB (11g) to do some DB setup (to update records).
I use oracle.manageddataaccess.dll (referenced to myDBWrapper class library project).
I have below dlls
(myDBWrapper is the one I created - where the issue is present)
My connection string:
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=<HOST>)(PORT=<PORT>>)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=<SERVICENAME>)));User Id=<USERNAME>;Password=<PASSWORD>;
Issue:
The below code during conn.open() throws error. It works sometimes & sometimes throws an error that 'Object reference not to set an instance of an object' in the same machine.
conn = New OracleConnection()
conn.ConnectionString = ConnectionString
MsgBox("isnothing? : " & (conn Is Nothing).ToString) 'it is always False. So , not null
conn.Open() ' It throws an error that "Object reference not to set an instance of an object"
Dim cmd As OracleCommand = New OracleCommand(strQuery)
cmd.BindByName = True
cmd.Connection = conn
RowsAffected = cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Dispose()
Note:
It is not a resource leak. I just make 1 connection and dispose. No one else is connecting to the DB.
Issue happens - When QTP has COM lib. When i use that to automate & make it invisible (QTP.Visible = FALSE). If I use QTP.Visible = TRUE - issue does not happen. What kind of issue is this!!!
StackTrace :
Object reference not set to an instance of an object.
at OracleInternal.I18N.Conv.GetMaxBytesPerChar(Int32 charsetId)
at OracleInternal.ServiceObjects.OracleConnectionImpl.Connect(ConnectionString cs, Boolean bOpenEndUserSession)
at OracleInternal.ConnectionPool.PoolManager`3.CreateNewPR(Int32 reqCount, Boolean bForPoolPopulation, ConnectionString csWithDiffOrNewPwd)
at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd)
at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword)
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword)
at Oracle.ManagedDataAccess.Client.OracleConnection.Open()
at myDBWrapper.Utility.DBUtil.OpenConnection()
Because that looks like VB.NET Have you got that code in a form load? That event is unpredictable if you are not showing the form.
have u tried the oracle connection using the ADOB connection object ? you need to have the Oracle driver installed and it will help you to connect to the DB by creation the ADOB connection object
Try this
conn = New Oracle.ManagedDataAccess.Client.OracleConnection(ConnectionString)

How to load data from oracle database using .dsn? (VB.NET)

I'm trying to test if the connection from my utility to oracle DB is working by using .dsn . I've searched from many forums but to no avail . Here is my code:
Dim filedsn As String = "C:\my_dsn.dsn"
Dim uid As String = "id123"
Dim pwd As String = "1234"
Dim cn As OdbcConnection
cn = New OdbcConnection("Driver=Oracle in OraClient11g_home2;Provider=msdaora;dsn=" & filedsn & ";uid=" & uid & ";pwd=" & pwd & ";")
Dim mystring As String = "Select * from DD_CADS1.PDTABLE_12_1001"
Dim cmd As OdbcCommand = New OdbcCommand(mystring)
cn.Open()
MsgBox("Connected")
cn.Close()
But these error appears..
ERROR [HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [HY000] [Oracle][ODBC][Ora]ORA-12560: TNS:protocol adapter error
Can you tell me what's the problem with my codes? Is there any other way to connect to oracle database through .dsn using VB.net? I'm currently using ** Oracle 11g**.
The biggest problem in your code is that you use ODBC to connect to Oracle. You should use ODP.NET, which is even recommended by Microsoft. What you doing is there [most likely] for backwards compatibility and not to write the new stuff.
Here your problem is in connection string, which should look like this
"DSN=oracledsn;UID=myUID;PWD=myPWD;Integrated Security=no;"
And you don't need command just to test connection
using cn= New OdbcConnection(".....")
cn.Open()
if cn.State = ConnState.Open Then MessageBox.Show(...)
end using
When you use using it will take care of closing and disposing of connection.

select data from Dataset Vb.Net

ok first ill explain what ive done.
First I imported an access db into my vb app it then named the dataset etc.
this is how my dataSet looks:
1 table
4 columns
so Far i have this:
Dim ds As ElementsDataSet
Dim dt As ElementsDataSet.ElementsDataTable
Dim conn As SqlConnection = New SqlConnection("Data Source=|DataDirectory|\Elements.accdb")
Dim selectString As String = "Select Atomic Mass FROM Elements WHERE No =" & mol
Dim cmd As New SqlCommand(selectString, conn)
If conn.State = ConnectionState.Closed Then conn.Open()
Dim datareader As SqlDataReader = cmd.ExecuteReader()
While datareader.Read = True
MessageBox.Show(datareader.Item("Atomic Mass"))
End While
datareader.Close()
and upon executing this I get this error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
The problem is you are using a SQLConnection to open a Access database. This wont work.
You either need a SQLServer database or to use the OleDbConnection for a Access database.
Here is a Microsoft KB article to help you connect to the Access database:
How To Retrieve and Display Records from an Access Database by Using ASP.NET, ADO.NET, and Visual Basic .NET and this one over at CodeProject: http://www.codeproject.com/Articles/8477/Using-ADO-NET-for-beginners
Private Sub ReadRecords()
Dim conn As OleDbConnection = Nothing
Dim reader As OleDbDataReader = Nothing
Try
conn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Temp\Elements.accdb"))
conn.Open()
Dim cmd As New OleDbCommand("Select Atomic Mass FROM Elements WHERE No =" & mol, conn)
reader = cmd.ExecuteReader()
While reader.Read = True
MessageBox.Show(reader.Item("Atomic Mass"))
End While
Finally
If reader <> Nothing Then
reader.Close()
End If
If conn <> Nothing Then
conn.Close()
End If
End Try
End Sub

Search button error

I have a search button to search the database by a name entered in a text box, this is working fine within visual studio but when trying to use this in the hosted version i get the following message:
Server Error in '/' Application.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
the code for this button is:
GridView2.Visible = True
lblEnterName.Text = ""
If txtLoanName.Text = "" Then
lblEnterName.Text = "Please enter a Bandie's Name"
Else
Dim conn As SqlConnection = Nothing
Try
Dim connString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BandDatabase.mdf;Integrated Security=True;User Instance=True"
conn = New SqlConnection(connString)
Dim query As String = "SELECT [SongName], [PartLearnt], [Status] FROM [Learning] WHERE ([BandieName] LIKE '%' + #Name + '%') ORDER BY [SongName]"
Dim cmd As SqlCommand = New SqlCommand(query, conn)
cmd.Parameters.AddWithValue("#Name", txtLoanName.Text)
conn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As DataTable = New DataTable()
dt.Load(dr)
GridView2.DataSource = dt
GridView2.DataBind()
lblSearchBandieName.Text = "Progress for " + txtLoanName.Text
Finally
conn.Close()
End Try
End If
I am unsure why this is bringing the error as didnt before, all i have done is changed the page the button is on.
Thanks
did you check the information in the connection string? probably not an sqlexpres on the hosted solution. Also you may want to have the connection info in the web.config
Now as an answer....: -)