Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 9th, 2005, 09:38 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default assigning a ds field value to a session variable

Gee. I thought this would be simple, but it isn't.

I created the dataset on postback. If the dataset isn't empty, I want to stuff a dataset item into a session variable for use on other pages.

I thought I could just write this line of code:

Session("userFirstName") = dsauth_user.FieldValue("firstName")

and be done with it, but no dice.
Can anyone explain to me how to do this? It should be simple, but I've invested a ton of time with no results.
 
Old February 10th, 2005, 06:44 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi
Default

U have to use dsauth_user.Tables[0].Rows[0][0].ToString()

 
Old February 10th, 2005, 01:06 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Santhi,

This didn't work. The dataset should have only one row with three values. I need the first value in the set: "firstName". BTW: I may need to retrieve and store another value in the future, but there should never be more than one row in the dataset.

Thought I should mention, I'm using VB. Is your code using VB syntax?

 
Old February 10th, 2005, 01:51 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

can you please clarify if you want just one value from a row or if you want the whole data set in a session variable.
 
Old February 10th, 2005, 01:57 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

For a single value you can use this (VB Code)

Session("userFirstName") = dsauth_user.Tables(0).Rows(0).Item("firstName")
 
Old February 10th, 2005, 02:00 PM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Vadivel Send a message via Yahoo to Vadivel
Default

You could use ds.Tables["Tablename"].Columns["firstName"] -- C# .. just chk for the syntax in VB

Best Regards
Vadivel

MVP ASP/ASP.NET
http://vadivel.thinkingms.com
 
Old February 10th, 2005, 02:29 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

If you want to put the whole dataset into a session variable then use this code:

Session("sdsauth_user") = dsauth_user

''Further on in your code you need to do this:
Dim ds As New dsauth_user()
    ds = Session("sdsauth_user")
 
Old February 10th, 2005, 06:52 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

All, Thanks for the suggestions. I've included the entire script for my page below. This is a login page. I collect the user name and password. Then on postback I execute a db query to retrieve a dataset containing userFirstName, userName and Password. If the dataset isn't empty, I want to stuff the 'firstName' value from the dataset into a session variable so I can use it on later pages for personalization. A very simple concept that apparently isn't so simple to implement.

BTW: I'm using VB and DreamWeaver for development.

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls" Assembly="DreamweaverCtrls,version=1.0.0.0,publicK eyToken=836f606ede05d46a,culture=neutral" %>

<MM:DataSet
id="dsauth_user"
runat="Server"
IsStoredProcedure="false"
ConnectionString='<%# System.Configuration.ConfigurationSettings.AppSett ings("MM_CONNECTION_STRING_connUserInfo") %>'
DatabaseType='<%# System.Configuration.ConfigurationSettings.AppSett ings("MM_CONNECTION_DATABASETYPE_connUserInfo") %>'
CommandText='<%# "SELECT firstName, userName, Password FROM dbo.UserProfile WHERE (userName = ?) and (Password = ?)" %>'
Expression='<%# IsPostBack %>'
Debug="true"
><Parameters>
  <Parameter Name="@userName" Value='<%# IIf((Request.Form("txtuserName") <> Nothing), Request.Form("txtuserName"), "") %>' Type="VarChar" />
  <Parameter Name="@Password" Value='<%# IIf((Request.Form("txtPassword") <> Nothing), Request.Form("txtPassword"), "") %>' Type="VarChar" />
</Parameters></MM:DataSet>
<MM:PageBind runat="server" PostBackBind="true" />

<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
dim MyName as string
If IsPostback then
If (dsauth_user.RecordCount <> 0) then
'CREATE A COOKIE - build later

'Create a session variable
'Session("userFirstName") = "MUDD" 'this code works!
MyName = Session("userFirstName") = dsauth_user.Tables(0).Rows(0).Item("firstName") 'this code doesn't
Session("userFirstName") = MyName
Response.redirect("Student_Page.aspx")
Else
Response.redirect("Login_Failed.aspx")
'Response.Write ("alert('The system did not recognize your user name and/or password.')")
End If
End If
End Sub

</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>User Authentication DW script</title>
<link href="World.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>The FBC Online </h1>
<form method="POST" name="login" runat="server" >
<asp:ValidationSummary DisplayMode="BulletList" EnableClientScript="true" HeaderText="Error Summary" ID="vsErrors" runat="server" ShowMessageBox="true" ShowSummary="false" />
  <p> Welcome to the FBC Online.&nbsp; We offer assistive and general on-line training courses<em>, </em>both self-paced and instructor-moderated, for Windows-based software products to help you master computer literacy.</p>
  <p>Come learn with us! </p>
  <p>Are you a registered FBC Online user? Use your user name and password to log in below.</p>
  <label for="User Name"></label><p>
    <strong>
    <asp:Label ID="userName" Text="User Name:" AccessKey="U" runat="server" />
</strong> <asp:TextBox Columns="40" ID="txtuserName" runat="server" TextMode="SingleLine" /><asp:RequiredFieldValidator ControlToValidate="txtuserName" Display="None" EnableClientScript="true" ErrorMessage="User Name is required" ID="rfvuserName" runat="server" />
</p>
  <p>
    <strong>
    <asp:Label ID="Password" Text="Password: " AccessKey="P" runat="server" /> </strong>&nbsp;
    <asp:TextBox ID="txtPassword" Columns="40" runat="server" TextMode="Password" /><asp:RequiredFieldValidator ControlToValidate="txtPassword" Display="None" EnableClientScript="true" ErrorMessage="Password is required" ID="rfvPassword" runat="server" />
</p>
  <p><asp:Button ID="btnLogin" runat="server" Text="Log In" CausesValidation="true" /></p>
  <p>If you're new to FBC Online, <a href="Register.aspx" title="Register now! "><strong>Register now! </strong></a></p>

<input name="txtfirstName" type="hidden" value="<%# dsauth_user.FieldValue("firstName", Container) %>">
</form>
</body>
</html>


 
Old February 11th, 2005, 01:19 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

the code I supplied does work, I currently am using it in many projects. Make sure you are getting rows back from your query.
 
Old February 11th, 2005, 01:44 PM
Registered User
 
Join Date: Feb 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yahoo! I just found the answer for my question. It was a DUH.

Null is not a valid value in VB. You have to use Nothing, e.g.:

MyName = dsauth_user.FieldValue("firstName", Nothing)

Now I only have to find code that allows me to call a Javascript function and I'm home free!






Similar Threads
Thread Thread Starter Forum Replies Last Post
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
Assigning value from variable cyberddindia Classic ASP Basics 3 November 7th, 2006 11:57 AM
Assigning Javascript variable to ASP session anaden Classic ASP Basics 2 September 18th, 2004 02:16 AM
Assigning Session Variables morpheus Classic ASP Basics 2 November 21st, 2003 01:55 PM





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