WebService cant access Postgre database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sakalicek
    New Member
    • Mar 2007
    • 51

    WebService cant access Postgre database

    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
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    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?

    Comment

    • Sakalicek
      New Member
      • Mar 2007
      • 51

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

      Socket socket = new Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);

      IAsyncResult result = socket.BeginCon nect(new IPEndPoint(Reso lveIPHost(conte xt.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.

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        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?

        Comment

        • Sakalicek
          New Member
          • Mar 2007
          • 51

          #5
          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.BeginCon nect() 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.

          Comment

          • Sakalicek
            New Member
            • Mar 2007
            • 51

            #6
            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.

            Comment

            • kenobewan
              Recognized Expert Specialist
              • Dec 2006
              • 4871

              #7
              Maybe an example will help:
              ASP.NET Web Service

              Comment

              • Sakalicek
                New Member
                • Mar 2007
                • 51

                #8
                Sorry but this wouldnt help me.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  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?

                  Comment

                  • Sakalicek
                    New Member
                    • Mar 2007
                    • 51

                    #10
                    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 NpgsqlConnectio n(_sConnectionS tring);
                    _conDatabase.Op en();
                    }
                    }
                    Then there is just code from Npgsql.dll and on the end there is

                    Socket socket = new Socket(AddressF amily.InterNetw ork,SocketType. Stream,Protocol Type.Tcp);

                    IAsyncResult result = socket.BeginCon nect(new IPEndPoint(Reso lveIPHost(conte xt.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.

                    Comment

                    • Sakalicek
                      New Member
                      • Mar 2007
                      • 51

                      #11
                      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.

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        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
                        Code:
                        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.

                        Comment

                        • Sakalicek
                          New Member
                          • Mar 2007
                          • 51

                          #13
                          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.

                          Comment

                          • Sakalicek
                            New Member
                            • Mar 2007
                            • 51

                            #14
                            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.

                            Comment

                            • navneetkaur
                              New Member
                              • Sep 2007
                              • 45

                              #15
                              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={Postgr eSQL};Server=Ip Address;Port=54 32;Database=dat abase 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...tr ue is i am not able to find that dll from internet...

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


                              i am very thankful to u in advance.

                              Comment

                              Working...