tags:

views:

25

answers:

1

HI.I searched this question inform and I found solution to change column property Unique Index.Now If I try to insert same record cmd.ExecuteNonQuery() gives error that record exist ,but how can use this exception to give user a message that record exist and must enter new one ? I am trying to make some thing like

if(cmd.ExecuteNonQuery() !=true )
{
 MessageBox.Show("User Exists");
}

But I dont know what returns cmd.ExecuteNonQuery() ?

Or I must get records using reader in table and compare them with text in Textfiel?

+1  A: 

Hi,

You can put a try catch block around the block of code you have and then look at the exception type and then instead of using Exception in the catch you can use the explicit exception you are trying to catch for.

try
{
  cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
  ex.Type.ToString(); //this will give you the type of exception.
}

Now once you know what the exception is you can catch only that one and inform the user like the following.

try
{
  cmd.ExecuteNonQuery();
}
catch(RecordExhistsException ex)
{
  MessageBox.Show("Duplicate Record");
}

Enjoy!

Doug
Thks. for quick response ))
Meko