Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear members and administrators,


Hello everyone, a pleasant day.

I've been studying C# for quite sometime and now I am doing a very simple Web application and a Web Service in which this Web Service will handle all database commands such as open, close, add a record, delete, and edit. So far, I am still stuck in opening a database.

This is the code of my Web Service, the file name is Service.asmx:
C#
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{

    private const string connectionString ="Server=MARK\\SQLEXPRESS2005;Database=SampleDatabase;Trusted_Connection=True;";
    
    string sqlQuery;
    DataSet dataSet;
    bool isConnectionFailed;
    SqlConnection sqlConnection;    
    
    public Service () {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public void connectToDatabase(){
        sqlConnection = new SqlConnection(connectionString);
        try {
            sqlConnection.Open();
            isConnectionFailed = false;
        }
        catch (Exception exception) {
            isConnectionFailed = true;
        }
        finally {  }
        
    }//end connectToDatabase()


    [WebMethod]
    public string getConnectionMessage() {
        string message="";
        if (isConnectionFailed)
            message = "Error while connecting to Microsoft SQL Server.";
        else
            message = "Connection SUCCESSFUL!!!";

        return message;
    }    
}//end class



I tested it and it works fine.


Then I created a new Solution/Project, a Web Application in C#. Afterwards, I added my WebService as a Web Reference to this Web Project(or Solution). This is the code of
Default.aspx.cs:
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebProject
{
    public partial class _Default : System.Web.UI.Page
    {
        
WebServiceDatabase.Service service = new WebProject.WebServiceDatabase.Service();//My Web Service

        protected void Page_Load(object sender, EventArgs e)
        {
            service.connectToDatabase();
            lblConnectionMessage.Text = service.getConnectionMessage();
        }
    }
}


I tested it and it works fine. The web browser(IE) displays the string:
Connection SUCCESSFUL!!!

BUT, when I tried to disconnect/stop the service of my SQL Server and try to run again the Default.aspx, I am still recieving the same message (i.e. Connection SUCCESSFUL!!!). Is not that I should be receiving Error while connecting to Microsoft SQL Server because my SQL Server has stopped?


Can anyone help me how would I change my webservice code?

Thank you and more power.

Warm regards,

Mark
Posted
Updated 8-Mar-12 18:02pm
v2

1 solution

something like this you can do.

public bool connectToDatabase(){
    sqlConnection = new SqlConnection(connectionString);
    try {
        sqlConnection.Open();
        return false;
    }
    catch (Exception exception) {
        return true;
    }
    finally {  }

}//end connectToDatabase()


[WebMethod]
public string getConnectionMessage() {
    string message="";
     bool isConnectionFailed = connectToDatabase();
    if (isConnectionFailed)
        message = "Error while connecting to Microsoft SQL Server.";
    else
        message = "Connection SUCCESSFUL!!!";

    return message;
}
 
Share this answer
 
v2
Comments
MarkSquall 9-Mar-12 0:27am    
Hello sushil.mate,

Thank you very much for your idea, but unfortunately, we got the other string (i.e. Error while connecting to Microsoft SQL Server.) even I started my SQL Server.


Warm regards,

Mark
Sushil Mate 9-Mar-12 1:24am    
Hi mark, the thing is that whenever you open the connection after your work you have to close the connection, if you don't every time the new connection has been created which is unnecessary. I can guess that you have opened many connections & now its not allowing to connect. Please go to Administrative tolls then services & restart the MSSQL$SQLEXPRESS service. & please close the connection once you done with it.. otherwise again you will face this kind of problem.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900