sorry - another newbie. I found a lot of similar FQAs and Solutions, but they do not fit ...

For some reasons I do not use Visual Studio and not Razor. My code example is a single .aspx page (with no code behind). I would like to get the value (=real email address) from a database into a session. Most likely I Need to insert some code into the onload section ...

The Access to the database works and I receive the ID and email in the DataList. But how to get it from the DataList to my email or into a session?

Thanks for Help

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ language="VB" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

<form id="form1" runat="server"> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SQLServer %>" SelectCommand="SELECT [pidcontact], [email] FROM [wwwcontact] WHERE ([pidcontact] = @pidcontact)">
        <asp:SessionParameter DefaultValue="1" Name="pidcontact" SessionField="pid" Type="Int32" />

<asp:DataList id="DataList1" runat="server" DataSourceID="SqlDataSource1">
        pidcontact: <asp:Label id="pidcontactLabel" runat="server" Text='<%# Eval("pidcontact") %>' /><br />
        email: <asp:Label id="emailLabel" runat="server" Text='<%# Eval("email") %>' />
        <% ' Session("email") = ???           I need to get the Eval("email") value in this session  %>
        <%# Session("email") = DataBinder.Eval(Container.DataItem, "email") %>
Pidx Session: <% response.write (Session("pidx")) %><br />
email Session: <% response.write (Session("email")) %><br />


------------------ RESULT:

pidcontact: 1
email: Smith@email.com False
Pidx Session: kk50
email Session:


I'm not sure why you are not using Visual Studio, there is a free version available (Visual Studio Community Edition) and it would make debugging these types of issues a lot easier.

Having messed around with your code a bit, it became apparent that the Container.DataItem is null and hence you are not getting any value back from the Eval statement. I honestly do not know the full reason for this so can't offer help there. However, As you have already assigned the email address to your label, why not just take the value from there:

<% Session("email") = CType(DataList1.Controls(0).FindControl("emailLabel"), Label).Text%>

I would encourage you to get Visual Studio and to also look at alternatives to the style of coding that you are currently doing. Ideally, you do not want code mixed in with your HTML. Either use WebForms with code behind or look at ASP.NET MVC (my preferred option if you are new to ASP.NET as it teaches good principles).


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.