message box for the application

Asked By srikanth panneer selvam
08-Nov-08 12:45 AM
Earn up to 0 extra points for answering this tough question.

this is my connection string for asp.net with c# in windows application

 

_ConnectionString = "server=(local);Database=testdb ;UID=sa;password=sa1985;";

_Connection = new SqlConnection(_ConnectionString);

_Command = new SqlCommand();

_Command.CommandType = CommandType.StoredProcedure;

_Command.Connection = this._Connection;

 

i want to know how to display the message box whether database is conected with application or not???

can anyone help me for this....

  Re :: Check SQL Connection

Shailendrasinh Parmar replied to srikanth panneer selvam
08-Nov-08 12:59 AM

Hi

You have written the code, for conneting SQL server ...

To test if a SQL Server is ready to process your SqlCommands is to OPEN a connection.

If that fails, it will Throw an exception. Then you know you can't go further.

Try the following code

try
{
 _ConnectionString = "server=(local);Database=testdb ;UID=sa;password=sa1985;";

 _Connection = new SqlConnection(_ConnectionString);

 _Command = new SqlCommand();

 _Command.CommandType = CommandType.StoredProcedure;

 _Command.Connection = this._Connection;

 _Connection.Open(); //Checks here.

}
catch (Exception Ex)
{
 // Throw Exeption or View a Messagebox alert here
}

Hope it helps

  Re :: Check SQL Connection

Shailendrasinh Parmar replied to srikanth panneer selvam
08-Nov-08 01:01 AM

Please check the following url for other coding options regarding connection check.

http://www.eggheadcafe.com/community/aspnet/2/10009861/how-to-check-the-database.aspx

Hope it helps.

  message box for the application

Perry replied to srikanth panneer selvam
08-Nov-08 01:06 AM

If you are using ADO then you can check whether connection fails or success like below:

ADOcon.Open("","","",0);

if (ADOcon.State == 1) {
MessageBox.Show("Connection Opened");
ADOcon.Close();
}
else {
MessageBox.Show("Connection Failed");
}

see http://support.microsoft.com/default.aspx?scid=kb;EN-US;310083

  read this
C_A P replied to srikanth panneer selvam
08-Nov-08 02:19 AM
if you are using SQLConnection/OleDbConnection object to connect your database, check the State property of it, it will tell you whether DB Connection is Closed or Open and also other status too like "Connetion is Broken", "Connecting", "executing", "fetching", try to look for ConnectionState enumeration in MSDN for more details

if(con.State == con.Open)
{
MessageBox.Show("Connection is open");
}
else
{
MessageBox.Show("Connection is close");
}
  try this similar code
C_A P replied to srikanth panneer selvam
08-Nov-08 02:20 AM
// This example needs the 
// System.Data.SqlClient library
 
#region Building the connection string
 
string Server = "localhost";
string Username = "my_username";
string Password = "my_password";
string Database = "my_database";
 
string ConnectionString = "Data Source=" + Server + ";";
ConnectionString += "User ID=" + Username + ";";
ConnectionString += "Password=" + Password + ";";
ConnectionString += "Initial Catalog=" + Database;
 
#endregion
 
 
#region Try to establish a connection to the database
 
SqlConnection SQLConnection = new SqlConnection();
 
try
{
SQLConnection.ConnectionString = ConnectionString;
SQLConnection.Open();
 
// You can get the server version
// SQLConnection.ServerVersion
}
catch (Exception Ex)
{
// Try to close the connection
if (SQLConnection != null)
SQLConnection.Dispose();
 
// Create a (useful) error message
string ErrorMessage = "A error occurred while trying to connect to the server.";
ErrorMessage += Environment.NewLine;
ErrorMessage += Environment.NewLine;
ErrorMessage += Ex.Message;
 
// Show error message (this = the parent Form object)
MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 
// Stop here
return;
}
 
#endregion
 
 
#region Execute a SQL query
 
string SQLStatement = "SELECT * FROM ExampleTable";
 
// Create a SqlDataAdapter to get the results as DataTable
SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(SQLStatement, SQLConnection);
 
// Create a new DataTable
DataTable dtResult = new DataTable();
 
// Fill the DataTable with the result of the SQL statement
SQLDataAdapter.Fill(dtResult);
 
// Loop through all entries
foreach (DataRow drRow in dtResult.Rows)
{
// Show a message box with the content of
// the "Name" column
MessageBox.Show(drRow["Name"].ToString());
}
 
// We don't need the data adapter any more
SQLDataAdapter.Dispose();
 
#endregion
 
 
#region Close the database link
 
SQLConnection.Close();
SQLConnection.Dispose();
 
#endregion

  Solution
san san replied to srikanth panneer selvam
08-Nov-08 12:47 PM
Hi
Follow the bellow code...

 SqlConnection conn = new SqlConnection("server=(local);Database=testdb ;UID=sa;password=sa1985;");
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                MessageBox.Show("Connection opened Successfully");
            }
            else
            {
                MessageBox.Show("Connected failed");
            }


  re: message box for the application
kumar replied to srikanth panneer selvam
24-Aug-10 10:21 AM
hi friends you want to display a message box

http://kumarmunendra.wordpress.com/
i think this may help you.
  re: message box for the application
kumar replied to srikanth panneer selvam
22-Oct-10 03:41 AM

if (con.State == ConnectionState.Open)

 
   messagebox.show("database connected");

 else

con.Open();
  re: message box for the application
kumar replied to srikanth panneer selvam
22-Oct-10 03:43 AM

if (con.State == ConnectionState.Open)

 
   messagebox.show("database connected");

 else

con.Open();



                           infynet.wordpress.com
Create New Account