2010-11-23 72 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 2010-11-23 07:42:37

回答

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];