1

After doing more reading and research into managing data, I have redone the code based off the MSDN site for a dataset; however, I am running into a snag. I believe I have declared the dataset within a class-level; however, whenever I attempt to pull that dataset with

DataRow[] drRow = tblEmployee.Select("EmployeeKey = " + TextBox12.Text)

I am getting the errors:

  1. The name 'tblEmployees' does not exist in the current context
  2. The name 'TextBox12' does not exist in the current context

I want to be able to declare it at the class level so I can call it from anywhere within the form.

I have already read through the MSDN site on class levels, and honestly I think that has just confused me to a greater degree.

Basically is what I want to happen is dataset provides a list of employees in alphabitical order by first name, then when a user selects the the employee from drop down box, it should select the employee ID and display it in textBox12, then perform a search of the tblEmplpoyees for the remaining data to display for the employee such as fields defined in //comments in the code.

Can someone assist me with this, here is what I have for code atm:

namespace Tempus.Menus
{
public partial class Employees : Form
{
    public class myData
    {
        DataSet[]drRow = tblEmployees.Select("EmployeeKey = " + TextBox12.Text);;
    }
    public Employees()
    {
        InitializeComponent();
        //Connect to database for Employees Table Headers
        SqlConnection myConnection = new SqlConnection(@"Server=server\CHCTEMPUS;Integrated Security=true;" +
            "user id=userID;password=password;" +
            "Trusted_Connection=yes;" +
            "Database=Database;" +
            "connection timeout=30");

        SqlDataAdapter daEmployees
            = new SqlDataAdapter("Select *, Lname +', '+ Fname as LastFirst, Fname +' '+ Lname as FirstLast FROM Employees WHERE Lname IS NOT NULL AND Fname IS NOT NULL", myConnection);



        DataSet dsEmployees = new DataSet("Employees");
        daEmployees.FillSchema(dsEmployees, SchemaType.Source, "Employees");
        daEmployees.Fill(dsEmployees, "Employees");

        DataTable tblEmployees;
        tblEmployees = dsEmployees.Tables["Employees"];



        comboBox1.DisplayMember = "FirstLast";
        comboBox1.ValueMember = "employeeNumber";
        comboBox1.DataSource = tblEmployees;



        try
        { }

        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
        finally
        {
            if (myConnection != null)
            {
                myConnection.Dispose();
            }


        }
        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;

        comboBox2.SelectedIndexChanged += comboBox2_SelectedIndexChanged;

    }



  private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedIndex != -1)
        {
            textBox1.Text = string.Empty;
        }
        {
            textBox12.Text = comboBox1.SelectedValue.ToString();

        }
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (comboBox2.SelectedIndex == -1)
        {
            textBox1.Text = string.Empty;
        }
        else
        {

            textBox1.Text = comboBox2.SelectedItem.ToString();
        }
    }



    private void Employees_Load(object sender, EventArgs e)
    {

    }
    private void label1_Click(object sender, EventArgs e)
    {

    }
    private void textBox1_TextChanged_1(object sender, EventArgs e)
    {
    //Last Name

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
    //First Name
    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
    //Middle Name
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
    //Hire Date
    }

    private void textBox5_TextChanged(object sender, EventArgs e)
    {
    //Term Date
    }

    private void textBox6_TextChanged(object sender, EventArgs e)
    {
    //Company
    }

    private void textBox7_TextChanged(object sender, EventArgs e)
    {
    //Notes
    }

    private void textBox8_TextChanged(object sender, EventArgs e)
    {
    //PTO
    }

    private void textBox9_TextChanged(object sender, EventArgs e)
    {
    //SNP
    }

    private void textBox10_TextChanged(object sender, EventArgs e)
    {
    //Credentials
    }

    private void textBox11_TextChanged(object sender, EventArgs e)
    {
    //Employee Full Name (First Last)

    }

    private void textBox12_TextChanged(object sender, EventArgs e)
    {
    //Employee ID
    }

    private void textBox13_TextChanged(object sender, EventArgs e)
    {
    //Branch

    }
    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBox1.Checked)

                {

            // logic here for if the box has now been checked


                }

            else

                {

                // what to do if the box has been unchecked


                }

    }
    private void button1_Click(object sender, EventArgs e)
    {
        Main myNewForm = new Main();

        myNewForm.Show();

        this.Close();
    }


    private void button2_Click(object sender, EventArgs e)
    {
        Reports myNewForm = new Reports();

        myNewForm.Show();

        this.Close();
    }


}

}

4

2 回答 2

0

您不能以这种方式初始化 myClass 的 drRow 成员。首先, tblEmployees 变量是一个局部变量,当Employees 构造函数完成时它就不再存在了。所以,没了。但即使它是一个类成员(在类级别范围内定义),您仍然无法像现在这样使用它。

考虑摆脱您的 myData 类,并将声明放在与您的员工类相同的范围级别。

public partial class Employees : Form 
{ 
    DataSet[]drRow;
    public Employees() 
    { 
// etc, etc.

顺便说一句,这也应该解决 TextBox12 上下文问题。

于 2012-07-24T21:26:35.077 回答
0

您正在以下构造函数中创建一个局部变量Employees

DataTable tblEmployees;
tblEmployees = dsEmployees.Tables["Employees"];

现在您正尝试从另一个类中访问该局部变量:

public class myData
{
    DataSet[]drRow = tblEmployees.Select("EmployeeKey = " + TextBox12.Text);;
}

当然,这是行不通的。出于同样的原因,您无权访问TextBox12表单中的实例。

我假设您实际上想DataTable通过输入的文本过滤 。您应该处理一个事件,例如(过滤器)按钮单击事件。

然后,您可以使用更具可读性和可维护性的 LNQ-To-DataSet 来过滤行。例如,您可以将表声明为表单中的成员变量并在form_load中对其进行初始化:

private DataTable tblEmployees;

private void buttonFilter_Click(object sender, System.EventArgs e)
{
   if(tblEmployees != null)
   {
       DataTable filtered = tblEmployees.AsEnumerable()
                            .Where(r => r.Fields<String>("EmployeeKey") == TextBox12.Text)
                            .CopyToDataTable();
       // do something with it ...
   }
}
于 2012-07-24T21:19:08.370 回答