Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
[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:

XML
<%@ 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:

C#
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:" ;
    }
}
Posted
Comments
rkthiyagarajan 20-Sep-11 22:08pm    
okay, Whats your question?
Nostromo76 21-Sep-11 0:28am    
How do I get at one of those values returned by the query - [EnrollmentCode], [Role] - in order to display it in a label or use it with Roles.AddUserToRole?

i.e. if I want lblResult.Text to equal the Role returned by the query, how do I do that?

1 solution

Hi,

try this code in that event

C#
        args.IsValid = false;
string eroll;
        foreach (DataRowView drv in SqlDataSourceEnrollCodes.Select(DataSourceSelectArguments.Empty))
        {
            if (drv["EnrollmentCode"].ToString() == args.Value)
            {
                args.IsValid = true;
eroll=drv["Role"].ToString();
                break;
            }
        }
 
        if (args.IsValid)
            lblResult.Text = "The enrollment code you entered was found; the Role is:"+eroll ;


All the Best
 
Share this answer
 
Comments
Nostromo76 21-Sep-11 13:11pm    
Thank you, Muralikrishna, that seems to have worked!

Note that "string eroll;" didn't compile; I had to change it to:

string eroll = string.Empty;

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