Click here to Skip to main content
15,914,500 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# ASP.net Update Data into a SQL server database Pin
Wes Aday9-Mar-12 5:29
professionalWes Aday9-Mar-12 5:29 
Generali need help???in c# Pin
amira luis9-Mar-12 0:25
amira luis9-Mar-12 0:25 
GeneralRe: i need help???in c# Pin
OriginalGriff9-Mar-12 0:27
mveOriginalGriff9-Mar-12 0:27 
AnswerRe: i need help???in c# Pin
V.9-Mar-12 1:59
professionalV.9-Mar-12 1:59 
GeneralRe: i need help???in c# Pin
Wes Aday9-Mar-12 3:03
professionalWes Aday9-Mar-12 3:03 
Questioninteracting with a webiste in c# Pin
james bradbery8-Mar-12 21:21
james bradbery8-Mar-12 21:21 
AnswerRe: interacting with a webiste in c# Pin
Dave Kreskowiak9-Mar-12 3:16
mveDave Kreskowiak9-Mar-12 3:16 
QuestionThe WebService still returns "Connection Successful" even SQL Server is off. Please Help. Pin
MarkSquall8-Mar-12 18:58
MarkSquall8-Mar-12 18:58 
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:

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:

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!!!). I should be expecting the error message, right?

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

Thank you and more power. God bless everyone. Smile | :)

Warm regards,

Mark
AnswerRe: The WebService still returns "Connection Successful" even SQL Server is off. Please Help. Pin
Bernhard Hiller8-Mar-12 21:26
Bernhard Hiller8-Mar-12 21:26 
GeneralRe: The WebService still returns "Connection Successful" even SQL Server is off. Please Help. Pin
Sentenryu9-Mar-12 0:21
Sentenryu9-Mar-12 0:21 
AnswerRe: The WebService still returns "Connection Successful" even SQL Server is off. Please Help. Pin
Shameel9-Mar-12 1:03
professionalShameel9-Mar-12 1:03 
AnswerRe: The WebService still returns "Connection Successful" even SQL Server is off. Please Help. Pin
jschell9-Mar-12 5:16
jschell9-Mar-12 5:16 
AnswerRe: The WebService still returns "Connection Successful" even SQL Server is off. Please Help. Pin
MarkSquall11-Mar-12 21:33
MarkSquall11-Mar-12 21:33 
QuestionEncryption and decryption Pin
sandy_418-Mar-12 17:19
sandy_418-Mar-12 17:19 
AnswerRe: Encryption and decryption Pin
OriginalGriff8-Mar-12 20:20
mveOriginalGriff8-Mar-12 20:20 
GeneralRe: Encryption and decryption Pin
sandy_419-Mar-12 0:12
sandy_419-Mar-12 0:12 
GeneralRe: Encryption and decryption Pin
OriginalGriff9-Mar-12 0:25
mveOriginalGriff9-Mar-12 0:25 
GeneralRe: Encryption and decryption Pin
sandy_419-Mar-12 0:27
sandy_419-Mar-12 0:27 
GeneralRe: Encryption and decryption Pin
Matt Meyer9-Mar-12 3:54
Matt Meyer9-Mar-12 3:54 
GeneralRe: Encryption and decryption Pin
Wayne Gaylard9-Mar-12 0:36
professionalWayne Gaylard9-Mar-12 0:36 
GeneralRe: Encryption and decryption Pin
OriginalGriff9-Mar-12 0:40
mveOriginalGriff9-Mar-12 0:40 
QuestionWhere is this c# console application running from?? Pin
Goalie358-Mar-12 17:08
Goalie358-Mar-12 17:08 
AnswerRe: Where is this c# console application running from?? Pin
Roger Wright8-Mar-12 17:30
professionalRoger Wright8-Mar-12 17:30 
AnswerRe: Where is this c# console application running from?? Pin
RobCroll9-Mar-12 3:19
RobCroll9-Mar-12 3:19 
GeneralRe: Where is this c# console application running from?? Pin
Roger Wright9-Mar-12 5:02
professionalRoger Wright9-Mar-12 5:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.