0

在我的项目中用于数据访问的两种形式和单独的类(DataConn)。Form1 中的按钮打开 Form2,并且此(子)表单调用 DataConn 类中的 GetData 方法以使用名称填充 Form2 文本框。Form2 具有用于检索下一条记录的按钮,该按钮正在调用 DataConn 类中的 NavigateRecords 方法。NavigateRecord 方法是从 GetData 内部调用的,以便在加载时填充 Form2 中的文本框,以及从 Form2 的 Next 事件中检索下一条记录。当从 Form2 Next 按钮事件调用 NavigateRecords 时,此行上的错误“对象引用未设置为对象的实例”:myDR = myDS.Tables["People"].Rows[increment]; 在导航记录中。为什么在 Form2 加载事件上执行同一行时不会发生这种情况?如果我创建 DataRow 的一个实例,则会出现警告“System.Data.

DataConn 类:

public class DataConn
{
    private DataSet myDS;
    private DataRow myDR;
    private int maxRows = 0;
    private int increment = 0;
    private string name;

    public int MaxRows
    { get { return maxRows; } }

    public int Increment
    { get { return increment; } set { increment = value; } }

    public string Name
    { get { return name; } }

    public void GetData()
    {
        // class variable
        string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Temp\Testing\TestDatabase.accdb";

        //everything created in 'using' gets destroyed/goes out of scope at end of 'using' block.
        using (OleDbConnection myConn = new OleDbConnection(connectionString))
        {
            string SQL = "SELECT * From Test";
            OleDbDataAdapter myDA = new OleDbDataAdapter(SQL, myConn);
            myDS = new DataSet();
            try
            {
                myConn.Open();
                myDA.Fill(myDS, "People");
                NavigateRecords();  //called to populate textbox in NewForm
                maxRows = myDS.Tables["People"].Rows.Count;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myConn.Close();
            }
        }
    }

    public void NavigateRecords()
    {
        try
        {
            myDR = myDS.Tables["People"].Rows[increment];//exception             
            name = myDR.ItemArray.GetValue(1).ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
        } 

    }
}

Form2 加载和 Next_CLick 事件:

private void frmNewForm_Load(object sender, EventArgs e)
    {
        DataConn theConn = new DataConn();
        theConn.GetData();
        txtName.Text = theConn.Name;
    }

private void btnNext_Click(object sender, EventArgs e)
    {
        DataConn theConn = new DataConn();
        if (theConn.Increment != theConn.MaxRows - 1)
        {
            theConn.Increment++;
            theConn.NavigateRecords();
            FillTextBox();
        }

编辑:我创建了一个新项目并将所有内容都包含在表单类本身中。Datarow 最初为 null,当在 NavigateRecords 方法中调用时,它设置为 System.Data.DataRow,只要表单打开,它就会保持不变。在我的原始项目中,当从 Form2 的下一个事件再次调用数据行时,它返回 null 并引发异常。为什么当它开始时它是 null 很重要?

4

1 回答 1

0

尝试这个:

DataConn theConn;
private void frmNewForm_Load(object sender, EventArgs e)
{
    theConn = new DataConn();
    theConn.GetData();
    txtName.Text = theConn.Name;
}

private void btnNext_Click(object sender, EventArgs e)
{
    if (theConn.Increment != theConn.MaxRows - 1)
    {
        theConn.Increment++;
        theConn.NavigateRecords();
        FillTextBox();
    }
}
于 2013-06-20T17:35:45.180 回答