Connecting Tech Pros Worldwide Help | Site Map

WebService cant access Postgre database

Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#1: Mar 21 '07
Hi all, could you please help me with my big problem?

I have WebService on IIS. This WebService has methods to control database stored on server.
I am using Postgre database and to access to database I have Npgsql.dll library.
This library is stored in GAC (global assembly cache).

The WebService is not able to open the connection to the database even the connection string is allright, everything seems to be OK. But when the function Open() is called, there occurs an errror "Object reference not set to instance of object" or something like this.

Although, if I access the database with some console application, everything goes well.
Just the bloody WebService :(

Thank you very much for any help.
Zdenek Mach.

ICQ# 173965423
skype: Zdenek_Mach
kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#2: Mar 21 '07

re: WebService cant access Postgre database


Welcome to the site. I assume that the problem is in the web service and not IIS. So the question is, what object reference is not set?
Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#3: Mar 21 '07

re: WebService cant access Postgre database


I downloaded source code for Npgsql.dll and debug it.
The problem lies in section where sockets are made:

Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

IAsyncResult result = socket.BeginConnect(new IPEndPoint(ResolveIPHost(context.Host), context.Port), null, null);

I thought about it and it seems to me that the problem could be in setting of WebService account.
Is it right that if I debug WebService, it all runs under some other account than localhost?
I think it would be somewhere here in all of these accounts and permissions.

Am I right?Tell me please :D
Thx, Zdenek.
kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#4: Mar 23 '07

re: WebService cant access Postgre database


You receive the error when you open try to open the connection, so what information do you have that its to do with sockets? Are you able to test the connection and the sockets separately?
Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#5: Mar 27 '07

re: WebService cant access Postgre database


I have found it from source of Npgsql.dll library. I step into Open() function and further found that it ends when there is invokes a method socket.BeginConnect() or something like this.
Thus I think that if I debug my Web Service, "I have no permimssions to create sockets".

Do yoy understand what I am trying to say?
Z.
Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#6: Mar 27 '07

re: WebService cant access Postgre database


I also tried to create simple web service and console application and simple create socket and call BeginConnection on it.
Console application works well but WebService ended with same exception "Object reference not set to instance of object".
So thereby I think it would be just in debugger permissions.

Tell me, am I wrong?
Z.
kenobewan's Avatar
Moderator
 
Join Date: Dec 2006
Posts: 4,745
#7: Mar 27 '07

re: WebService cant access Postgre database


Maybe an example will help:
ASP.NET Web Service
Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#8: Mar 27 '07

re: WebService cant access Postgre database


Sorry but this wouldnt help me.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,745
#9: Mar 27 '07

re: WebService cant access Postgre database


The problem is not that you dont have permission. If that would be the case it would throw some permission exception, not a null reference exception.

As the error say's. Some object that your code is trying to access is set to null.
My guess would be the object you are calling the Open() method on.

Could you perhaps show us the code that gives you the error?
Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#10: Mar 27 '07

re: WebService cant access Postgre database


Yes, it could be.
This object may be the Npgsql.dll library which I have in GAC.
I have method to open connection to Postgre database:

private void Open()
{
// open connection
if (_conDatabase == null)
{
_conDatabase = new NpgsqlConnection(_sConnectionString);
_conDatabase.Open();
}
}
Then there is just code from Npgsql.dll and on the end there is

Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Strea m,ProtocolType.Tcp);

IAsyncResult result = socket.BeginConnect(new IPEndPoint(ResolveIPHost(context.Host), context.Port), null, null);

And then exception occurs.
Do you think that it could be in Npgsql.dll in my GAC?
Or could I add this dll to GAC in some wrong way or what?

Thx a lot for any help, Zdenek.
Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#11: Mar 28 '07

re: WebService cant access Postgre database


So what?
Do you have some idea?
The exception I got is "The attempted operation was not supported for the type of object referenced".

Zdenek.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,745
#12: Mar 28 '07

re: WebService cant access Postgre database


I think it could be your if statment.
It could be that the _conDatabase object cant be null, or that the parser throws the null exception when your checking it.

So I'd try using a try block
Somewhat like this
Expand|Select|Wrap|Line Numbers
  1. private bool Open()
  2. {
  3.   try{
  4.     _conDatabase.Open();
  5.     return true;
  6.   } 
  7.   catch(Exception) {
  8.     try {
  9.       _conDatabase = new NpgsqlConnection(_sConnectionString);
  10.       _conDatabase.Open();
  11.       return true;
  12.     } 
  13.     catch(Exception) {
  14.       // Fatal error
  15.       return false;
  16.     }
  17.   }
  18. }
  19.  
This method will also return false on faliure so your app can handle it rather than having your program crash cause of it.
Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#13: Mar 28 '07

re: WebService cant access Postgre database


I think it would not help me, but I will try it tomorrow morning.

But didnt mention that my colleague tried it on his PC and it worked well.
Connection was opened...

Thereby I think it could be in some permissions or ASPNET account permissions.
I read lot of about it but I am not able to solve this.

Quite desperate, Zdenek.
Sakalicek's Avatar
Member
 
Join Date: Mar 2007
Location: Prague, Czech Republic
Posts: 51
#14: Jul 4 '07

re: WebService cant access Postgre database


Sorry that I am answering too late.
I have found that problem was in my IIS.
It was broken or something, so I have to reinstall OS and than install IIS again.
Now its working well.
navneetkaur's Avatar
Member
 
Join Date: Sep 2007
Location: gurgaon
Posts: 45
#15: Jan 25 '08

re: WebService cant access Postgre database


I am doing connectivity to PostgreSql from asp.net 2.0 using odbc but i am not able to connect i got error Data source name not found and no default driver specified.

code is written for connectivity is like...

con = new OdbcConnection("Driver={PostgreSQL};Server=Ip Address;Port=5432;Database=database name;Uid=user id;Pwd=;");

from google i found its alternative npgsql but i dont know why,i am not able to download npgsql.dll...true is i am not able to find that dll from internet...

please help me...how i can download dll for npgsql....please provide me source i need it very much....


i am very thankful to u in advance.
Reply