2010-12-03 15 views
0

私はfunction.Iをコールしているclickイベントにbutttonを持っています。そのボタンにacceptボタンのプロパティを設定しました。しかし、クリック。イベントを発生させるためにwinformボタンを2回クリックする必要があります

private void btnConnect_Click(object sender, EventArgs e) 
{ 
     try 
     { 
      //Function call for validating the textboxes entry 
      ValidateForm(); 

      if(lblMessage.Text != string.Empty) 
      { 
       //Function call for binding the dropdown with all DB names 
       BindDBDropDown(); 

       //Function call for binding the operation names in dropdown 
       SetOperationDropDown(); 
      } 
      else 
      { 
       //Else give the error message to user 
       lblMessage.Text = "Invalid Credentials"; 
      } 
     } 
     catch(Exception ex) 
     { 
      //All the exceptions are handled and written in the EventLog. 
      EventLog log = new EventLog("Application"); 
      log.Source = "MFDBAnalyser"; 
      log.WriteEntry(ex.Message); 
     } 
    } 


public void BindDBDropDown() 
{ 

     SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder(); 
     objConnectionString.DataSource = txtHost.Text; 
     objConnectionString.UserID = txtUsername.Text; 
     objConnectionString.Password = txtPassword.Text; 

     SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString); 

     //If connected then give this message to user 
     lblMessage.Visible = true; 
     lblMessage.Text = "You are connected to the SQL Server...."; 

     try 
     { 
      //To Open the connection. 
      sConnection.Open(); 

      //Query to select the list of databases. 
      string selectDatabaseNames = @"SELECT 
               NAME 
              FROM 
               [MASTER]..[SYSDATABASES]"; 

      //Create the command object 
      SqlCommand sCommand = new SqlCommand(selectDatabaseNames, sConnection); 

      //Create the data set 
      DataSet sDataset = new DataSet("master..sysdatabases"); 

      //Create the dataadapter object 
      SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectDatabaseNames, sConnection); 
      sDataAdapter.TableMappings.Add("Table", "master..sysdatabases"); 

      //Fill the dataset 
      sDataAdapter.Fill(sDataset); 

      //Bind the database names in combobox 
      DataViewManager dsv = sDataset.DefaultViewManager; 

      //Provides the master mapping between the sourcr table and system.data.datatable 
      cmbDatabases.DataSource = sDataset.Tables["master..sysdatabases"]; 
      cmbDatabases.DisplayMember = "NAME"; 
      cmbDatabases.ValueMember = ("NAME"); 
     } 
     catch(Exception ex) 
     { 
      //All the exceptions are handled and written in the EventLog. 
      EventLog logException = new EventLog("Application"); 
      logException.Source = "MFDBAnalyser"; 
      logException.WriteEntry(ex.Message); 
      MessageBox.Show ("Login Failed!!", "Error Occured"); 
     } 
     finally 
     { 
      //If connection is not closed then close the connection 
      if(sConnection.State != ConnectionState.Closed) 
      { 
       sConnection.Close(); 
      } 
     } 
    } 

誰でも私を助けることができますか?

+1

ここでイベントハンドラを登録していますか?あなたはそのコードを投稿できますか? – Oded

+0

「ダブルクリック」は、ダブルクリックの2倍、または2回の別々のシングルクリックと同じですか? –

+0

btnConnect_Clickメソッドの最初の行にMessageBox.Show()を追加してみます。何が起こるか教えてください。 –

答えて

2

最初のクリックで、lblMessageには文字列が含まれていますか?

初めて実行した場合は、条件付きテストに失敗し、文字列「無効な資格情報」がラベルに挿入されるためです。次に、2回目に実行すると、条件付きテストに合格し、期待通りにBindDBDropDownメソッドを呼び出します。

具体的には、あなたのコードのこのセクション:

if(lblMessage.Text != string.Empty) 
{ 
    //Function call for binding the dropdown with all DB names 
    BindDBDropDown(); 

    //Function call for binding the operation names in dropdown 
    SetOperationDropDown(); 
} 
else 
{ 
    //Else give the error message to user 
    lblMessage.Text = "Invalid Credentials"; 
} 

は、私はあなたがいずれかのユーザーが自分の資格情報を入力したテキストボックスの内容が空でないことを確認しようとしていること、またはあなたがしたいことを想定エラーメッセージが現在lblMessageに表示されていないことを確認してください。あなたのコードがあなたの意図を正確に反映していることを確認してください!

関連する問題