I want, when I populate my database, if the REPETITIOUS value, the error appears
I use this code until I see the data in my database and edit, delete, and insert the data into it. but what i want is when i insert data into the database, not REPETITIOUS and if it is REPETITIOUS show me a message box about it.
The code:
private void btnok_Click(object sender, EventArgs e)
{
string id = idTextBox.Text.ToString();
string name = nameTextBox.Text.ToString();
string family = familyTextBox.Text.ToString();
table1BindingSource.EndEdit();
table1TableAdapter.Update(database1DataSet.Table1);
}
private void butins_Click(object sender, EventArgs e)
{
table1BindingSource.AddNew();
}
private void butdelete_Click(object sender, EventArgs e)
{
table1BindingSource.RemoveCurrent();
table1TableAdapter.Update(database1DataSet.Table1);
}
private void butedit_Click(object sender, EventArgs e)
{
table1BindingSource.EndEdit();
table1TableAdapter.Update(database1DataSet.Table1);
}
I am writing this query, but I don’t know how can I use it? I write this in "Database1DataSet.xsd" and fillby () and gatedataby () appear in the Tableadapter.
inquiry:
SELECT id, name, family
FROM Table1
WHERE (id = @id) AND (name = @name) AND (family = @family)
a source to share
I would suggest that you follow Mark's answer.
The exception you catch is a SqlException with error number 2601, it could be something like this:
catch(System.Data.SqlClient.SqlException lExSql)
{
if(lExSql.Number == 2601)
return Result.Duplicate;
else
return Result.Failure;
}
catch
{
return Result.Failure;
}
Also note, there is no need to call TextBox.Text.ToString () because the Text property is the string itself.
a source to share
Well, you could set up a UNIQUE constraint, covering interesting columns (presumably [id]
, [name]
and [family]
) in the database ..., in order to avoid duplication of pretty good (by creating an exception that you can catch and field posts, respectively).
Another approach is to check for existing values first, but unless you are using something like a serializable isolation level, you still have a race condition.
a source to share