Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 311535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:56:01+00:00 2026-05-12T07:56:01+00:00

I am using VSTS 2008 + C# + .Net 3.0. I have two input

  • 0

I am using VSTS 2008 + C# + .Net 3.0. I have two input strings, I think they are different. But the following C# code thinks they are the same, and throws System.Data.ConstraintException, says Column Name is contrained to be unique, but value already exists. Any ideas what is wrong?

Here is my code and my input strings,

Hex view of my input strings,

http://i30.tinypic.com/2anx2b.jpg

Notepad view of my input strings,

http://i30.tinypic.com/2q03hn4.jpg

My code,

    static void Main(string[] args)
    {
        string[] buf = new string[] { "2ch", "2ch" };

        DataTable bulkInserTable = new DataTable("BulkTable");
        DataColumn column = null;
        DataRow row = null;

        column = new DataColumn();
        column.DataType = System.Type.GetType("System.String");
        column.ColumnName = "Name";
        column.ReadOnly = true;
        column.Unique = true;
        bulkInserTable.Columns.Add(column);

        foreach (string item in buf)
        {
            row = bulkInserTable.NewRow();
            row["Name"] = item;
            bulkInserTable.Rows.Add(row);
        }
    }

EDIT 1:

My confusion is, why C# Dictionary thinks they are different, but DataSet thinks they are of the same. Any solution to make the behavior consistent? Here is my code to prove C# Dictionary thinks they are different, the return buf array is of two elements.

            Dictionary<string, bool> dic = new Dictionary<string, bool>();
            foreach (string s in buf)
            {
                dic[s] = true;
            }
            buf = new List<string>(dic.Keys).ToArray(); // we got two strings here, other than one, which proves Dictionary thinks the two strings are different.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:56:02+00:00Added an answer on May 12, 2026 at 7:56 am

    well for a start you need you sample code to be:

    foreach (string item in buf)
    {
        row = bulkInserTable.NewRow();
        row["Name"] = item;
        bulkInserTable.Rows.Add(row);
    }
    

    Though that still exhibits the issue at least it’s for the real reason

    The reason for this is that, when creating a data table the default compare options in effect are:

    this._compareFlags = CompareOptions.IgnoreWidth 
                         CompareOptions.IgnoreKanaType | 
                         CompareOptions.IgnoreCase;
    

    From the docs Ignore Width:

    Indicates that the string comparison must ignore the character width. For example, Japanese katakana characters can be written as full-width or half-width. If this value is selected, the katakana characters written as full-width are considered equal to the same characters written as half-width.

    System.Globalization.CultureInfo.CurrentCulture.CompareInfo.Compare(
        "2ch", "2ch", System.Globalization.CompareOptions.IgnoreWidth);
    

    returns 0, i.e. identical

    I strongly suggest you do consider such values identical or cause further confusion down the line however if you really want to change it:

    //CaseSensitive property uses this under the hood
    internal bool SetCaseSensitiveValue(
        bool isCaseSensitive, bool userSet, bool resetIndexes)
    {
        if (!userSet && (
            this._caseSensitiveUserSet || (this._caseSensitive == isCaseSensitive)))
        {
            return false;
        }
        this._caseSensitive = isCaseSensitive;
        if (isCaseSensitive)
        {
            this._compareFlags = CompareOptions.None;
        }
        else
        {
            this._compareFlags = CompareOptions.IgnoreWidth | 
                                 CompareOptions.IgnoreKanaType | 
                                 CompareOptions.IgnoreCase;
        }
        if (resetIndexes)
        {
            this.ResetIndexes();
            foreach (Constraint constraint in this.Constraints)
            {
                constraint.CheckConstraint();
            }
        }
        return true;
    }
    

    Thus you can ignore case and totally disable the complex comparison options.

    If you want to make a Dictionary with the same behaviour use the following comparer:

    public class DataTableIgnoreCaseComparer : IEqualityComparer<string>
    {
        private readonly System.Globalization.CompareInfo ci =
            System.Globalization.CultureInfo.CurrentCulture.CompareInfo; 
        private const System.Globalization.CompareOptions options = 
            CompareOptions.IgnoreCase | 
            CompareOptions.IgnoreKanaType | 
            CompareOptions.IgnoreWidth;
    
        public DataTableIgnoreCaseComparer() {}
    
        public bool Equals(string a, string b)
        {
            return ci.Compare(a, b, options) == 0;
        }
    
        public int GetHashCode(string s)
        {
            return ci.GetSortKey(s, options).GetHashCode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a couple of variants of a rename command,… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer There is currently no way to build CLS-compliant assemblies from… May 12, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer You might want to look at Google Protocol Buffers or… May 12, 2026 at 12:33 pm

Related Questions

I am using VSTS 2008 + C# + .Net 3.5 to read Excel file
I am using VSTS 2008 + C# + .Net 3.0. I am using self-hosted
I am using VSTS 2008 + C# + .NET 3.0. I am using a
I am using VSTS 2008 + C# + .Net 3.5 + SQL Server 2008
Is there any C# function which could be used to escape and un-escape a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.