Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Problem validating data input to a DataTable

Problem validating data input to a DataTable

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcsharpdatabasemysqlvisual-studio
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ernesto R D
    wrote on last edited by
    #1

    Hello everyone, back to coding after a long absence so excuse my rustiness :sigh: When doing database apps ibe allways done the wrong thing & validated user input on the form, via simple checks against the controls .text property. Now i want to do it the right way, and concentrate all validation closer to the data side of things, in this case the data tables. So i decided to do it via the RowChanging event: On my table...

    public override void EndInit()
    {
    base.EndInit();
    customersRowChanging += customersRowChangeEvent;
    }

    public void customersRowChangeEvent(object sender, customersRowChangeEvent e)
    {
    if (e.Row.Name.Length == 0)
    {
    e.Row.SetColumnError("name", "Customer name cannot be empty");
    }
    else
    {
    e.Row.SetColumnError("name", "");
    }
    }

    On my app, in a form the textboxes & other input are bound to a BindingSource, wich in turn is bound to the datatable in the dataset (100% as per visual studio designer). To add a record, I call the BindingSource.AddNew(), and allow the user to enter the data and then on my handler for the "Save" button...

       try
            {
                Validate();
                customersBindingSource.EndEdit();
                customersTableAdapter.Update(samDataSet.customers);
                // other stuff here, close the form, etc.
            }
            catch(Exception ex)
            {
                // ... show a message box with the error.
    
            }
    

    Even without handling the rowChange event, This works when a datatable or database constraint is violated, e.g. if the name is NULL or not unique (as specified in the data table constraints), an exception occurs, and the record is not saved. However, Even with the RowChangeEvent implemented as above, if a record with an empty (not null, empty string) name is in the record, no exception is thrown, the datatable gladly accepts the new record, and the table adapter saves it (the MySQL database does not enforce empty string values). Ibe checked, and the handler does get called, and the call to SetColumnError is made, but it doesnt prevent the table from accepting the new row. I read to use this method from here: Validate data in datasets[^] And i was under the impression tha

    L 1 Reply Last reply
    0
    • E Ernesto R D

      Hello everyone, back to coding after a long absence so excuse my rustiness :sigh: When doing database apps ibe allways done the wrong thing & validated user input on the form, via simple checks against the controls .text property. Now i want to do it the right way, and concentrate all validation closer to the data side of things, in this case the data tables. So i decided to do it via the RowChanging event: On my table...

      public override void EndInit()
      {
      base.EndInit();
      customersRowChanging += customersRowChangeEvent;
      }

      public void customersRowChangeEvent(object sender, customersRowChangeEvent e)
      {
      if (e.Row.Name.Length == 0)
      {
      e.Row.SetColumnError("name", "Customer name cannot be empty");
      }
      else
      {
      e.Row.SetColumnError("name", "");
      }
      }

      On my app, in a form the textboxes & other input are bound to a BindingSource, wich in turn is bound to the datatable in the dataset (100% as per visual studio designer). To add a record, I call the BindingSource.AddNew(), and allow the user to enter the data and then on my handler for the "Save" button...

         try
              {
                  Validate();
                  customersBindingSource.EndEdit();
                  customersTableAdapter.Update(samDataSet.customers);
                  // other stuff here, close the form, etc.
              }
              catch(Exception ex)
              {
                  // ... show a message box with the error.
      
              }
      

      Even without handling the rowChange event, This works when a datatable or database constraint is violated, e.g. if the name is NULL or not unique (as specified in the data table constraints), an exception occurs, and the record is not saved. However, Even with the RowChangeEvent implemented as above, if a record with an empty (not null, empty string) name is in the record, no exception is thrown, the datatable gladly accepts the new record, and the table adapter saves it (the MySQL database does not enforce empty string values). Ibe checked, and the handler does get called, and the call to SetColumnError is made, but it doesnt prevent the table from accepting the new row. I read to use this method from here: Validate data in datasets[^] And i was under the impression tha

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I've never had input-validation on a DataTable; usually there is a (generated) validation in the UI, and another one in the database itself. Yes, they partially overlap. If your DataTable HasErrors, you could try to RejectChanges[^].

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      E 1 Reply Last reply
      0
      • L Lost User

        I've never had input-validation on a DataTable; usually there is a (generated) validation in the UI, and another one in the database itself. Yes, they partially overlap. If your DataTable HasErrors, you could try to RejectChanges[^].

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        E Offline
        E Offline
        Ernesto R D
        wrote on last edited by
        #3

        Thanks for the reply Eddy. I just found out whats wrong... This:

        public void customersRowChangeEvent(object sender, customersRowChangeEvent e)
        {
        if (e.Row.Name.Length == 0)
        {
        e.Row.SetColumnError("name", "Customer name cannot be empty");
        throw new System.Exception("Customer Name cannot be empty");
        }
        else
        {
        e.Row.SetColumnError("name", "");
        }
        }

        Does work as intended. The unhandled exception i was getting was a First chance exception. If i hit continue and let the code continue to run, it does get handled properly on my own try / catch statements on the Save button handler i posted above. Now i feel embarassed. i guess im rustier than i tought i was. I completely forgot about first chance exceptions and how the debuger handles them.

        L 1 Reply Last reply
        0
        • E Ernesto R D

          Thanks for the reply Eddy. I just found out whats wrong... This:

          public void customersRowChangeEvent(object sender, customersRowChangeEvent e)
          {
          if (e.Row.Name.Length == 0)
          {
          e.Row.SetColumnError("name", "Customer name cannot be empty");
          throw new System.Exception("Customer Name cannot be empty");
          }
          else
          {
          e.Row.SetColumnError("name", "");
          }
          }

          Does work as intended. The unhandled exception i was getting was a First chance exception. If i hit continue and let the code continue to run, it does get handled properly on my own try / catch statements on the Save button handler i posted above. Now i feel embarassed. i guess im rustier than i tought i was. I completely forgot about first chance exceptions and how the debuger handles them.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Hehe, I recognize those days :D

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups