www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to extract value returned from query, assign it to user as Roles.AddUserToRole?

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    29

    How to extract value returned from query, assign it to user as Roles.AddUserToRole?

    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:

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

    Code:
    using System;
    using System.Collections;
    using 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:" ;
        }
    }

  2. #2
    Join Date
    Oct 2006
    Posts
    29
    Argh, should this have been posted to the .NET forum? Sorry..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles