C# .NET - message box for the application

Asked By srikanth panneer selvam
08-Nov-08 12:45 AM

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  Re :: Check SQL Connection

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  Re :: Check SQL Connection

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  message box for the application

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  read this
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  try this similar code
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 = http://www.google.com/search?q=new+msdn.microsoft.com 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 = http://www.google.com/search?q=new+msdn.microsoft.com SqlDataAdapter(SQLStatement, SQLConnection);
 
// Create a new DataTable
DataTable dtResult = http://www.google.com/search?q=new+msdn.microsoft.com 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  Solution
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");
            }


  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.
  kumar replied to srikanth panneer selvam
22-Oct-10 03:41 AM

if (con.State == ConnectionState.Open)

 
   messagebox.show("database connected");

 else

con.Open();
  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
help
Transfering data from Excel to Sql Hello friends thanks for ur support. I used the code given by u, and the it withouyt using the bulkcopy method and while entering the data from Excel sheet to Sql database it should check the column ID which is primary key in sql database if the records already exists then that records will not be inserted , and if is the code for you string xConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = " + Server.MapPath( "ExcelImport.xls" ) + ";" + "Extended Properties = Excel 8.0;" ; using (OleDbConnection connection = new OleDbConnection(xConnStr)) { OleDbCommand command = new OleDbCommand( "Select * FROM [Sheet1$]" , connection); connection.Open(); / / Create DbDataReader to Data Worksheet using (DbDataReader dr = command.ExecuteReader()) { / / SQL Server Connection String string sqlConnectionString = DataAccess_Perf.GetConnectionString() ; / / Bulk Copy to SQL Server using (SqlBulkCopy bulkCopy = new
project data - ADO .Net Datasets - mydataset - mytable and run the project It is showing the error Error in file temp_993338ac-4be7-4b0c-87cc-6facb57da075{c4895001-7489-4D79-A724-7A526DA2B4F6}.rpt and if try this out Give the IIS user account rights to C: \ windows \ temp on the server, then iisreset Regards D Hello Muzaffar, Here a pointer may help you, that is from in my WindowsApplication However when I use dataset in the crystalreport form i am getting error Failed to load database information. Error in File temp_1d5040db-f113-4f15-be73-634fd72b4151 {549F9DE7-F9FF-4695-9E18-D644B06CA0EC}.rpt: Failed to is working properly I have used the following cnstring = ConfigurationManager .ConnectionStrings[ "amber" ].ConnectionString; cn = new OleDbConnection (cnstring); cmd = new OleDbCommand (); cmd.Connection = cn; da = new OleDbDataAdapter (); ds = new test.Admin. DataSet1 (); / / My Typed Dataset CrystalReport1 rpt = new CrystalReport1 (); / / My CrystalReport.rpt rpt.Close(); rpt.Dispose(); sql = "select * from PROFILEMASTER" ; cmd.CommandText sql; if (cn.State ! = ConnectionState .Closed) cn.Close(); cn.Open(); da.SelectCommand = cmd; da.Fill(ds
is better to check if the data is valid I have written to insert into SQL in the button click like below code protected void insertdata_Click(object sender, EventArgs e) { OleDbConnection oconn = new OleDbConnection (@"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + Server.MapPath("example.xls") + "; Extended Properties = Excel 8.0 OledbConnection and / / connectionstring to connect to the Excel Sheet try { / / After connecting to the Excel sheet 0); } protected void viewdata_Click(object sender, EventArgs e) / / Code to View / / the data from the SQL Server { SqlConnection conn = new SqlConnection("Data Source = . \ sqlexpress; AttachDbFileName = | DataDirectory | exceltosql.mdf;Trusted_Connection = yes"); try { SqlDataAdapter sda = new SqlDataAdapter("select * from emp", conn); DataSet ds = new DataSet(); sda.Fill(ds); GridView1
how to insert images into tables in SQL Server ? how to insert images into tables in SQL Server ? Adapt this snippet - Dim con As New SqlConnection("Server = yileiw2;uid = sqlauth;pwd = sqlauth;database = pubs") Dim da As New SqlDataAdapter("Select * From pub_info", con) Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da) Dim ds As New For more detailed info, check this MS KB article - http: / / support.microsoft.com / kb / 326502 SQL Server Script To Create Images Table And Stored Procedures if exists (select * from dbo.sysobjects where