Hi,
I am following along an example from the book <b>Beginning Visual Web
Programming in C#</b>
My environment is this:
IIS 5.1 on XP Pro, I have the checkbox 'use windows integrated
authentication' unchecked, and I am allowing anonymous access.
Prior to building the example that is giving the error, the app would ask
you to login if you weren't already logged in by redirecting you to the login
page.
In the case of the example, for testing purposes you are instructed to enter
a url with a parameter appened to the URL. What is supposed to happen is
you will be redirected to the login page, then once you login you should be
redirected back to the original page that was in the url with the appended
parameter, that parameter being used to pull some data from the db and
display on this page.
I have carefully checked my code, and even used the authors code downloaded
from apress with the same error. I will post the code, but what I am really
looking for is a general guideline how to debug this issue. I am wondering if
this is a bug, or error in their program.
I am new to C# and asp.net, looking at this as a learning opportunity, but
also feeling frustrated.
viewuser.aspx
<code>
<%@ Page language="c#" Codebehind="viewuser.aspx.cs" AutoEventWireup="false"
Inherits="FriendsReunion.viewuser" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>viewuser</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="styles/iestyle.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<TABLE class="TableLines" id="tbLogin" cellSpacing="2" cols="2"
cellPadding="2" border="0">
<TR>
<TD align="right" colSpan="1" rowSpan="1">
<P align="right">name:</P>
</TD>
<TD>
<asp:Label id="lblName" runat="server"
Text='<%# dsUser.Tables[0].Rows[0]["FirstName"] + " " +
dsUser.Tables[0].Rows[0]["LastName"] %>'>
</asp:Label>
</TD>
</TR>
<TR>
<TD>birth date:</TD>
<TD>
<asp:Label id="lblBirth" runat="server"
Text='<%#
DataBinder.Eval(dsUser.Tables[0].Rows[0],"[DateOfBirth]","{0:MMMM dd,
yyyy}")%>'>
</asp:Label>
</TD>
</TR>
<TR>
<TD>
<P align="right">phone number:</P>
</TD>
<TD>
<asp:Label id="lblPhone" runat="server"
Text='<%#
DataBinder.Eval(dsUser.Tables["User"].Rows[0],"[PhoneNumber]") %>' >
</asp:Label>
</TD>
</TR>
<TR>
<TD>
<P align="right">mobile number:</P>
</TD>
<TD>
<asp:Label id="lblMobile" runat="server"
Text='<%# dsUser.Tables["User"].Rows[0]["CellNumber"] %> '>
</asp:Label>
</TD>
</TR>
<TR>
<TD>
<P align="right">address:</P>
</TD>
<TD>
<asp:Label id="lblAddress" runat="server"
Text='<%#
DataBinder.Eval(dsUser.Tables["User"].Rows[0],"[Address]") %> '>
</asp:Label>
</TD>
</TR>
<TR>
<TD>
<P align="right">email:</P>
</TD>
<TD>
<asp:HyperLink id="lnkEmail" runat="server"
NavigateUrl='<%#
DataBinder.Eval(dsUser.Tables["User"].Rows[0],"[email]","mailto:{0}") %> '>
send mail</asp:HyperLink>
</TD>
</TR>
</TABLE>
<P>the user
<asp:Label id="lblPending" runat="server"
Text='<%# GetPending() %>'>
</asp:Label> requests for contact is pending.</P>
<asp:Button id="btnAuthorize" runat="server" Text="authorize
contact"></asp:Button>
</form>
</body>
</HTML>
</code>
viewuser.aspx.cs
<code>
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace FriendsReunion
{
/// <summary>
/// Summary description for viewuser.
/// </summary>
public class viewuser : FriendsBase
{
protected System.Web.UI.WebControls.Label lblName;
protected System.Web.UI.WebControls.Label lblBirth;
protected System.Web.UI.WebControls.Label lblPhone;
protected System.Web.UI.WebControls.Label lblMobile;
protected System.Web.UI.WebControls.Label lblAddress;
protected System.Web.UI.WebControls.Label lblPending;
protected System.Web.UI.WebControls.HyperLink lnkEmail;
protected System.Web.UI.WebControls.Button btnAuthorize;
protected DataSet dsUser;
private void Page_Load(object sender, System.EventArgs e)
{
string userID = Request.QueryString["RequestID"];
//ensure we receive an ID
if (userID == null)
throw new ArgumentException("this page expects a RequestID parameter");
//create the connection and data adaptor
SqlConnection cnFriends = new SqlConnection(
ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
SqlDataAdapter adUser = new SqlDataAdapter(
"SELECT * FROM [User] WHERE UserID=@ID", cnFriends);
adUser.SelectCommand.Parameters.Add("@ID", userID);
//initialize the dataset and fill it with data
dsUser = new DataSet();
adUser.Fill(dsUser, "User");
//finally, bind all controls on the page 'one control to rule.... and in
the darkness
//bind them'. no wait wrong venue!
this.DataBind();
}
protected string GetPending()
{
//create the connection and command to execute
SqlConnection cnFriends = new SqlConnection(
ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
SqlCommand cmd = new SqlCommand(
@"SELECT COUNT(*) FROM Contact
WHERE IsApproved=0 AND DestinationID=@ID",
cnFriends);
cmd.Parameters.Add("@ID", Page.User.Identity.Name);
cnFriends.Open();
try
{
return cmd.ExecuteScalar().ToString();
}
finally
{
cnFriends.Close();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnAuthorize.Click += new
System.EventHandler(this.btnAuthorize_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnAuthorize_Click(object sender, System.EventArgs e)
{
//create the connection and command to execute
SqlConnection cnFriends = new SqlConnection(
ConfigurationSettings.AppSettings["cnFriends.ConnectionString"]);
SqlCommand cmd = new SqlCommand(
@"UPDATE Contact
SET IsApproved=1
WHERE
RequestID=@RequestID AND DestinationID=@DestinationID",
cnFriends);
cmd.Parameters.Add("@RequestID", Request.QueryString["RequestID"]);
cmd.Parameters.Add("@DestinationID", Page.User.Identity.Name);
cnFriends.Open();
try
{
cmd.ExecuteNonQuery();
}
finally
{
cnFriends.Close();
}
//return to the news page
Response.Redirect("news.aspx");
}
}
}
</code>