Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have PostgreSQL 9.5 and Visual studio 2010;
I'm trying to connect to database through C# application; below is the code

C#
string connstring = "Server=127.0.0.1;Port=5432;User Id=[UserID];Password=[UserPassword];Database=[Database];";
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();


I have referenced Npgsql and Mono.Security in my code.

Below is the error message I received:
C#
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


and stacktrace is
C#
at Npgsql.NpgsqlEventLog.LogMsg(ResourceManager resman, String ResourceString, LogLevel msglevel, Object[] Parameters)
at Npgsql.NpgsqlClosedState.Open(NpgsqlConnector context, Int32 timeout)
at Npgsql.NpgsqlConnector.Open()
at Npgsql.NpgsqlConnectorPool.GetPooledConnector(NpgsqlConnection Connection)
at Npgsql.NpgsqlConnectorPool.RequestPooledConnectorInternal(NpgsqlConnection Connection)
at Npgsql.NpgsqlConnectorPool.RequestConnector(NpgsqlConnection Connection)
at Npgsql.NpgsqlConnection.Open()
at PostGreSQLTest.Form1.button1_Click(Object sender, EventArgs e) in E:\TRIALS GD\PostGreSQLTest\PostGreSQLTest\Form1.cs:line 35
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at PostGreSQLTest.Program.Main() in E:\TRIALS GD\PostGreSQLTest\PostGreSQLTest\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Posted
Updated 2-Feb-16 0:27am
v3
Comments
StM0n 2-Feb-16 6:31am    
Did you wrap the connection like shown in the example on the mainpage of http://www.npgsql.org/doc/
pravinchopade 2-Feb-16 6:56am    
I tried this also, but same results.
StM0n 2-Feb-16 7:07am    
Leaves me puzzled... did you use a nuget-package?
pravinchopade 2-Feb-16 7:12am    
No. I just downloaded Npgsql.dll and Mono.Security.dll and referenced both in my project.
StM0n 2-Feb-16 7:18am    
My guess would be that there's something missing. Maybe you should do it by a nuget-package as mentioned on http://www.npgsql.org/install.html if possible.

1 solution

Like described in our conversation ;)

Download the approprioate version via nuget and reference it. in your case, it's 2.2.7 which provides a version for .net3.5

Glad I could help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900