Click here to Skip to main content
14,668,708 members
Rate this:
Please Sign up or sign in to vote.
I have PostgreSQL 9.5 and Visual studio 2010;
I'm trying to connect to database through C# application; below is the code

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

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

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

and stacktrace is
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()
Updated 2-Feb-16 0:27am
StM0n 2-Feb-16 6:31am
Did you wrap the connection like shown in the example on the mainpage of
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 if possible.
pravinchopade 2-Feb-16 7:18am
I just tried to install "Npgsql 3.0.5" through "Package Manager Console";
with PM>Install-Package Npgsql -Version 3.0.5
but it says

Install-Package : Could not install package 'Npgsql 3.0.5'. You are trying to install this package into a project that targets
'.NETFramework,Version=v4.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
At line:1 char:16
+ Install-Package <<<< Npgsql
+ CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
StM0n 2-Feb-16 7:21am
Mhm... you need an older package, which provides .Net 3.5
Get a look on and scroll to the version history
pravinchopade 2-Feb-16 7:42am
I installed npgsql 2.2.7, above 3 are not installing.
StM0n 2-Feb-16 7:45am
Now I'm curious :)
pravinchopade 2-Feb-16 7:49am
me too. Now how should I have to use it in my code?
StM0n 2-Feb-16 7:55am
Like you did... maybe you could make a prototype to boil it down. Pretty much like the afore mentioned example on
pravinchopade 2-Feb-16 8:24am
OK. Now its working. Thanks for help.
I had changed my project to .Net framework 3.5, added references of npgsql.dll and Mono.Security.dll from downloaded packages.
and now its working.
StM0n 2-Feb-16 8:38am
Glad to hear it... should I post this as solution?
pravinchopade 2-Feb-16 8:44am
Yes. U can. and thanks again.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100