2010-11-23 73 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];