单独的类中的函数不起作用

时间:2010-11-23 07:33:46

标签: c# visual-studio

我必须在像这样的按钮点击事件上调用一个函数

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 = ("");
        }

但它没有在下拉列表中绑定所需的列表。 你们能帮我吧!!

3 个答案:

答案 0 :(得分:1)

我猜这个问题主要在这里:

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

如果出现任何问题,你会吞下它(返回null)。您没有显示catchBindDBDropDown的内容,但我猜它是相似的......

答案 1 :(得分:1)

添加“cmbDatabases.DataBind();”到BindDBDropDown()修复它? 另外,如果调试BindDBDropDown(),数据源是否在数据表中有任何行?

答案 2 :(得分:1)

尝试

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

cmbDatabases.DataSource = dsDatabaseList.Tables[0];