hi everyone,
is there any way that I can programatically tell the user weather he/she is successfully connected to the database(SQL Server 2005). or connection failed.
Code is written in C#.

string Connectionstring = "....";
sqlConnection con = new sqlConnection(Conncetionstring);
con.Open();

how I can check programatically weather con.Open() is sucess of failed.

Recommended Answers

All 2 Replies

Use try..catch..finally block.

You can use like this,

string Connectionstring = "....";
sqlConnection con = new sqlConnection(Conncetionstring);
try
        {
            con .Open();
        }
        catch (SqlException exp)
        {
//assigned into string vari
            strErrorState = exp.Message;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.