Trouble with ds.ReadXml(myFileStream);
-
Saturday, August 04, 2012 9:49 PM
I have verified that my passfile.xml exists and contains:
<AlarmData>
<Alarms>
<Alarm>02:45 PM</Alarm>
<Days>Everyday</Days>
<Sound>Default</Sound>
<Volume>25%</Volume>
<Enabled>True</Enabled>
</Alarms>
<Alarms>
<Alarm>03:45 PM</Alarm>
<Days>Everyday</Days>
<Sound>Default</Sound>
<Volume>25%</Volume>
<Enabled>False</Enabled>
</Alarms>
<Alarms>
<Alarm>04:45 PM</Alarm>
<Days>Everyday</Days>
<Sound>Default</Sound>
<Volume>25%</Volume>
<Enabled>False</Enabled>
</Alarms>
</AlarmData>I then have code:
if (File.Exists("passfile.xml")) //<--- This is where I set my breakpoint { myFileStream = new System.IO.FileStream("passfile.xml", System.IO.FileMode.Open); ds.ReadXml(myFileStream); //<--- Program seems to stop running here. myFileStream.Close(); if (ds.Tables.Contains("Alarms")) { for (int i = 0; i < ds.Tables["Alarms"].Rows.Count; i++) { dataGridView1.Rows.Add(ds.Tables["Alarms"].Rows[i]["Alarm"], ds.Tables["Alarms"].Rows[i]["Days"], ds.Tables["Alarms"].Rows[i]["Sound"], ds.Tables["Alarms"].Rows[i]["Volume"], ds.Tables["Alarms"].Rows[i]["Enabled"]); } } } else { ds = new DataSet("AlarmData"); alarms = ds.Tables.Add("Alarms"); alarms.Columns.AddRange(new DataColumn[] { new DataColumn("Alarm"), new DataColumn("Days"), new DataColumn("Sound"), new DataColumn("Volume"),new DataColumn("Enabled") }); }
For some reason the rest of the code after ds.ReadXml(myFileStream) does not ever seem to be executing. In fact the debugger seems to kick me out of the program at that line exactly and then Form1 is displayed. I am very perplexed as to what is going on. Thanks in advance for any suggestions, comments or answers!
All Replies
-
Saturday, August 04, 2012 11:58 PMAnswerer
I have tested your XML myself with your method and I can confirm that it should normally work fine, I got the dataset as you'd expect.
This kind of freeze considering it was a file access points to a problem with opening the file. You need to check permissions, move it around, experiment and find how you can use it. Maybe you had it open at the same time in Notepad or something...
if (File.Exists("c:\\test.xml")) //<--- This is where I set my breakpoint { var myFileStream = new System.IO.FileStream("c:\\test.xml", System.IO.FileMode.Open); var ds = new DataSet(); ds.ReadXml(myFileStream); //<--- Worked fine
myFileStream.Close(); }Regards,
Pete
#PEJL
-
Sunday, August 05, 2012 3:01 AM
Do you have this wrapped in a try/catch?
"debugger seems to kick me out of the program at that line exactly and then Form1 is displayed"
This would suggest an exception is being thrown but not caught.
-
Sunday, August 05, 2012 4:23 AMThere is no try catch.
-
Sunday, August 05, 2012 4:30 AMI tried also copying all the files to my local disk (C:\) on my desktop machine I am using and checking that "passfile.xml" is not set to read only after restarting the system. The same problem occurs.
- Edited by S.e.p.y Sunday, August 05, 2012 4:32 AM
-
Sunday, August 05, 2012 4:57 AMModerator
Rather than trying to guess what the problem might be, why don' t you wrap it all up in a try/catch and write the Exception.message in the catch. Then you might know what is causing the problem, assuming that the Exception message is helpful...~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.com- Marked As Answer by S.e.p.y Sunday, August 05, 2012 5:51 AM
-
Sunday, August 05, 2012 5:51 AM
This code turns out to be the much better code:
if (File.Exists("passfile.xml")) //<--- This is where I set my breakpoint { try { myFileStream = new System.IO.FileStream("passfile.xml", System.IO.FileMode.Open); ds = new DataSet("AlarmData"); ds.ReadXml(myFileStream); //<--- Program seems to stop running here. myFileStream.Close(); if (ds.Tables.Contains("Alarms")) { for (int i = 0; i < ds.Tables["Alarms"].Rows.Count; i++) { dataGridView1.Rows.Add(ds.Tables["Alarms"].Rows[i]["Alarm"], ds.Tables["Alarms"].Rows[i]["Days"], ds.Tables["Alarms"].Rows[i]["Sound"], ds.Tables["Alarms"].Rows[i]["Volume"], ds.Tables["Alarms"].Rows[i]["Enabled"]); } } } catch (Exception message) { MessageBox.Show(message.Message.ToString()); } } else { ds = new DataSet("AlarmData"); alarms = ds.Tables.Add("Alarms"); alarms.Columns.AddRange(new DataColumn[] { new DataColumn("Alarm"), new DataColumn("Days"), new DataColumn("Sound"), new DataColumn("Volume"),new DataColumn("Enabled") }); }
-
Sunday, August 05, 2012 12:07 PMBest to always wrap things in try/catch; espcially operations that can be error prone; like file access.
-
Sunday, August 05, 2012 12:14 PMWhy do you say the program "seems" to stop running at the ReadXml line? Have you stepped through the code in the debugger line by line?My guess is that the datagridview does not have the columns defined, and is failing on the Rows.Add call, or that one of the columns is defined wrong and not accepting the data.
--
Mike -
Sunday, August 05, 2012 3:11 PMModerator
And what was the result of wrapping it all in a try/catch? Did you determine the cause of your problem or not?
Also, I think you'd be better off databinding that grid rather than adding rows to it like you're doing.
if (ds.Tables.Contains("Alarms")) { dataGridView1.DataSource = ds.Tables["Alarms"]; }~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.com -
Sunday, August 05, 2012 6:51 PM
I like the suggestion and I will try it. I have to admit I have been wrestling around with this data set object. I'm still having a bit of a problem with one of the objects throwing an exception for not being set yet here:
private void Form1_Load(object sender, EventArgs e) { dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = "hh:mm tt"; dateTimePicker1.ShowUpDown = true; comboBoxAlarmVolume.Items.Add("10%"); comboBoxAlarmVolume.Items.Add("20%"); comboBoxAlarmVolume.Items.Add("30%"); comboBoxAlarmVolume.Items.Add("40%"); comboBoxAlarmVolume.Items.Add("50%"); comboBoxAlarmVolume.Items.Add("60%"); comboBoxAlarmVolume.Items.Add("70%"); comboBoxAlarmVolume.Items.Add("80%"); comboBoxAlarmVolume.Items.Add("90%"); comboBoxAlarmVolume.Items.Add("100%"); trayMenu = new ContextMenu(); trayMenu.MenuItems.Add("Exit", new EventHandler(MenuExit)); trayMenu.MenuItems.Add("Panel", new EventHandler(MenuRestore)); // Initialize the tray icon trayIcon = new NotifyIcon(); trayIcon.Text = "sepysalarm"; trayIcon.Icon = this.Icon; // Set the ContextMenu object for the tray icon trayIcon.ContextMenu = trayMenu; // Show the tray icon trayIcon.Visible = true; Application.ApplicationExit += new EventHandler(Application_ApplicationExit); if (File.Exists("passfile.xml")) { try { myFileStream = new System.IO.FileStream("passfile.xml", System.IO.FileMode.Open); ds = new DataSet("AlarmData"); alarms = ds.Tables.Add("Alarms"); ds.ReadXml(myFileStream); myFileStream.Close(); if (ds.Tables.Contains("Alarms")) { for (int i = 0; i < ds.Tables["Alarms"].Rows.Count; i++) { dataGridView1.Rows.Add(ds.Tables["Alarms"].Rows[i]["Alarm"], ds.Tables["Alarms"].Rows[i]["Days"], ds.Tables["Alarms"].Rows[i]["Sound"], ds.Tables["Alarms"].Rows[i]["Volume"], ds.Tables["Alarms"].Rows[i]["Enabled"]); } } } catch (Exception message) { MessageBox.Show(message.Message.ToString()); } } else { ds = new DataSet("AlarmData"); alarms = ds.Tables.Add("Alarms"); alarms.Columns.AddRange(new DataColumn[] { new DataColumn("Alarm"), new DataColumn("Days"), new DataColumn("Sound"), new DataColumn("Volume"),new DataColumn("Enabled") }); } }
private void Baddalarm_Click(object sender, EventArgs e) { try { string wholeTimeValue; wholeTimeValue = dateTimePicker1.Value.ToString("hh:mm tt"); if (dataGridView1.Rows.Count == 0) { dataGridView1.Rows.Add(wholeTimeValue, "Everyday", "Default", "25%", "True"); DataRow workRow = alarms.NewRow(); workRow["Alarm"] = wholeTimeValue; workRow["Days"] = "Everyday"; workRow["Sound"] = "Default"; workRow["Volume"] = "25%"; workRow["Enabled"] = "True"; alarms.Rows.Add(workRow); } else { dataGridView1.Rows.Add(wholeTimeValue, "Everyday", "Default", "25%", "False"); for (int i = 1; i < dataGridView1.Rows.Count; i++) { dataGridView1.Rows[i].Cells[1].Value = 0; } DataRow workRow = alarms.NewRow(); //<-- Exception is thrown here if passfile.xml exists workRow["Alarm"] = wholeTimeValue; workRow["Days"] = "Everyday"; workRow["Sound"] = "Default"; workRow["Volume"] = "25%"; workRow["Enabled"] = "False"; alarms.Rows.Add(workRow); } for (int i = 0; i < dataGridView1.Rows.Count; i++) { dataGridView1.Rows[i].Cells[0].ReadOnly = true; } } catch (Exception message) { MessageBox.Show(message.Message.ToString()); } }
Sorry I had to post all this code but essentially what happens is if passfile.xml exists then an exception is thrown at DataRow workRow = alarms.newRow(); as the object is said to not be set yet. I'm not sure why that is because I would think that alarms.newRow(); would be setting the object. Perhaps I need to try databinding as you have the example.
Alternatively I was thinking about going through Hour 14 of Sams Teach yourself C# in 24 hours which I have in my library which outlines quite a different set of techniques for using XML.- Edited by S.e.p.y Sunday, August 05, 2012 7:11 PM
-
Sunday, August 05, 2012 9:05 PMModerator
That looks like it should have worked, but how about if you try this instead:
ds = new DataSet("AlarmData"); ds.ReadXml(myFileStream); alarms = ds.Tables["Alarms"];~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.com -
Sunday, August 05, 2012 9:43 PMThat's remarkable. Just a small change and that cleared up the whole problem. Thank you for the code.
-
Sunday, August 05, 2012 10:38 PMModeratorYou're welcome!
~~Bonnie Berent DeWitt [C# MVP]
geek-goddess-bonnie.blogspot.com

