C# DataRow not working

mrpddnos

Member
Joined
Nov 25, 2008
Messages
24
Location
Oldebroek, The Netherlands
For a school assignment I have to write a small program that allows the user to safe a student number and status (present(1) or absent(0)) into a data set.

I wrote the code but when I start the project in debug mode I get an error when I try to add a new student to the dataset. I don;t know the exact error as my Visual Studio is the Dutch version. Its something about an object reference with has no object to refere to?

Below you kan see the code. I hope you can help me!

Dataset and Datatable
Code:
DataSet dsStudenten = new DataSet("studenten");
DataTable dtStudent = new DataTable("student");

The code in question
Code:
private void BtnAanmelden_Click(object sender, EventArgs e)
        {
            DataRow drStudent = dsStudenten.Tables["student"].NewRow();
            if (cbAanw.Checked)
            {
                instatus = "1";
            }
            else
            {
                instatus = "0";
            }
            drStudent["ovnummer"] = TbOvnr.Text;
            drStudent["instatus"] = instatus;
            dsStudenten.Tables["student"].Rows.Add(drStudent);
            LbStudenten.Items.Add(drStudent[0]);
            TbOvnr.Clear();
        }
 
It looks like you are not adding dtStudent object to dsStudenten.

Something like this at the Form Load may help you:

Code:
// define vars
DataSet dsStudenten = new DataSet("studenten");
DataTable dtStudent = new DataTable("student");

// add dataTable to dataSet
dsStudenten.Tables.Add(dtStudent);

This is how you tie your DataTable into your DataSet, later on you may add another DataTable like Professors, and then create a DataRelation between the two.
 
Hi Nate,

The piece of code you gave me... well... I allready had that. Guess I forgot to post that here, so here is it now:

Code:
private void form1_Load(object sender, EventArgs e)
{
dtStudent.Columns.Add("ovnummer", typeof(string));
dtStudent.Columns.Add("instatus", typeof(string));
dsStudenten.Tables.Add(dtStudent);
dsStudenten.ReadXml("studenten.xml");
}

If it might help to understand the (easy) structure of the program, here is the full code:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Studenten
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string instatus;
        string ovnummer;

        DataSet dsStudenten = new DataSet("studenten");
        DataTable dtStudent = new DataTable("student");

        private void form1_Load(object sender, EventArgs e)
        {
            dtStudent.Columns.Add("ovnummer", typeof(string));
            dtStudent.Columns.Add("instatus", typeof(string));
            dsStudenten.Tables.Add(dtStudent);
            dsStudenten.ReadXml("studenten.xml");
        }

        //private void btnToon_Click(object sender, EventArgs e)
        //{

        //}

        private void BtnAanmelden_Click(object sender, EventArgs e)
        {
            DataRow drStudent = dsStudenten.Tables["student"].NewRow();
            if (cbStatus.Checked)
            {
                instatus = "1";
            }
            else
            {
                instatus = "0";
            }
            drStudent["ovnummer"] = TbOvnr.Text;
            drStudent["instatus"] = instatus;
            dsStudenten.Tables["student"].Rows.Add(drStudent);
            LbStudenten.Items.Add(drStudent[0]);
            TbOvnr.Clear();
        }
    }
}
 
Which line do you get the exception, "Object reference not set to an instance of an Object?" What you posted looks good. Is it possible that the xml file you are reading is empty? If so, that line could be setting your DataSet back to null.
 
The error comes up at the folowing line:
Code:
DataRow drStudent = dsStudenten.Tables["student"].NewRow();

The XML is not empty. studenten.xml is placed in the debug folder. Below is the full contect of the XML, for reference:


Code:
<studenten>
<student>
<ovnummer>88091409</ovnummer> 
<in>1</in> 
</student>
<student>
<ovnummer>84226712</ovnummer> 
<in>0</in> 
</student>
<student>
<ovnummer>73829273</ovnummer> 
<in>1</in> 
</student>
<student>
<ovnummer>79387290</ovnummer> 
<in>1</in> 
</student>
</studenten>
 
If you step through the code when you hit the line
Code:
DataRow drStudent = dsStudenten.Tables["student"].NewRow();
Do either dsStudenten or Tables["student"] appear as null in the watch window?
 
Not sure if this is in your code that you are running, or just what you posted here, but:

You have this for every student (xml):
Code:
<student>
<ovnummer>88091409</ovnummer> 

[SIZE="4"][b]<in>1</in>[/b][/SIZE]

</student>

and this for every student (C#)
Code:
dtStudent.Columns.Add("ovnummer", typeof(string));
dtStudent.Columns.Add("[SIZE="4"][B]instatus[/B][/SIZE]", typeof(string));

Its possible this is causing your exception.
 
Back
Top