mrpddnos Posted November 25, 2008 Posted November 25, 2008 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(); } Quote
Nate Bross Posted November 25, 2008 Posted November 25, 2008 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
mrpddnos Posted November 25, 2008 Author Posted November 25, 2008 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(); } } } Quote
Nate Bross Posted November 26, 2008 Posted November 26, 2008 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
mrpddnos Posted November 26, 2008 Author Posted November 26, 2008 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> Quote
Administrators PlausiblyDamp Posted November 26, 2008 Administrators Posted November 26, 2008 If you step through the code when you hit the line DataRow drStudent = dsStudenten.Tables["student"].NewRow(); Do either dsStudenten or Tables["student"] appear as null in the watch window? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted November 26, 2008 Author Posted November 26, 2008 I'm not sure, when I press "watch" in the lower-left I see no output there... Quote
Nate Bross Posted November 26, 2008 Posted November 26, 2008 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Administrators PlausiblyDamp Posted November 26, 2008 Administrators Posted November 26, 2008 If you add a watch on those two items what appears in the watch window? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted November 28, 2008 Author Posted November 28, 2008 How do I add a watch to those items? Quote
Administrators PlausiblyDamp Posted November 28, 2008 Administrators Posted November 28, 2008 Right click on them and select 'Add Watch' while in the debugger. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted November 29, 2008 Author Posted November 29, 2008 Hmmmm. I can't select "ad watch"... The option seems to be deactivated (gray) Quote
Administrators PlausiblyDamp Posted November 29, 2008 Administrators Posted November 29, 2008 Highlight the code dsStudenten.Tables["student"] and right click - does the option appear then? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted November 30, 2008 Author Posted November 30, 2008 No. When I highlight the code the option still doen't become active. http://www.med-base-one.com/add%20watch.jpg Quote
Nate Bross Posted December 1, 2008 Posted December 1, 2008 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
mrpddnos Posted December 11, 2008 Author Posted December 11, 2008 Declaration statements are only allowed in the immediate window When I add a watch to the line the abough error shows. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.