Example #1
0
 public static bool IsEntityConnectionValid(BrightVisionServers server)
 {
     try {
         EntityConnectionStringBuilder entityBuilder = null;
         if (server == BrightVisionServers.Hamachi) {
             entityBuilder = new EntityConnectionStringBuilder(Hamachi.Replace(""", "'"));
             sProviderConnection = entityBuilder.ProviderConnectionString;
         }
         else if (server == BrightVisionServers.Gothenburg) {
             entityBuilder = new EntityConnectionStringBuilder(GothenBurg.Replace(""", "'"));
             sProviderConnection = entityBuilder.ProviderConnectionString;
         }
         else if (server == BrightVisionServers.DemoEnv) {
             entityBuilder = new EntityConnectionStringBuilder(DemoEnv.Replace(""", "'"));
             sProviderConnection = entityBuilder.ProviderConnectionString;
         }
         using (var conn = new SqlConnection(sProviderConnection)) {
             if (conn.CanOpen())
                 return true;
             else {
                 System.Windows.Forms.MessageBox.Show(
                     "Could not establish connection to server. Please make sure you are connected to local network or use Hamachi client to connect and try again or contact network administrator.",
                     "Network Connection Error",
                         System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                 return false;
             }
         }
     } catch {
         System.Windows.Forms.MessageBox.Show(
                     "Could not establish connection to server. Please make sure you are connected to local network or use Hamachi client to connect and try again or contact network administrator.",
                     "Network Connection Error",
                         System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
         return false;
     }
 }
Example #2
0
        /// <Check Database Connection and operator>a
       /// It consume the Can open function and get the operator ID after validating the credentials.
       /// </Check Database Connection and Operator>
       /// <param name="operatorName"></param>
       /// <returns>integer: operator id</returns>
        public int OperatorID(string operatorName)
      {
          try
          {
              object a;
              string strSecurity = "";

              // Added and changed by usman to make SQL logins work
              if (txtdbserver.Text == "") { return -1; }
              if (txtPassword.Text == "") { strSecurity = ";Integrated Security=True"; }
              string strConn = "Data Source=" + txtdbserver.Text + ";Initial Catalog=" + txtdbName.Text + "; User ID=" + txtUserName.Text + ";Password=" + txtPassword.Text + strSecurity + ";";
              //SqlConnection conn = new SqlConnection(strConn);//old statement before using
              using (var connection = new SqlConnection(strConn)) {
                  if (connection.CanOpen())
                  {
                      connection.Open();
                    // SqlCommand cmd = new SqlCommand("SELECT operator_ID FROM ENTITY.dbo.Operator WHERE [email protected]", connection);
                      SqlCommand cmd = new SqlCommand("SELECT operator_ID FROM tbl_User WHERE [email protected]", connection);
                      cmd.CommandType = CommandType.Text;
                      cmd.CommandTimeout = 5;   //NILS: Needs useful timeout in case doesn't work
                      cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = txtOperatorName.Text;
                      a = cmd.ExecuteScalar();
                      connection.Close();
                      if (string.IsNullOrEmpty(Convert.ToString(a)))
                      {
                          MessageBox.Show("Username doesn't match any operator in the database!", "Cannot proceed", MessageBoxButtons.OK);
                          bool c = false;
                          int b = 0;
                          b = ErrorLog((System.Windows.Forms.Application.StartupPath.ToString() + "/Logs/ErrorLog"), "Username doesn't match any operator in the database!" + "Cannot proceed");
                          switch (b)
                          {
                              case 1:
                                  break;
                              default:
                                  break;
                          }
                          return -1;
                      }// end if
                      else { return Convert.ToInt32(a); }
                  }//end if:CanOpen()
                  else { MessageBox.Show("connection cannot be open is called"); return -1; }                
                }// end using
          }//end try
          catch (Exception ex)
          {
              bool a = false;
              int b = 0;

              b = ErrorLog((System.Windows.Forms.Application.StartupPath.ToString() + "/Logs/ErrorLog"), lblfname.Text + "\t" + ex.Message + "\n" + "Please correct the connection credential!");
              MessageBox.Show("Please correct/provide the database connection credentials!");
              switch (b)
              {
                  case 1:

                      break;
                  default:
                      break;

              }
              return -1;
          }// end catch

      }