Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

IsPostBack() to display message from SQL Stored Procedure

Posted on 2009-05-14
9
Medium Priority
?
254 Views
Last Modified: 2012-05-07
I am trying to created a form that will allow a member to enter their member number in the text field and press the enter key to send it to the database for verification of membership.  If the members number is valid it timestamps the time they entered, if the number is invalid it returns a message letting them know it expired.

I have the valid number process working, but not the invalid numbers.  When an invalid number is entered it crashes the page.  In the dump how ever is the error message from the stored procedure that I would like to display on the page and return the focus to the text field for the next entry.

This is what I get back when entering an invalid member number.

Stack Trace:
[SqlException (0x80131904): This badge number is invalid, please see the Membership Desk.]

What am I missing here, thanks in advance.
protected void Page_Load(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (!this.IsPostBack)
        {
            // done only during initialization
            TextBox tbox = (TextBox)frmSignIn.FindControl("txtEnrollNumber");
            Button btn = (Button)frmSignIn.FindControl("btnSubmit");
 
            this.form1.DefaultFocus = tbox.UniqueID;
            this.form1.DefaultButton = btn.UniqueID;
        }
 
        else
        {
            // done only during postback
            Label lblErrMsg = (Label)form1.FindControl("lblErrorMessage");
 
            if (e.Exception != null)
            {
                lblErrMsg.Text = "";
            }
            else
            {
                lblErrMsg.Text = e.Exception.Message;
                e.ExceptionHandled = true;
            } 
        }
 
 
 
 

Open in new window

0
Comment
Question by:DGP_Tech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 8

Expert Comment

by:MrAgile
ID: 24390857
Hi There,

Post a link to the page so we can have a look at it.

Sean
0
 

Author Comment

by:DGP_Tech
ID: 24390867
Unfortunately it is an internal page, but I added the page code here.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SignInForm.aspx.cs" Inherits="SignInForm" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="css/global.css" rel="stylesheet" type="text/css" />
    <title>PRCI - Membership Meeting Sign In</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <span class="pgtitle">Meeting with Tribal Council</span>    
        <div class="datedisplay">
            <script src="scripts/datedisplay.js" type="text/javascript"></script>
        </div>
        
    <asp:FormView ID="frmSignIn" DataKeyNames="EnrollNumber" DataSourceID="srcSignIn" DefaultMode="Insert" runat="server">
    <InsertItemTemplate>
        <asp:RequiredFieldValidator ID="rfvEnrollNumberExist" runat="server" SetFocusOnError="true" ControlToValidate="txtEnrollNumber" Display="Dynamic" CssClass="reqmsg">
		    <div>"Enrollment Number" is a required field.</div>
		</asp:RequiredFieldValidator>
		<asp:Label ID="lblErrorMessage" CssClass="errmsg" runat="server" Text=""></asp:Label>
        <asp:Label ID="lblEnrollNumber" CssClass="txtlabel" Text="Enrollment Number:" AssociatedControlID="txtEnrollNumber" runat="server" /><br />
        <asp:TextBox ID="txtEnrollNumber" Text='<%# Bind("EnrollNumber", "{0}") %>' CssClass="tbox" runat="server" TabIndex="0" AutoPostBack="True" /><br />
        <asp:Button ID="btnSubmit" Text="Submit" CommandName="Insert" CssClass="sbtn" runat="server" TabIndex="1" />
    </InsertItemTemplate>
    </asp:FormView>
 
    <asp:SqlDataSource ID="srcSignIn"
        InsertCommand="sp_SignIn_Insert" InsertCommandType="StoredProcedure"
        ConnectionString="<%$ ConnectionStrings:PerCapitaConnectionString %>"
        runat="server">
        <InsertParameters>
            <asp:Parameter Name="EnrollNumber" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
 
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 2000 total points
ID: 24391076
I am not sure but can we have SqlDataSourceStatusEventArgs in the Page_Load event as is your code suggesting?
I think you may need to change the following line;
Page_Load(object sender, SqlDataSourceStatusEventArgs e)
with
Page_Load(object sender, System.EventArgs e)
For handling exceptions you may need to use some appropriate event of SqlDataSource or Page/Page Controls.
For detail on SqlDataSource, see this link: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24391128
I have some more information for you.
If you are going to use the SqlDataSource for SELECT, INSERT, DELETE, UPDATE you have to put exception handling code in respective events of the SqlDataSource. If you wish to perform only SELECT statement within your SqlDataSource, do the exception handling code within Selected event only. See the code below for more reference, for brevity reasons, the exception handling sample code is written only in Selected event.

	Private Sub srcSignIn_Deleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles srcSignIn.Deleted
 
	End Sub
 
	Private Sub srcSignIn_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles srcSignIn.Inserted
 
	End Sub
 
	Private Sub srcSignIn_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles srcSignIn.Selected
 
		If e.Exception IsNot Nothing Then
			lblMessage.Text = e.Exception.Message
			e.ExceptionHandled = True
 
		Else
			lblMessage.Text = String.Empty
			lblMessage.Visible = False
 
		End If
 
	End Sub
 
	Private Sub srcSignIn_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles srcSignIn.Updating
 
	End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24391141
I just again read your provided code, if that is working for you, try changing this line of your code:
if (e.Exception != null)
to  
if (e.Exception == null)
and see if your requirements are met.
0
 

Author Comment

by:DGP_Tech
ID: 24399508
Moghazali - I tried the second suggestion with the code changing it to ==null, but it did not seem to make a difference.
As for the seperate sub routine, I am using C# and this looks to be in VB (please correct me if I am wrong).  I did try placing it outside fo the page load in: protected void but did not get anywhere with it.  
And now in playing with the code the page is not allowing the enter key to act as a click on the Submit button after entering in the number.
Going backwards here.
0
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24400260
I am assuming you have named your SqlDataSource srcSignIn. In the code behind file, write the following code in the Selected event of srcSingIn object. For this you may have to choose the srcSignIn in top-left drop down within Code Editor and choose Selected from the top-right drop down in Code Editor.
The above is neccessary as I mentioned earlier having SqlDataSourceStatusEventArgs in Page_Load event is incorrect. The right place for coding/handling exceptions for SqlDataSource is its four events i.e. Selected, Deleted, Inserted, and Updated. If you perform any of these operations using the SqlDataSource object, you should write code in respective event to handle exceptions.

if (e.Exception != null) {
  lblMessage.Text = e.Exception.Message;
  e.ExceptionHandled = true;
}
else
{
  lblMessage.Text = string.Empty;
  lblMessage.Visible = false;
}

Open in new window

0
 

Author Comment

by:DGP_Tech
ID: 24400387
Moghazail,  thank you.  I am reading through the information provided in the link and looking over the code and other advice here.  Unfortunatly I am a one man shop and today has just been all but here working on this.  I will keep you posted.
0
 

Author Closing Comment

by:DGP_Tech
ID: 31581731
Thank you very much, that was a lot of information within the link and if did lead to a solution.  Thank you again.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question