Click here to Skip to main content
15,885,630 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! Thank you very much for your nice tutorials on SQL Server at codeproject. Please, I need some help. I'm new in programming with ASP.NET in Microsoft Visual Studio 2008, with the page language being C#.

I made a simple application to register wireless connection users. When a user doesn't instert a primar key, in my case a MAC address, the following error msg is displayed: Violation of PRIMARY KEY constraint 'aaaaaclients_PK'. Cannot insert duplicate key in object 'dbo.clients'. The duplicate key value is (XXXXXXXXX).
The statement has been terminated.

I know why the message is displayed, but I want to customize this and put my own message as the user will not understand what that is. Please help me how to handle this.

The code (part) of the .aspx page for registering clients(where the error occurs) is this:

C#
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:wirelessConnectionString1 %>" InsertCommand="INSERT INTO [clients] ([FirstName], [Surname], [RegNo], [Brand], [MAC], [RegDate], [Expiry], [Remarks]) VALUES (@FirstName,@MAC)" 

ProviderName="<%$ ConnectionStrings:wirelessConnectionString1.ProviderName %>" 
SelectCommand="SELECT [FirstName], [Surname], [MAC] FROM [clients]" 

UpdateCommand="UPDATE [clients] SET [FirstName] = @FirstName, [Surname] = @Surname, WHERE [MAC] = @MAC">

<deleteparameters>
 
<asp:Parameter Name="MAC" Type="String" />

</deleteparameters>
<insertparameters> 

<asp:Parameter Name="FirstName" Type="String" />
 <asp:Parameter Name="Surname" Type="String" />
<asp:Parameter Name="MAC" Type="String" />


</insertparameters>


Thanks in Advance,

Frank.
Posted
Updated 7-Nov-11 2:19am
v2

You can handle the SqlDataSource.Inserting event:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.inserting.aspx[^]

You can use this event to check if the MAC parameter is a null or empty string and handle the situation accordingly before you even go near the database:

C#
private void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e) 
{
    var pkValue = e.Command.Parameters["@MAC"].Value;

    if(string.IsNullOrEmpty(pkValue))
    {
        //Show some error message here assuming you've got a Label lblError in your page:
        lblError.Text = "You must specify a MAC Address";        

        //cancels the insert
        e.Cancel = true;
    }
}
 
Share this answer
 
Comments
thatraja 7-Nov-11 11:02am    
5!
savedlema 9-Nov-11 11:54am    
Thanks jim lahey, but, I see you have provided the solution to take when a user doesn't fill anything in the primary key field, but I handled that in a much simpler way. My problem is when a user tries to enter a primary key that has already been entered into the database i.e entering the same MAC address again. That's where I want help. But, thank you for taking your time. Please give some clues.
Frank.
As we can Handle Application level and session level errors in Global asax page..
and page level errors..
you can use any one of that events..
to display your own Error messege, if you forget to catch any Exceptions
HTML
Server.GetLastError();
//Will retreives the latest Exception that was unhandled 


And you can use this in Application error handler
C#
void Application_Error(object sender, EventArgs e)
       {
           // Code that runs when an unhandled error occurs
       }
 
Share this answer
 
Comments
thatraja 7-Nov-11 11:01am    
Classic ASP way, 5!
savedlema 9-Nov-11 12:17pm    
thank you shashikanth 2011, but, please, where do I put these codes in my page?
You may use SqlDataSource.Updating Event to handle this error.

SqlDataSource.Updating Event


Write a Try-Catch Block in above Event to display your User-Friendly Error Message.

http://msdn.microsoft.com/en-us/library/0yd65esw(v=VS.100).aspx
 
Share this answer
 
Comments
thatraja 7-Nov-11 11:01am    
Right, 5!
RaisKazi 7-Nov-11 11:24am    
yup, Thanks. :)
You need to customize the things in your code.

Creating and Throwing Exceptions (C# Programming Guide)[^]
SqlException Class[^]
And an article in CP
Exception Handling in 3-Tier Architecture[^]
 
Share this answer
 
Comments
savedlema 9-Nov-11 12:19pm    
Thank you. I'm following your links just now..
savedlema 9-Nov-11 14:14pm    
friends,Some more help please.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900