Catch Exception with their code

Posted by Vanchi050585 under Regular Expressions on 8/14/2010 | Points: 10 | Views : 1728 | Status : [Member] | Replies : 5
hi all,

pls tell me what are all codes with examples about Catch Exception with their code for customise the exception like
below

catch (SqlException ex)
{
lblErrormsg.Text = ex.Message.ToString();
if (ex.Number.Equals(2627))
{
lblErrormsg.Text = "This Property Name was already Registered. Please select another Property Name";
}

}

Thanks

Thanks
tvn



Responses

Posted by: Raja on: 8/14/2010 [Member] Starter | Points: 25

Up
0
Down
This is what your code is, you can have your try and finally block to complete this

try

{
// perform action
}
catch (SqlException ex)
{
lblErrormsg.Text = ex.Message.ToString();
if (ex.Number.Equals(2627))
{
lblErrormsg.Text = "This Property Name was already Registered. Please select another Property Name";
}
}





Regards,
Raja, USA

Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vanchi050585 on: 8/16/2010 [Member] Starter | Points: 25

Up
0
Down
hello,

first u read all my requirements then u post the answer. ok? Dont write like this without complete read. First u should understand then u post answer, otherwise dont waste others time like this.

Thanks
tvn

Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajumm on: 8/16/2010 [Member] Starter | Points: 25

Up
0
Down
your code is not a better practice to display error message "This Property Name was already Registered" , why don't you check the datatbase if the item already in database or not?
this is logic.
string itemsID=DBHelper.Execute("Select ItemId from database Where ItemName='someItem'");
if(itemsID==string.Empty)
{
//insert in to dataBase
}
else
{
lblErrormsg.Text = "This Property Name was already Registered. Please select another Property Name";
}


Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vanchi050585 on: 8/17/2010 [Member] Starter | Points: 25

Up
0
Down
hi pls post full coding because i did not use dbhelper

Thanks

Thanks
tvn

Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajumm on: 8/17/2010 [Member] Starter | Points: 25

Up
0
Down
  //normally connection string are written in AppSettings in Web.Config file, this is best pratice

// here i pass connection string inline code.
SqlConnection con = new SqlConnection("your connection string");
SqlCommand cmd = new SqlCommand("Select ItemId from database Where ItemName='someItem'", con);
//open sql Connection
con.Open();
string itemID = string.Empty;
//taking checking Item in DataBase
try
{
itemID = Convert.ToString(cmd.ExecuteNonQuery());
}
catch (Exception er)
{
//here you can catch Sql Errors
}
finally {
//closing SQL connection
con.Close(); }
//there is no item with ItemName
if (itemID == string.Empty)
{
//insert to database
}
else
{
lblErrormsg.Text = "This Property Name was already Registered. Please select another Property Name";
}


Vanchi050585, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response