Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends ! I am using a table in sql databse having columns ID, FName and LName. In my application I fetch this table to a dataset. Now I need a column which should be used to make the full name to be unique in a table.
So i am adding one more column called full name which is not presented in database. It will be only on dataset so that the user can't insert 2 persons with the same First and Last Name. If i used this column as expression based then I can't add Unique constraint to this column. otherwise the rows remains null in the dataset when i fetch the data from the database. What should I do? Is there any way to do in the dataset to make the full name of a person to be unique in a dataset?
Posted

A unique constraint is not going to be evaluated until the data is persisted to the database, there is no way to enforce it in the dataset itself. You'll have manually check when adding to the datatable.
 
Share this answer
 
Comments
R.Binu Port Blair 6-Jun-10 16:08pm    
Reason for my vote of 2
Ok but we have to find A SOLUTION
Hi Friends, finally i created a solution for my question but i don't know that is it the best way for my question. The solution is -
fill the dataset from the database. Add one more column to the dataset named fullname. Using foreach loop for the rows of existion data set the value of added column to fullname as shown below -
DataSet ds = GetDataFromDatabase();
DataColumn col = ds.Tables["TableName"].Columns.add("fullName", (typeof) string);

foreach(DataRow row in ds.Rows)
{
	row["fullName"] = row["FName"].ToString()+row["LName"].ToString();
}
col.Unique = true;
return ds;

//On Insert or Update of application
try
{
	DataRow row = ds.Tables["TableName"].NewRow();
	row.BeginEdit();
	row["FName"] = tbFName.Text;
	row["LName"] = tbLName.Text;
	row["fullName"] = tbFName.Text + tbLName.Text;
	row.EndEdit();
 	ds.Tables["TableName"].Rows.Add(row);
}
catch(Exception ex)
{
	if(ex.GetType() == typeof(ConstraintException))
	{
		MessageBox.Show("A person is already exist with the given First Name and Last Name");
	}
}
 
Share this answer
 
v3
Excuse me if I am being a bit thick but how are you going to ensure that there are not two people with the same name.

For example, if it were a club membership database. What do you do if John Smith wants to join but you already have a John Smith, are you going to refuse the second chap membership, just because of his name?
 
Share this answer
 
Comments
R.Binu Port Blair 6-Jun-10 16:09pm    
Reason for my vote of 1
The question is based on the given circumstance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900