WebService cant access Postgre database  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | |
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
|  | Moderator | | Join Date: Dec 2006
Posts: 4,745
| | | 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?
|  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | | 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.
|  | Moderator | | Join Date: Dec 2006
Posts: 4,745
| | | 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?
|  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | | 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.
|  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | | 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.
|  | Moderator | | Join Date: Dec 2006
Posts: 4,745
| | | re: WebService cant access Postgre database
Maybe an example will help: ASP.NET Web Service |  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | | re: WebService cant access Postgre database
Sorry but this wouldnt help me.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,745
| | | 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?
|  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | | 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.
|  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | | 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.
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,745
| | | 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 -
private bool Open()
-
{
-
try{
-
_conDatabase.Open();
-
return true;
-
}
-
catch(Exception) {
-
try {
-
_conDatabase = new NpgsqlConnection(_sConnectionString);
-
_conDatabase.Open();
-
return true;
-
}
-
catch(Exception) {
-
// Fatal error
-
return false;
-
}
-
}
-
}
-
This method will also return false on faliure so your app can handle it rather than having your program crash cause of it.
|  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | | 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.
|  | Member | | Join Date: Mar 2007 Location: Prague, Czech Republic
Posts: 51
| | | 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.
|  | Member | | Join Date: Sep 2007 Location: gurgaon
Posts: 45
| | | 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.
|  | Similar IIS / Microsoft Internet Information Services bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|