I need a help with a sql exceptin of inserting duplicate key into database.
I cause the exception deliberatly to not insert the duplicit record into database.
In code behind I have this code:
Protected Sub SqlDataSource3_Inserting(sender As Object, e As SqlDataSourceCommandEventArgs)
Try
Catch ex As SqlException
End Try
End Sub
But I don’t know what to write into the Try...Catch block. I just want to display a MessageBox with text You are already a member of this group. insted of error page.
This is my server code:
<asp:SqlDataSource ID="SqlDataSource3" OnInserted="SqlDataSource3_Inserted" OnInserting="SqlDataSource3_Inserting" runat="server" ConnectionString="<%$ ConnectionStrings:MotWebConnectionString %>"
InsertCommand="INSERT INTO [GroupMembers] ([UserName], [GroupId]) VALUES (@UserName, @GroupId)" >
<asp:SqlDataSource ID="SqlDataSource3" OnInserted="SqlDataSource3_Inserted" OnInserting="SqlDataSource3_Inserting" runat="server" ConnectionString="<%$ ConnectionStrings:MotWebConnectionString %>"
InsertCommand="INSERT INTO [GroupMembers] ([UserName], [GroupId]) VALUES (@UserName, @GroupId)" >
The exception will not be raised in the event handler; instead you should do something like: