read RichTextBox line by line (C#)

mrpddnos

Freshman
Joined
Nov 25, 2008
Messages
25
Location
Wezep, The Netherlands
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:

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

watch-dslogging.jpg
 
I get the folowing error when I add dsLogging.Tables["roepnummers"].Commit() between the
Code:
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:
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!");
        }
 
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:

Code:
private void AcceptOrReject(DataTable dtRoepnummers)
        {
            if (dtRoepnummers.HasErrors())
            {
                if (Reconcile(dtRoepnummers))
                {
                    dtRoepnummers.AcceptChanges();
                }
                else
                {
                    dtRoepnummers.RejectChanges();
                }
            }
            else
            {
                dtRoepnummers.AcceptChanges();
            }
        }
 
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.
 
I have the folowing now:
Code:
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.
 
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):

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