Advertisement
Guest User

EventHandler for btn_ConnectionStringSave

a guest
Mar 26th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1.   private void btnSaveConnection_Click(object sender, EventArgs e)
  2.         {
  3.             string warnEmptyField = "Spceifier un Id Utilisateur et un mot de pass";
  4.             string serverName = string.Empty;
  5.             string db_name = string.Empty;
  6.  
  7.             if (txtServerName.Text.Trim() != string.Empty)
  8.             {
  9.                 if (cbConnectionType.SelectedIndex == 0)
  10.                 {
  11.                     serverName = @".\";
  12.                     serverName += txtServerName.Text.Trim();
  13.                 }
  14.                 else
  15.                 {
  16.                     serverName = txtServerName.Text.Trim();
  17.                 }
  18.                 connectionStringBuilder.DataSource = serverName;
  19.             }
  20.             else
  21.             {
  22.                 warnEmptyField = "Spceifier le nom du serveur";
  23.                 MessageBox.Show(warnEmptyField, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  24.             }
  25.             if (String.IsNullOrEmpty(txtBDName.Text) == false)
  26.             {
  27.                 db_name = txtBDName.Text.Trim();
  28.                 connectionStringBuilder.InitialCatalog = db_name;
  29.             }
  30.             else
  31.             {
  32.                 warnEmptyField = "Spceifier votre Base de donnée";
  33.                 MessageBox.Show(warnEmptyField, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  34.             }
  35.             if (cbIntegratedSecurity.Checked)
  36.             {
  37.                 connectionStringBuilder.IntegratedSecurity = true;
  38.             }
  39.             else
  40.             {
  41.                 if (String.IsNullOrEmpty(txtUserName.Text) == false &&
  42.                     String.IsNullOrEmpty(txtUserName.Text))
  43.                 {
  44.                     connectionStringBuilder.UserID = txtUserName.Text;
  45.                     connectionStringBuilder.Password = txtPassword.Text;
  46.                 }
  47.                 else
  48.                 {
  49.                     warnEmptyField = "Spceifier un ID Utilisateur et un mot de pass";
  50.                     MessageBox.Show(warnEmptyField, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  51.                 }
  52.             }
  53.  
  54.             try
  55.             {
  56.                 try
  57.                 {
  58.                     bool connected = TestConnectionString(connectionStringBuilder.ToString());
  59.                     if (connected)
  60.                     {
  61.                         MessageBox.Show("Connection  Etablie", "Success",
  62.                                      MessageBoxButtons.OK, MessageBoxIcon.Information);
  63.                         connectionSet = true;
  64.                         tab_BasicInfos.PageVisible = true;
  65.                         tabs.SelectedTabPage = tab_BasicInfos;
  66.                         tabConnectionConfig.PageVisible = false;
  67.                         currentConfig.ConnectionString = connectionStringBuilder.ToString();
  68.                         DataSource.InitConnection(connectionStringBuilder.ToString());
  69.                     }
  70.                     else
  71.                     {
  72.                         MessageBox.Show("Connection Non Etablie", "Erreur",
  73.                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
  74.                     }
  75.                    
  76.                 }
  77.                 catch (Exception)
  78.                 {
  79.                     MessageBox.Show("Connection Non Etablie", "Erreur",
  80.                                     MessageBoxButtons.OK, MessageBoxIcon.Error);
  81.                     DataSource.DisposeConnection();
  82.                 }
  83.             }
  84.             catch (Exception)
  85.             {
  86.                 MessageBox.Show("Erreur de connection...\nImpossible d'atteindre le serveur fournis" +
  87.                                 "Veullez verfier vos parametres de connection et ressayer", "Erreur",
  88.                                 MessageBoxButtons.OK, MessageBoxIcon.Error);
  89.                 DataSource.DisposeConnection();
  90.             }
  91.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement