[Note: this is a more specific version of my question
here, which may have been too vaguely-worded.]
I'm stuck on what might be some simple ASP.NET syntax: I have a page that allows a user to enter a code into a textbox, and then checks that code against a database table to return the values of one specific table row (if the code was correct) or an error message (if the code doesn't exist in the database table).
The page functions without a problem, and I can display the results of the one returned table row in a DetailsView control.
What I want to do, is to display some of the returned data in a Label or Literal control, and also to assign one of the returned string values ("Role") as the logged-in users new role, using Roles.AddUserToRole. So, for example, if the user enters the valid code 12Ab34Cd, the query will return the values:
EnrollmentCode:12Ab34Cd
Role:student
...I want to assign the string "student" as the user's new membership role, but I'm not sure how to do this. I'm using the standard ASP.NET membership provider, and all of the "Role" strings returned by the query are preexisting, valid membership roles that I have already created.
Thanks for any advice.
The aspx page's code is:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_validate.aspx.cs" Inherits="testvalidation_test_validate" %>
<!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">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Test Validate</h2>
Enroll code:
<asp:TextBox
ID="txtEnrollCode"
runat="server" />
<asp:Button
ID="btnValidate"
runat="server"
Text="Go" />
<br />
<asp:RequiredFieldValidator
ID="reqval_txtTeam"
runat="server"
ErrorMessage="You must enter an enrollment code."
ControlToValidate="txtEnrollCode"
Display="Dynamic" />
<br />
<asp:RegularExpressionValidator
ID="revEnrollCode"
runat="server"
ErrorMessage="Your enrollment code must consist of eight characters, with no spaces."
ControlToValidate="txtEnrollCode"
ValidationExpression="[a-zA-Z0-9]{8}">
</asp:RegularExpressionValidator>
<br />
<asp:CustomValidator
ID="custval_txtEnrollCode"
runat="server"
ErrorMessage="The enrollment code you entered cannot be found in our database; please try again."
ControlToValidate="txtEnrollCode"
Display="Dynamic"
OnServerValidate="custval_txtEnrollCode_ServerValidate" />
<asp:Label
ID="lblResult"
runat="server"
EnableViewState="False" />
<asp:SqlDataSource
ID="SqlDataSourceEnrollCodes"
runat="server"
ConnectionString="<%$ ConnectionStrings:NSH_ONLINE_TOLConnectionString %>"
SelectCommand="SELECT [EnrollmentCode], [Role] FROM [enrollmentcodes] WHERE ([EnrollmentCode] = @EnrollmentCode)"
onselecting="SqlDataSourceEnrollCodes_Selecting">
<SelectParameters>
<asp:ControlParameter
ControlID="txtEnrollCode"
Name="EnrollmentCode"
PropertyName="Text"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView
ID="DetailsView1"
runat="server"
Height="50px"
Width="125px"
AutoGenerateRows="False"
DataSourceID="SqlDataSourceEnrollCodes">
<Fields>
<asp:BoundField
DataField="EnrollmentCode"
HeaderText="EnrollmentCode"
SortExpression="EnrollmentCode" />
<asp:BoundField
DataField="Role"
HeaderText="Role"
SortExpression="Role" />
</Fields>
</asp:DetailsView>
</div>
</form>
</body>
</html>
The Codebehind is:
using System;
using System.Collections;
usi
ng System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class testvalidation_test_validate : System.Web.UI.Page
{
protected void custval_txtEnrollCode_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
foreach (DataRowView drv in SqlDataSourceEnrollCodes.Select(DataSourceSelectArguments.Empty))
{
if (drv["EnrollmentCode"].ToString() == args.Value)
{
args.IsValid = true;
break;
}
}
if (args.IsValid)
lblResult.Text = "The enrollment code you entered was found; the code is:" ;
}
}