DataRow - '对象引用未设置为对象的实例'

时间:2013-06-20 16:56:48

标签: c# winforms visual-studio-2008 ms-access-2010 nullreferenceexception

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

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 Load和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的下一个事件再次调用datarow时,它返回null并抛出异常。为什么从null开始时它是null是否重要?

1 个答案:

答案 0 :(得分: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();
    }
}