2010-11-23 5 views
0

に動作していない私は、この機能が

private void btnConnect_Click(object sender, EventArgs e) 
{ 
    string localHost = "192.168.10.3"; 
    string logInDetails = "gp"; 

    //SqlConnection sConnection = new 
    //SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); 
    try 
    { 
     //Checking for the Valid entries in textboxes. 
     if ((txtPassword.Text == logInDetails) && (txtUsername.Text == logInDetails)) 
     //Checking for the appropriate local server address. 
     if (txtHost.Text == localHost) 
     { 
      BindDBDropDown(); 
      SetOperationDropDown(); 
      PrimaryKeyTable(); 
      lblError.Text = "You are connected to the SQL Server...."; 
     } 
     else 
     { 
      lblError.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); 
     } 
     //finally 
     //{ 
     // //To close the connection 
     // if (sConnection != null) 
     // { 
     //  sConnection.Close(); 
     // } 
     //} 
    } 

のようなボタンのクリックイベントに関数を呼び出す必要がありBindDBDropDown関数定義は

public class DataAccessMaster 
{ 

    /// <summary> 
    /// This function gets the list of all the databases present in the local server. 
    /// </summary> 
    /// <returns></returns> 
    public static DataSet GetAllDataBaseNames() 
    { 
     SqlConnection sConnection = new 
      SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); 
     //To Open the connection. 
     sConnection.Open(); 

     string selectDatabase = @"SELECT 
             [NAME] 
            FROM  
             [master..sysdatabases]"; 

     SqlCommand sCommand = new SqlCommand(selectDatabase, sConnection); 

     try 
     { 

      DataSet dsListOfDatabases = new DataSet("master..sysdatabases"); 
      SqlDataAdapter da = new SqlDataAdapter(selectDatabase, sConnection); 
      da.TableMappings.Add("Table", "master..sysdatabases"); 
      da.Fill(dsListOfDatabases); 

      DataViewManager dsv = dsListOfDatabases.DefaultViewManager; 
      return dsListOfDatabases; 
     } 
     catch (Exception ex) 
     { 
      EventLog log = new EventLog("Application"); 
      log.Source = "MFDBAnalyser"; 
      log.WriteEntry(ex.Message); 
      return null; 
     } 

     finally 
     { 
      //To close the connection 
     if(sConnection != null) 
      { 
       sConnection.Close(); 
      } 
     } 

のような別のクラスであり、私はと呼ばれますこの

public void BindDBDropDown() 
    { 
     DataSet dsDatabaseList = default(DataSet); 

     try 
     { 
      //The function GetAllDataBaseNames() is called from the DataAccessMaster class. 
      dsDatabaseList = DataAccessMaster.GetAllDataBaseNames(); 

      //Path to the combo box is given to get the value through the GetAllDataBaseNames(). 
      cmbDatabases.DataSource = dsDatabaseList.Tables["master..sysdatabases"]; 
      cmbDatabases.DisplayMember = "NAME"; 
      cmbDatabases.ValueMember = (""); 
     } 

しかし、それは、ドロップダウンに必要なリストをバインドされていないようにこの機能。 皆さん、私を助けてくれるのですか?

+0

ドロップダウンリストで必要なリストを結合しないでみては?あなたのドロップダウンには何が表示されますか?それは例外をスローしますか?はいの場合は? – pavanred

+0

これは何もバインドされておらず、例外もスローされません。 – Srivastava

+0

btw、try/finally接続を閉じるのではなく、 'using'を使うほうがクリーンで堅牢です。 –

答えて

1

私はこの問題を推測しているが、主にここにある:

catch (Exception ex) 
    { 
     EventLog log = new EventLog("Application"); 
     log.Source = "MFDBAnalyser"; 
     log.WriteEntry(ex.Message); 
     return null; 
    } 

何かがうまくいかない場合は、(nullを返す)、それを嚥下されています。 catchBindDBDropDownにあるものは表示されませんが、私はそれが類似していると推測しています...

1

"cmbDatabases.DataBind();" BindDBDropDown()に修正しますか? また、BindDBDropDown()をデバッグすると、データソースにはデータテーブルの行がありますか?

1

cmbDatabases.DataSource = dsDatabaseList.Tables["Table"]; 

それとも

cmbDatabases.DataSource = dsDatabaseList.Tables[0];