Answer on Question#38300- Programming, C#
1. The operations-related data of FlyHigh Airlines is stored in a central database. The software fails to respond to user inputs, if there is a connectivity problem with the database. Add the code snippet that the development team should use to ensure that the application shows a user-friendly message, if such a situation arises in future.
Solution.
This code must be placed in a function that will call the connection test:
string Security = "";
if (comboBox1.Text == "")
{
comboBox1.Text = "master";
}
if (radioButton2.Checked == true)
{
Security = @"Persist Security Info=True;User ID=" + textBox5.Text + ";Password=" + textBox4.Text + "";
}
else if (radioButton1.Checked == true)
{
Security = "Integrated Security=true";
}
SqlConnection sqlConn = new SqlConnection(@"Server=" + comboBox2.Text + ";Initial Catalog=" + comboBox1.Text + ";" + Security);
try
{
string _databasename = comboBox1.Text;
sqlConn.Open();
MessageBox.Show("Checking the connection. Connecting to database ", MessageBoxButtons.OK, MessageBoxIcon.Information);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlConn;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "sp_helpdb";
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
comboBox1.Items.Add(row["name"].ToString());
}
sqlConn.Close();
comboBox1.Text = _databasename;
button3.Enabled = true;
}
catch
{
MessageBox.Show("Failed to check the connection. \n \n SQL server is unavailable. \n Log in data may be incorrect. Connecting to a database ", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Comments