Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

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

 

The code in question

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();
       }

Posted

It looks like you are not adding dtStudent object to dsStudenten.

 

Something like this at the Form Load may help you:

 

// 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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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:

 

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:

 

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();
       }
   }
}

Posted
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.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

The error comes up at the folowing line:

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:

 

 

<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>

Posted

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):

<student>
<ovnummer>88091409</ovnummer> 

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

</student>

 

and this for every student (C#)

dtStudent.Columns.Add("ovnummer", typeof(string));
dtStudent.Columns.Add("[size="4"][b]instatus[/b][/size]", typeof(string));

 

Its possible this is causing your exception.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
I believe that you need to set a break point on the line (to put the debugger in to "pause" mode) in order to add a watch.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...