I have a datatable in which one of the columns has string values. I want to enforce an unique constraint on that column, but the constraint fails if I have two values that differ only by a space at the end (like "test" and "test "). I don't want this, I want the two values to be considered unique even if the only difference is a space at the end. How can I do this?
try
{
DataTable dt = new DataTable("test");
dt.Columns.Add("nr");
dt.Columns.Add("text");
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] { 1, "test" };
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray = new object[] { 2, "test " };
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray = new object[] { 3, "alabala" };
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray = new object[] { 4, "bbb" };
dt.Rows.Add(dr);
DataSet ds = new DataSet("testds");
ds.Tables.Add(dt);
ds.EnforceConstraints = true;
UniqueConstraint unqUID = new UniqueConstraint(ds.Tables[0].Columns[1], true);
ds.Tables[0].Constraints.Add(unqUID);
int count = ds.Tables[0].Rows.Count;
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}