Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I made a web service that connects to a database hosted in SQL Server 2008 R2 on my local computer, and makes some operations on it.
 
This is the connection string in the web service:
 
conn = new SqlConnection("Data Source=amir-pc\\SQLEXPRESS;User Id=sa;Password=1234; Initial Catalog=Election;Integrated Security=True");
 
It works well and successfully accesses the database and runs right.
 
Now I want to add this web service to IIS.
 
I successfully added it on Windows 7 to the IIS and can run it from the browser.
 
localhost/election_service/service.asmx
 
but when I tried to call a function that checks the connection, it failed, and I don't know why. This is the function:
 
[WebMethod]
    public string Check_conn()
    {
        try
        {
            conn.Open();
            conn.Close();
            return "ok";
        }
        catch 
        {
            return "failed";
        }
    }
Is there any modification I must to do to be able to access the database?
Posted 3-Dec-12 0:59am

1 solution

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

Solution 1

Amir
 
Have you instantiated the conn object with the above mentioned connection string?
 
Also do check if the connection is already open or not, since if the connection is already open the conn.open will throw InvalidOperationException exception resulting in your function returning false in catch statement. So ideally check this:
 
public string Check_conn()
   {
       try
       {
           if(!conn.IsOpen)
           {
             conn.Open();
             conn.Close();
           }
           return "ok";
       }
       catch
       {
           return "failed";
       }
   }
 
Regards
Pawan
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 Tadit Dash 350
2 Manas Bhardwaj 319
3 OriginalGriff 243
4 Sergey Alexandrovich Kryukov 205
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,716
2 Peter Leow 4,320
3 Maciej Los 3,540
4 Abhinav S 3,373


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid