I have a formview, hooked up to a datasource that calls an insert method. What I want to happen is, in my dataobject, I pass in some parameters and call a few private functions to check the username, another to check the email. They each call a stored procedure
that returns true of false if it returns a result. Here is the code...
[DataObject(true)]
class MemberSqlBuilder
{
[DataObjectMethod(DataObjectMethodType.Insert, true)]
public void CreateMember(string Username,
string Password,
string Email,
string Gender,
string Month,
string Day,
string Year)
{
SqlCommand objCommand = new SqlCommand();
objCommand.CommandText = "spCreateMember";
objCommand.CommandType = CommandType.StoredProcedure;
string DOB = Month + "/" + Day + "/" + Year;
objCommand.Parameters.AddWithValue("@Username", Username);
objCommand.Parameters.AddWithValue("@Password", Password);
objCommand.Parameters.AddWithValue("@Email", Email);
objCommand.Parameters.AddWithValue("@Gender", Gender);
objCommand.Parameters.AddWithValue("@DOB", DOB);
if (!CheckUsername(Username))
throw new Exception("Username already exists.");
if (!CheckEmail(Email))
throw new Exception("The E-mail entered has already been registered.");
int intRowsAffected = DAL.SendData(objCommand);
if (intRowsAffected < 1)
throw new Exception("An error occured during the registration process.");
}
private static bool CheckUsername(string Username)
{
SqlCommand objCommand = new SqlCommand();
objCommand.CommandText = "spCheckUsername";
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.Parameters.AddWithValue("@Username", Username);
using (DataSet ds = DAL.GetDataSet(objCommand))
{
if (ds != null)
return false;
}
return true;
}
private static bool CheckEmail(string Email)
{
SqlCommand objCommand = new SqlCommand();
objCommand.CommandText = "spCheckEmail";
objCommand.CommandType = CommandType.StoredProcedure;
objCommand.Parameters.AddWithValue("@Email", Email);
using (DataSet ds = DAL.GetDataSet(objCommand))
{
if (ds != null)
return false;
}
return true;
}
}
I have a Literal on the formview to display errors. How do I get these thrown errors to display on this Literal control?
Hi, thanks for your reply. Unfortunately I don't have access to the literal from this code... The code shown is in another project called components to which my main project has a reference to. But not vice versa right? So this is where I'm having the issue.
You can handle the ItemInserted event of the FormView. You will find the Exception in the Event Arguments. If you have handled the Exception, don't forget to set the Event Arguments to stop ASP.NET from showing a nasty Exception page.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
sleepingpeac...
Member
107 Points
62 Posts
Formview And Throwing Exceptions
Apr 28, 2011 11:59 PM|LINK
I have a formview, hooked up to a datasource that calls an insert method. What I want to happen is, in my dataobject, I pass in some parameters and call a few private functions to check the username, another to check the email. They each call a stored procedure that returns true of false if it returns a result. Here is the code...
[DataObject(true)] class MemberSqlBuilder { [DataObjectMethod(DataObjectMethodType.Insert, true)] public void CreateMember(string Username, string Password, string Email, string Gender, string Month, string Day, string Year) { SqlCommand objCommand = new SqlCommand(); objCommand.CommandText = "spCreateMember"; objCommand.CommandType = CommandType.StoredProcedure; string DOB = Month + "/" + Day + "/" + Year; objCommand.Parameters.AddWithValue("@Username", Username); objCommand.Parameters.AddWithValue("@Password", Password); objCommand.Parameters.AddWithValue("@Email", Email); objCommand.Parameters.AddWithValue("@Gender", Gender); objCommand.Parameters.AddWithValue("@DOB", DOB); if (!CheckUsername(Username)) throw new Exception("Username already exists."); if (!CheckEmail(Email)) throw new Exception("The E-mail entered has already been registered."); int intRowsAffected = DAL.SendData(objCommand); if (intRowsAffected < 1) throw new Exception("An error occured during the registration process."); } private static bool CheckUsername(string Username) { SqlCommand objCommand = new SqlCommand(); objCommand.CommandText = "spCheckUsername"; objCommand.CommandType = CommandType.StoredProcedure; objCommand.Parameters.AddWithValue("@Username", Username); using (DataSet ds = DAL.GetDataSet(objCommand)) { if (ds != null) return false; } return true; } private static bool CheckEmail(string Email) { SqlCommand objCommand = new SqlCommand(); objCommand.CommandText = "spCheckEmail"; objCommand.CommandType = CommandType.StoredProcedure; objCommand.Parameters.AddWithValue("@Email", Email); using (DataSet ds = DAL.GetDataSet(objCommand)) { if (ds != null) return false; } return true; } }I have a Literal on the formview to display errors. How do I get these thrown errors to display on this Literal control?
mitja.GTI
Star
11157 Points
2094 Posts
Re: Formview And Throwing Exceptions
Apr 29, 2011 02:35 AM|LINK
Hi,
please check this site: Error Handling in ASP.NET Pages and Applications.
quick sample:
try { int intRowsAffected = DAL.SendData(objCommand); } catch (Exception ex) { Literal1.Text = ex.Message; }But i would suggest that you send this error Message to ur email or save to log file and tranfer the user to the error page.
If you're trying to simply display msg if the procedure returns < 1 then u're on the right way...
if (intRowsAffected < 1) { Literal1.Text = "An error occured during the registration process."; }Hope this helps you.
mitja.gti | www.mitjagti.com
sleepingpeac...
Member
107 Points
62 Posts
Re: Formview And Throwing Exceptions
Apr 29, 2011 07:35 PM|LINK
Hi, thanks for your reply. Unfortunately I don't have access to the literal from this code... The code shown is in another project called components to which my main project has a reference to. But not vice versa right? So this is where I'm having the issue.
superguppie
All-Star
48225 Points
8679 Posts
Re: Formview And Throwing Exceptions
May 02, 2011 09:57 AM|LINK
You can handle the ItemInserted event of the FormView. You will find the Exception in the Event Arguments. If you have handled the Exception, don't forget to set the Event Arguments to stop ASP.NET from showing a nasty Exception page.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
mitja.GTI
Star
11157 Points
2094 Posts
Re: Formview And Throwing Exceptions
May 02, 2011 11:11 AM|LINK
Hi,
in your components project, where you're calling CreateMember(...), capture the error...
try { //... CreateMember(...); //... } catch (Exception ex) { Literal1.Text = ex.Message; }mitja.gti | www.mitjagti.com