Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • Administrators
Posted

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?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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

Posted

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!");
       }

Posted

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

Posted

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.

Posted
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!");
       }
}

Posted

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.

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