views:

77

answers:

3

Hello, I have a C# Service project where I am connectiong to SQL and retrieving data. When I debug locally eg: in the Visual Studion Developement Server it works nice. But when I upload to the server(simply localhost/MyProject/) The SqlCommand() is throws an exception. Are there way to get more information on SqlCommand()? What permissions I should set to run web service on the server?

Maybe mode details: The project within VS2008 envirenoment is works nice:

http://localhost:50301/GetJpeg.aspx?ra=224.5941&dec=-1.09&width=1000&height=1000&scale=1

but on the http://localhost/GetJpeg.aspx?ra=224.5941&dec=-1.09&width=1000&height=1000&scale=1 no.

The exception is not really exception the:

SqlDataReader reader does not return any result in second case:

reader = cmdCenter.ExecuteReader();
                if (reader.Read()) 
                {
                    //Do Something

                    reader.Close(); 
                }                               
                else 
                {                   
                    throw new Exception("Request is failed");

                }

Thanks Arman.

EDIT Just for information: In one case the code is debugged via ASP.NET Developement Server and the second one is running on IIS 7.0

UPDATE

After deep digging I discovered: The connection is open and connected, usual queries is ok, but queries with stored functions are failing... can be that IIS miss configuration ?

A: 

You can attach the Debugger in VS to IIS on your box and continue debugging. To do this, go to Debug->Attach to Process and then find the W3wp.exe process that is running the Application Pool your application is running in.

Benjamin Anderson
The debugger is atached, therefore I discovered that reader.Read() does not return results in second case...
Arman
@Benjamin: In one case the code is debugged via ASP.NET Developement Server and the second one is running on ISS 7.0
Arman
You can debug attached to IIS 7, you just need to know which w3wp.exe process is running your application. Cassini, the VS debug web server, is convenient, but too often it doesn't replicate a production web service environment to properly prepare and test an application.
Benjamin Anderson
+1  A: 

If you are using the SqlCommand control (drag-dropped onto a page) instead of the SqlCommand object (code), your best bet will be to add a page-level error handler (http://msdn.microsoft.com/en-us/library/ed577840.aspx).

Most likely, the problem is that your connection string uses SSPI for authenticating to the SQL Server (integrated/domain security). That would allow you to connect via Visual Studio, but not once it is deployed. You might want to look at this article (http://msdn.microsoft.com/en-us/library/bsz5788z.aspx). The answers in that article are not ideal, but they will get you moving along. The better approaches can get pretty complicated. Read-up on those once you get your app working again.

tgolisch
@tgolisch: Thanks to putting me in to right way. I wondered why I need impersonate if I am using in web.conf <authentication mode="Windows" />
Arman
permissions problem....thanks
Arman
A: 
try
{
    using (SqlConnection connection = new SqlConnection("Your connection string here"))
    {
        using (SqlCommand command = new SqlCommand("your sql here", connection))
        {
            connection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
            }
        }
    }
}
catch (Exception exception)
{
    System.Diagnostics.Debug.WriteLine(exception);
}

Breakpoint in the catch to see the exception in your debugger.

JLWarlow
Thanks for snippet. How to check if reader is ok?
Arman
I was checking the connection status is "Open". So the user can connect to database, but the query is still empty.
Arman
You can check `reader.HasRows`, or when returning many rows use `while reader.Read()` to get each row. If there is a problem calling `.ExecuteReader` either a `SqlException` or `InvalidOperationException` will be thrown.
JLWarlow
JLWarlow: reader.HasRows is false in case if page is running on IIS7.0. If it is running on VS development server then it is true(as expected).
Arman