Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 20th, 2011, 05:49 PM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2006
Location: , , .
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
Default 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:


...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>
    <form id="form1" runat="server">
    <h2>Test Validate</h2>
         Enroll code: 
            runat="server" />
            Text="Go" />
         <br />
            ErrorMessage="You must enter an enrollment code."
            Display="Dynamic" />
         <br />
            ErrorMessage="Your enrollment code must consist of eight characters, with no spaces."
         <br />
            ErrorMessage="The enrollment code you entered cannot be found in our database; please try again."
            OnServerValidate="custval_txtEnrollCode_ServerValidate" />
            EnableViewState="False" />
            ConnectionString="<%$ ConnectionStrings:NSH_ONLINE_TOLConnectionString %>"  
            SelectCommand="SELECT [EnrollmentCode], [Role] FROM [enrollmentcodes] WHERE ([EnrollmentCode] = @EnrollmentCode)" 
                    Type="String" />
                    SortExpression="EnrollmentCode" />
                    SortExpression="Role" />
The Codebehind is:

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;

        if (args.IsValid)
            lblResult.Text = "The enrollment code you entered was found; the code is:" ;
  #2 (permalink)  
Old September 21st, 2011, 06:29 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts

Personally, I wouldn't use DetailsView and SqlDataSource controls for this. While you probably could dig into the content of the DetailsView to find the values, it'll become messy quite quickly.

Instead, I would access the database from Code Behind using plain ADO.NET objects (like the SqlConnection and SqlCommand), LINQ to SQL or Entity Framework. This also makes it easier to abstract your logic to separate classes in case you need to reuse it.


Imar Spaanjaars
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Nostromo77 (September 26th, 2011)
  #3 (permalink)  
Old September 26th, 2011, 02:24 PM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Oct 2006
Location: , , .
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts

I will try that - thank you!

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract Query from OLEDB Datasource lavisualstudio BOOK: Professional Microsoft SQL Server 2008 Integration Services ISBN: 978-0-470-24795-2 0 August 4th, 2010 10:19 PM
Need to assign current logged in USER name to the people picker control in ASPX page jagsusa ASP.NET 2.0 Professional 6 December 10th, 2009 12:50 PM
Query about the USER ROLES in INFOPATH anandhienator Infopath 1 May 2nd, 2007 12:51 PM
assign a user to a bug? mrco BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 9 January 26th, 2007 12:23 PM
Accessing User Roles in MS Access through Java sfali Java Databases 0 September 24th, 2003 08:12 AM

All times are GMT -4. The time now is 11:09 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.