OleDbDataReader将值传递给textbox.Text

时间:2017-02-03 22:12:59

标签: c# winforms

我有两个表单,一个主表单,其中一个DataGridView列出了表的行。我想双击(或浏览菜单)以新的形式打开所选记录进行编辑。

我已成功将记录索引传递给新表单,并且只是运行另一个查询来从数据库中提取该记录。

我正在收到System.NullReferenceException => {“对象引用未设置为对象的实例。”}错误我将textboxes.Text设置为读取器的值,我无法弄明白。现在这是一个大杂烩,因为我只是在尝试我能找到的每一个解决方案。

如果我做一个MessageBox.Show(reader [x] .ToString()),它工作正常,我得到我想要的值,所以我不认为这是数据,它只是将数据设置到消息框。我无法解开的文字。我想这样做,所以可以编辑值并将其保存回数据库(这将是下一个令人头痛的问题)。

以下是我应该显示个人记录的表单代码。

public partial class IndividualIncident : Form
{
    private int currentRecordIndex;
    private OleDbConnection connString;
    private OleDbCommand command;

    public IndividualIncident(string action, int rowIndex)
    {
        if (action == "modify")
        {
            currentRecordIndex = rowIndex;
            // do stuff with rowIndex
            connString = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\AssetTracking.accdb");
            command = new OleDbCommand("SELECT id,serialnumber,owner,datesubmitted,dateprocessed,technician,status,datereturned,problem from incidents where id="+currentRecordIndex, connString);
            connString.Open();
            OleDbDataReader reader;
            reader = command.ExecuteReader();

            while (reader.Read())
            {
                string serno = reader.GetString(1);
                string owner = reader.GetString(2).ToString();
                //serialBox.Text = serno;
                ownerBox.Text = owner;
                //dateSubmittedBox.Text = reader[3].ToString();
                //dateProcessedBox.Text = reader[4].ToString();
                //technicianBox.Text = reader[5].ToString();
                //statusBox.Text = reader[6].ToString();
                //dateReturnedBox.Text = reader[7].ToString();
                //problemBox.Text = reader[8].ToString();
            }
        }
    }

0 个答案:

没有答案