I am new to C# .NET. I have been trying since 2 days, but I couldn't succeed.
I have created windows application.
I have table which has two unique columns email,phone.
I want to find the column name which throws the unique key constraint exception at the time of insertion of record. I tried,
var ex = new SqlException();
ex.Data["Mesg"] = "Exception in Email";
throw ex;
But it leads to Error "System.Data.SqlClient.SqlException' has no constructors defined "
Is it possible?
Help me.
Asked
Active
Viewed 1,148 times
2

Aniket Kulkarni
- 12,825
- 9
- 67
- 90
-
My advice would be to validate the incoming model, and do not depend on the database throwing back an error. Then you know up front before connecting, and don't have to waste time talking to the DB. Now you can throw a simple `ArgumentException` upfront, and would know exactly what the problem is, as you just did the validation. – asawyer Mar 29 '12 at 13:33
-
You should be aware that the data you're attempting to insert/update may violate *multiple* constraints, and you'll only get violation information about one of them (which one is indeterminate). – Damien_The_Unbeliever Mar 29 '12 at 13:33
1 Answers
2
You want to obtain this information, not produce it. So use the following construct:
try
{
... (access the database here) ...
}
catch (SqlException e)
{
... (look at e.Message)
}

Jirka Hanika
- 13,301
- 3
- 46
- 75