mrpddnos Posted December 25, 2008 Posted December 25, 2008 Hi everyone, I'm trying to set up a piece of code that does the following. In a RichTextBox I add a series of numbers (each number on a different line). Then I press the add butting a foreach loop reads the first line and starts a new forach loop. this new loop sees wether the number is allready in the dataset. If so, a error is shown. If not, the loop continues and the number is added to the dataset. Then it starts over again with the next number, and so on. The propblem is, when I start debugging I get the messagebox stating that the loop was succesfull everytime, though no numbers are added to the dataset. Here is the code: private void btnNropslaan_Click(object sender, EventArgs e) { foreach (string nummer in rtbRoepnummers.Lines) { foreach (DataRow drRoepnrc in dsLogging.Tables["roepnummers"].Rows) { if (drRoepnrc["nummer"].ToString() != nummer) { DataRow drRoepnr = dsLogging.Tables["roepnummers"].NewRow(); drRoepnr["nummer"] += nummer; dsLogging.Tables["roepnummers"].Rows.Add(drRoepnr["nummer"]); lbroepnrnieuwbericht1.Items.Add(drRoepnr[0]); } else { MessageBox.Show("Een van de roepnummers bestaat al", "Error"); } } } MessageBox.Show("Roepnummer is toegevoegd!", "Toegevoegd!"); } The code used to work when I did not had the program check if the number exist yet. What did I do wrong? yours, bernhard Quote
Administrators PlausiblyDamp Posted December 26, 2008 Administrators Posted December 26, 2008 If you step through the code in the debugger does it step through the loops as you would expect? Does the if statement equal true? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted December 26, 2008 Author Posted December 26, 2008 The entire if statement is not being procecced... Quote
Administrators PlausiblyDamp Posted December 27, 2008 Administrators Posted December 27, 2008 On the line foreach (DataRow drRoepnrc in dsLogging.Tables["roepnummers"].Rows) What is the value of the .Rows property? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted December 27, 2008 Author Posted December 27, 2008 It seems like .Rows has no value. In the Watch I get "The name rows does not exist in the current context" Quote
Administrators PlausiblyDamp Posted December 27, 2008 Administrators Posted December 27, 2008 Does dsLogging.Tables["roepnummers"] have a value then? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted December 28, 2008 Author Posted December 28, 2008 dsLogging.Tables["roepnummers"] had the value "roepnummers" Quote
Administrators PlausiblyDamp Posted December 28, 2008 Administrators Posted December 28, 2008 Did dsLogging.Tables["roepnummers"].Rows not have any value? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted December 28, 2008 Author Posted December 28, 2008 According to the "watch" it didn't Quote
Administrators PlausiblyDamp Posted December 29, 2008 Administrators Posted December 29, 2008 Seems odd, I can't really understand how dsLogging.Tables["roepnummers"] can have a value while dsLogging.Tables["roepnummers"].Rows doesn't have a value. Did you put a watch on the full dsLogging.Tables["roepnummers"].Rows expression or just on .Rows? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted December 29, 2008 Author Posted December 29, 2008 This time I added a Watch to dsLogging.Tables["roepnummers"].Rows. As you can see in the exhibit rtbRoepnummers.Lines has the value's [0]=1, [1]=2, [2]=3 and [3]=4. dsLogging.Tables["roepnummers"].Rows had the value {System.Data.DataRowCollection}. Count = 0. http://www.compuservice-oldebroek.nl/images/mai/watch-dslogging.jpg Quote
Administrators PlausiblyDamp Posted December 29, 2008 Administrators Posted December 29, 2008 You probably need to add a dsLogging.Tables["roepnummers"].Commit() before the closing } in the if statement. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted December 29, 2008 Author Posted December 29, 2008 I get the folowing error when I add dsLogging.Tables["roepnummers"].Commit() between the if (...) { } 'System.Data.DataTable' does not contain a definition for 'Commit' and no extension method 'Commit' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?) Here the full section of the code: private void btnNropslaan_Click(object sender, EventArgs e) { foreach (string nummer in rtbRoepnummers.Lines) { foreach (DataRow drRoepnrc in dsLogging.Tables["roepnummers"].Rows) { if (drRoepnrc["nummer"].ToString() == nummer) { DataRow drRoepnr = dsLogging.Tables["roepnummers"].NewRow(); drRoepnr["nummer"] += nummer; dsLogging.Tables["roepnummers"].Rows.Add(drRoepnr["nummer"]); lbroepnrnieuwbericht2.Items.Add(drRoepnr[0]); ClbRoepnr.Items.Add(drRoepnr[0]); dsLogging.Tables["roepnummers"].Commit(); } else { MessageBox.Show("Een van de roepnummers bestaat al", "Error"); } } } MessageBox.Show("Roepnummer is toegevoegd!", "Toegevoegd!"); } Quote
Administrators PlausiblyDamp Posted December 29, 2008 Administrators Posted December 29, 2008 Try http://msdn.microsoft.com/en-us/library/system.data.datatable.acceptchanges.aspx instead Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted December 29, 2008 Author Posted December 29, 2008 now I get the folowing errors: Error 1 Non-invocable member 'System.Data.DataTable.HasErrors' cannot be used like a method. Error 2 The name 'Reconcile' does not exist in the current context Here is the code: private void AcceptOrReject(DataTable dtRoepnummers) { if (dtRoepnummers.HasErrors()) { if (Reconcile(dtRoepnummers)) { dtRoepnummers.AcceptChanges(); } else { dtRoepnummers.RejectChanges(); } } else { dtRoepnummers.AcceptChanges(); } } Quote
Administrators PlausiblyDamp Posted December 29, 2008 Administrators Posted December 29, 2008 HasErrors is a property not a method so remove the () from it. Is Reconcile a method of your own? If so make sure all the relevant namespaces etc. are in a using clause at the top or that it exists in the current class. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted December 29, 2008 Author Posted December 29, 2008 I have the folowing now: private void AcceptOrReject(DataTable dtRoepnummers) { if (dtRoepnummers.HasErrors) { dtRoepnummers.RejectChanges(); MessageBox.Show("Mislukt", "error"); } else { dtRoepnummers.AcceptChanges(); MessageBox.Show("Gelukt", "error"); } } The code isn't even being proccesed. I added breakpoints to the code. Quote
mrpddnos Posted January 3, 2009 Author Posted January 3, 2009 Is there anyone who can help me further? Quote
Administrators PlausiblyDamp Posted January 3, 2009 Administrators Posted January 3, 2009 What do you mean by isn't even being proccesed is this method not being called? Are parts of it not running? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted January 4, 2009 Author Posted January 4, 2009 What do you mean by is this method not being called? Are parts of it not running? Maybe this will help? Here is the full code (atleast the parts that have to do with this): 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 MAI_Meldkamer_Logginh { public partial class main : Form { public main() { InitializeComponent(); } //create dataset DataSet dsLogging = new DataSet(); DataTable dtRoepnummers = new DataTable("roepnummers"); DataTable dtLogging = new DataTable("logging"); //Dataset Accept or Reject changes private void AcceptOrReject(DataTable dtRoepnummers) { if (dtRoepnummers.HasErrors) { dtRoepnummers.RejectChanges(); MessageBox.Show("Mislukt", "error"); } else { dtRoepnummers.AcceptChanges(); MessageBox.Show("Gelukt", "error"); } } //Load form, add DataSet Columns, Read XML file private void main_load(object sender, EventArgs e) { dtRoepnummers.Columns.Add("nummer", typeof(string)); dsLogging.Tables.Add(dtRoepnummers); dsLogging.ReadXml("roepnummers.xml"); lsvBerichten.Columns.Add("Tijd"); lsvBerichten.Columns.Add("Roepnummer"); lsvBerichten.Columns.Add("Bericht"); } //Safe callsigns private void btnNropslaan_Click(object sender, EventArgs e) { foreach (string nummer in rtbRoepnummers.Lines) { foreach (DataRow drRoepnrc in dsLogging.Tables["roepnummers"].Rows) { if (drRoepnrc["nummer"].ToString() == nummer) { DataRow drRoepnr = dsLogging.Tables["roepnummers"].NewRow(); drRoepnr["nummer"] += nummer; dsLogging.Tables["roepnummers"].Rows.Add(drRoepnr["nummer"]); lbroepnrnieuwbericht2.Items.Add(drRoepnr[0]); ClbRoepnr.Items.Add(drRoepnr[0]); } else { MessageBox.Show("Een van de roepnummers bestaat al", "Error"); } } } MessageBox.Show("Roepnummer is toegevoegd!", "Toegevoegd!"); } } Quote
Administrators PlausiblyDamp Posted January 4, 2009 Administrators Posted January 4, 2009 AcceptOrReject isn't being called from anywhere in the code you posted, did you mean to call it from somewhere or did you not post the code that does call it? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
mrpddnos Posted January 11, 2009 Author Posted January 11, 2009 With my .DOT teacher I looked at the code and he gave me the next advise: I sould write a code that checks wether or not dslogging.Tables["roepnummers"] is empty. If the table is empty, then the numers should just be entered. If it's not empty then each number should be checked to see if the numer allready exists. This is what I did: private void btnNropslaan_Click(object sender, EventArgs e) { if (dsLogging.Tables["roepnummers"].Rows.Count == 0) { foreach (string nummer in rtbRoepnummers.Lines) { DataRow drRoepnr = dsLogging.Tables["roepnummers"].NewRow(); drRoepnr["nummer"] += nummer; dsLogging.Tables["roepnummers"].Rows.Add(drRoepnr["nummer"]); lbroepnrnieuwbericht2.Items.Add(drRoepnr[0]); ClbRoepnr.Items.Add(drRoepnr[0]); } } else { foreach (string nummer in rtbRoepnummers.Lines) { foreach (DataRow drRoepnrc in dsLogging.Tables["roepnummers"].Rows) { if (drRoepnrc["nummer"].ToString() != nummer) { DataRow drRoepnr = dsLogging.Tables["roepnummers"].NewRow(); drRoepnr["nummer"] += nummer; dsLogging.Tables["roepnummers"].Rows.Add(drRoepnr["nummer"]); lbroepnrnieuwbericht2.Items.Add(drRoepnr[0]); ClbRoepnr.Items.Add(drRoepnr[0]); } break; } } } MessageBox.Show("Roepnummer is toegevoegd!", "Toegevoegd!"); } What I now get is the folowing. When I add some numbers everything works fine. When I update the number list with new numbers the variable "nummer" stays "1" though it should be increased each time. [b]Example:[/b] A list of numbers in the dataset... 1 2 In the RichTextBox the folowing numbers are shown: 1 2 I update the list to this: 1 2 3 4 5 The result in the dataset will be: 1 2 2 3 4 5 I add the folowing numbers: 6 7 Result: 1 2 2 3 4 5 2 3 4 5 6 7 As you can see only the FIRST number will be used to check if it allready exist. It should check every number in the dataset. How can I get this done? The reason why I started with this is because according to my teacher I am trying to use data from an empty datatable. And that can't be done. And that sounds right since the program starts to work now that I have the software checking the table wether or not it is empty. 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.