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. DataTable PrimaryKey problem

DataTable PrimaryKey problem

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasehelptutorialquestion
8 Posts 3 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.
  • C Offline
    C Offline
    calhuskerfan
    wrote on last edited by
    #1

    I have a database which has a unique primary key defined key on two columns, in the example below the columns are type and data.

    type

    value

    data

    9

    0

    'ABC'

    9

    0

    'ABC '

    When I read in a datatable and then set the PrimaryKey collection with the designated columns I get an exception saying that 'These columns don't currently have unique values'. So, to me it appears that ADO is ignoring the spaces with respect to determining uniqueness (although the whitespace appears in the rows collection). I started looking into the DataTable and DataColumn object to see if there was anything that jumped out, but nothing so far. Can anyone confirm this behavior and/or offer any assistance? Thanks!

    L 1 Reply Last reply
    0
    • C calhuskerfan

      I have a database which has a unique primary key defined key on two columns, in the example below the columns are type and data.

      type

      value

      data

      9

      0

      'ABC'

      9

      0

      'ABC '

      When I read in a datatable and then set the PrimaryKey collection with the designated columns I get an exception saying that 'These columns don't currently have unique values'. So, to me it appears that ADO is ignoring the spaces with respect to determining uniqueness (although the whitespace appears in the rows collection). I started looking into the DataTable and DataColumn object to see if there was anything that jumped out, but nothing so far. Can anyone confirm this behavior and/or offer any assistance? Thanks!

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      calhuskerfan wrote:

      I have a database

      calhuskerfan wrote:

      So, to me it appears that ADO is

      ADO is NOT a database. If you are setting up the keys and constraints in the database it is the database product that will contain your answer not ADO.

      led mike

      C 1 Reply Last reply
      0
      • L led mike

        calhuskerfan wrote:

        I have a database

        calhuskerfan wrote:

        So, to me it appears that ADO is

        ADO is NOT a database. If you are setting up the keys and constraints in the database it is the database product that will contain your answer not ADO.

        led mike

        C Offline
        C Offline
        calhuskerfan
        wrote on last edited by
        #3

        The constraints in the database are fine. They allow the described scenario where 'ABC' and 'ABC ' are considered unique. When I read, with ADO, from the database into a datatable object ADO does not bring along the primary key identified in the database. So it allows at this point the two above rows to exist. When I set the PrimaryKey property on the DataTable object (after the rows have been read) to include the column containing the above data I get the constraint exception. Edit - The exception is actually an ArgumentException stating that the values are not unique.

        L 1 Reply Last reply
        0
        • C calhuskerfan

          The constraints in the database are fine. They allow the described scenario where 'ABC' and 'ABC ' are considered unique. When I read, with ADO, from the database into a datatable object ADO does not bring along the primary key identified in the database. So it allows at this point the two above rows to exist. When I set the PrimaryKey property on the DataTable object (after the rows have been read) to include the column containing the above data I get the constraint exception. Edit - The exception is actually an ArgumentException stating that the values are not unique.

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          calhuskerfan wrote:

          The constraints in the database are fine. They allow the described scenario where 'ABC' and 'ABC ' are considered unique.

          What database are you using? I cannot reproduce that behavior using SQL Express 2005.

          led mike

          C 2 Replies Last reply
          0
          • L led mike

            calhuskerfan wrote:

            The constraints in the database are fine. They allow the described scenario where 'ABC' and 'ABC ' are considered unique.

            What database are you using? I cannot reproduce that behavior using SQL Express 2005.

            led mike

            C Offline
            C Offline
            calhuskerfan
            wrote on last edited by
            #5

            Sybase SQL Anywhere 10. Thanks.

            1 Reply Last reply
            0
            • L led mike

              calhuskerfan wrote:

              The constraints in the database are fine. They allow the described scenario where 'ABC' and 'ABC ' are considered unique.

              What database are you using? I cannot reproduce that behavior using SQL Express 2005.

              led mike

              C Offline
              C Offline
              calhuskerfan
              wrote on last edited by
              #6

              Here is a very quick (and sloppy) example that reproduces the behavior.

              using System;
              using System.Collections.Generic;
              using System.Text;
              using System.Windows.Forms;
              using System.Data;

              namespace ConsoleApplication2
              {
              class Program
              {
              static void Main(string[] args)
              {
              Test1();
              }

                  public static void Test1()
                  {
                      DataTable dt1 = new DataTable();
                      //
                      dt1.Columns.Add("col0");
                      dt1.Columns.Add("col1");
                      dt1.Columns.Add("col2");
                      //
                      dt1.Columns\[0\].DataType = typeof(Int32);
                      dt1.Columns\[1\].DataType = typeof(String);
                      dt1.Columns\[2\].DataType = typeof(String);
                      //
                      DataRow dr = dt1.NewRow();
                      dr\["col0"\] = 1;
                      dr\["col1"\] = "Column 2";
                      dr\["col2"\] = "Column 3";
                      dt1.Rows.Add(dr);
                      //
                      dr = dt1.NewRow();
                      dr\["col0"\] = 1;
                      dr\["col1"\] = "Column 2";
                      dr\["col2"\] = "Column 3   ";
                      dt1.Rows.Add(dr);
                      //
                      try
                      {
                          dt1.PrimaryKey = new DataColumn\[\] { dt1.Columns\[0\], dt1.Columns\[2\] };
                      }
                      catch (ArgumentException ae)
                      {
                          MessageBox.Show(ae.Message);
                      }
                  }
              }
              

              }

              L W 2 Replies Last reply
              0
              • C calhuskerfan

                Here is a very quick (and sloppy) example that reproduces the behavior.

                using System;
                using System.Collections.Generic;
                using System.Text;
                using System.Windows.Forms;
                using System.Data;

                namespace ConsoleApplication2
                {
                class Program
                {
                static void Main(string[] args)
                {
                Test1();
                }

                    public static void Test1()
                    {
                        DataTable dt1 = new DataTable();
                        //
                        dt1.Columns.Add("col0");
                        dt1.Columns.Add("col1");
                        dt1.Columns.Add("col2");
                        //
                        dt1.Columns\[0\].DataType = typeof(Int32);
                        dt1.Columns\[1\].DataType = typeof(String);
                        dt1.Columns\[2\].DataType = typeof(String);
                        //
                        DataRow dr = dt1.NewRow();
                        dr\["col0"\] = 1;
                        dr\["col1"\] = "Column 2";
                        dr\["col2"\] = "Column 3";
                        dt1.Rows.Add(dr);
                        //
                        dr = dt1.NewRow();
                        dr\["col0"\] = 1;
                        dr\["col1"\] = "Column 2";
                        dr\["col2"\] = "Column 3   ";
                        dt1.Rows.Add(dr);
                        //
                        try
                        {
                            dt1.PrimaryKey = new DataColumn\[\] { dt1.Columns\[0\], dt1.Columns\[2\] };
                        }
                        catch (ArgumentException ae)
                        {
                            MessageBox.Show(ae.Message);
                        }
                    }
                }
                

                }

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                calhuskerfan wrote:

                Here is a very quick (and sloppy) example that reproduces the behavior.

                You didn't understand my post. Using SQL Express 2005 with a Table1 designed as your example with the two column primary key constraint. One row in the table as follows:

                Type Value Data


                1 2 ABC

                Executing the following SQL

                insert into Table1 (Type, Value, Data) VALUES(1, 3, 'ABC ')

                Produces this error: Cannot insert duplicate key row in object 'dbo.Table1' with unique index 'IX_Table1'.

                led mike

                1 Reply Last reply
                0
                • C calhuskerfan

                  Here is a very quick (and sloppy) example that reproduces the behavior.

                  using System;
                  using System.Collections.Generic;
                  using System.Text;
                  using System.Windows.Forms;
                  using System.Data;

                  namespace ConsoleApplication2
                  {
                  class Program
                  {
                  static void Main(string[] args)
                  {
                  Test1();
                  }

                      public static void Test1()
                      {
                          DataTable dt1 = new DataTable();
                          //
                          dt1.Columns.Add("col0");
                          dt1.Columns.Add("col1");
                          dt1.Columns.Add("col2");
                          //
                          dt1.Columns\[0\].DataType = typeof(Int32);
                          dt1.Columns\[1\].DataType = typeof(String);
                          dt1.Columns\[2\].DataType = typeof(String);
                          //
                          DataRow dr = dt1.NewRow();
                          dr\["col0"\] = 1;
                          dr\["col1"\] = "Column 2";
                          dr\["col2"\] = "Column 3";
                          dt1.Rows.Add(dr);
                          //
                          dr = dt1.NewRow();
                          dr\["col0"\] = 1;
                          dr\["col1"\] = "Column 2";
                          dr\["col2"\] = "Column 3   ";
                          dt1.Rows.Add(dr);
                          //
                          try
                          {
                              dt1.PrimaryKey = new DataColumn\[\] { dt1.Columns\[0\], dt1.Columns\[2\] };
                          }
                          catch (ArgumentException ae)
                          {
                              MessageBox.Show(ae.Message);
                          }
                      }
                  }
                  

                  }

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #8

                  I tried your test case and ran into the same problem. Seems that when the values are compared, the trailing spaces are removed (or added until the length is the same). Actually the same effect can happen in SQL Server when varchar data type is used so this may be by design. Quickly cannot think anything else than replacing trailing spaces with a special character that can be later removed (like ascii 255).

                  The need to optimize rises from a bad design. My articles[^]

                  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