Unable to use session variables in my queries

Last post 11-16-2009 9:55 AM by bbcompent1. 7 replies.

Sort Posts:

  • Unable to use session variables in my queries

    11-13-2009, 3:43 PM
    • Member
      32 point Member
    • bbcompent1
    • Member since 07-30-2008, 9:28 AM
    • Rochester, NY
    • Posts 127

    Hello all, I know I'm not crazy but this application is getting me there. First off, I am capturing the aspnet_db userID and placing it into a session variable so I can refer to it in my queries. However, what's happening is when I try accessing the value, I get errors:

    The server tag is not well formed.

    Literal expressions like '<%$ ConnectionStrings:MySQLCN %>' are not allowed. Use <asp:Literal runat="server" Text="<%$ ConnectionStrings:MySQLCN%>" /> instead.

    Now this is where the problem comes in. If I execute my query with a static value, all is right with the world. When I try to bring my session variable UserID into the mix, it gives me the errors above.

    Visual Element:
    <asp:SqlDataSource ID="MySQLCustData" runat="server"
    ConnectionString="<%$ ConnectionStrings:MySQLCN %>"
    ProviderName="<%$ ConnectionStrings:MySQLCN.ProviderName %>"
                    
    SelectCommand="SELECT egw_addressbook.n_fn, egw_addressbook.adr_one_street, egw_addressbook.adr_one_street2, egw_addressbook.adr_one_locality, egw_addressbook.adr_one_region, egw_addressbook.adr_one_postalcode FROM egw_addressbook INNER JOIN ewise_device ON egw_addressbook.aspnet_id = ewise_device.aspnet_id WHERE (ewise_device.aspnet_id = <%Session["UserID"])">
    </asp:SqlDataSource>

    Code Behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Security;

    public partial class home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           Session["UserID"] = Membership.GetUser().ProviderUserKey;
        }
    }

    Please help and thank you.

    Web Application Engineer
  • Re: Unable to use session variables in my queries

    11-13-2009, 4:26 PM
    • Contributor
      3,268 point Contributor
    • Rajneesh Verma
    • Member since 07-15-2008, 10:22 AM
    • Lucknow, U.P. INDIA
    • Posts 745

    Hi,

    Use this

    bbcompent1:
    SelectCommand="SELECT egw_addressbook.n_fn, egw_addressbook.adr_one_street, egw_addressbook.adr_one_street2, egw_addressbook.adr_one_locality, egw_addressbook.adr_one_region, egw_addressbook.adr_one_postalcode FROM egw_addressbook INNER JOIN ewise_device ON egw_addressbook.aspnet_id = ewise_device.aspnet_id WHERE (ewise_device.aspnet_id = <%Session["UserID"].ToString())"

    New value is in Bold try this...


    RajneesH Verma
    Sr. Software Developer
    09839040266
    Technology Specialist at www.innovateonindia.com

    Please remember to click "Mark as Answer" on the post that helps you and close that thread which fulfill your requirement.
  • Re: Unable to use session variables in my queries

    11-13-2009, 4:29 PM
    • Member
      204 point Member
    • MinaSamy
    • Member since 12-17-2008, 8:04 AM
    • Posts 149

    Hi

    try using

    WHERE (ewise_device.aspnet_id = '<%Session["UserID"])' "> 

    put the quotes quotation marks

    Mina Samy
    --------------------------------------------------
    http://www.learn-mvc.blogspot.com
  • Re: Unable to use session variables in my queries

    11-13-2009, 4:50 PM
    • All-Star
      24,814 point All-Star
    • budugu
    • Member since 01-12-2006, 7:15 PM
    • North Carolina
    • Posts 3,701

    First of all you are missing >

    WHERE(ewise_device.aspnet_id = <%Session["UserID"])%>">

    Another way of doing this by using SessionParameter, which sets the name of the session variable that the parameter binds to. Try like this..

        <asp:SqlDataSource ID="MySQLCustData" runat="server" ConnectionString="<%$ ConnectionStrings:MySQLCN %>"
            ProviderName="<%$ ConnectionStrings:MySQLCN.ProviderName %>" 
            SelectCommand="SELECT egw_addressbook.n_fn, egw_addressbook.adr_one_street, egw_addressbook.adr_one_street2, egw_addressbook.adr_one_locality, egw_addressbook.adr_one_region, egw_addressbook.adr_one_postalcode FROM egw_addressbook INNER JOIN ewise_device ON egw_addressbook.aspnet_id = ewise_device.aspnet_id 
            WHERE (ewise_device.aspnet_id = ? ">
                    <SelectParameters>
                <asp:SessionParameter Name="UserID" SessionField="UserID" DefaultValue="YourDefaultValue" />
            </SelectParameters>
        </asp:SqlDataSource>


     

    Vijay Kodali || My Blog


    "Don't be afraid to be wrong; otherwise you'll never be right."
  • Re: Unable to use session variables in my queries

    11-16-2009, 8:47 AM
    • Member
      32 point Member
    • bbcompent1
    • Member since 07-30-2008, 9:28 AM
    • Rochester, NY
    • Posts 127

    budugu:

    1. <asp:SqlDataSource ID="MySQLCustData" runat="server" ConnectionString="<%$ ConnectionStrings:MySQLCN %>"  
    2.     ProviderName="<%$ ConnectionStrings:MySQLCN.ProviderName %>"   
    3.     SelectCommand="SELECT egw_addressbook.n_fn, egw_addressbook.adr_one_street, egw_addressbook.adr_one_street2, egw_addressbook.adr_one_locality, egw_addressbook.adr_one_region, egw_addressbook.adr_one_postalcode FROM egw_addressbook INNER JOIN ewise_device ON egw_addressbook.aspnet_id = ewise_device.aspnet_id   
    4.     WHERE (ewise_device.aspnet_id = ? ">  
    5.             <SelectParameters>  
    6.         <asp:SessionParameter Name="UserID" SessionField="UserID" DefaultValue="YourDefaultValue" />  
    7.     </SelectParameters>  
    8. </asp:SqlDataSource>  

    Well, it makes sense to me but when I try building the page, I get (using MySQL):

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line 2
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line 2

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line 2] MySql.Data.MySqlClient.MySqlStream.ReadPacket() +216 MySql.Data.MySqlClient.NativeDriver.ReadResult() +91 MySql.Data.MySqlClient.ResultSet.NextResult() +429 MySql.Data.MySqlClient.MySqlDataReader.NextResult() +400 MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) +998 MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +4 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +130 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +287 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +92 System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1297 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73 System.Web.UI.WebControls.FormView.DataBind() +4 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82 System.Web.UI.WebControls.FormView.EnsureDataBound() +166 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72 System.Web.UI.Control.EnsureChildControls() +87 System.Web.UI.Control.PreRenderRecursiveInternal() +44 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Control.PreRenderRecursiveInternal() +171 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842


    Web Application Engineer
  • Re: Unable to use session variables in my queries

    11-16-2009, 8:53 AM

    in the select statement the where clause is not ended with ')' close the paranthesis.. 

  • Re: Unable to use session variables in my queries

    11-16-2009, 9:25 AM
    • Member
      32 point Member
    • bbcompent1
    • Member since 07-30-2008, 9:28 AM
    • Rochester, NY
    • Posts 127

    The source you mention was copied from another posting. Intellitype caught that one but I still have the error. I used a vanilla page and got the same error. Here's the code:

            <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MySQLCN %>" 
                ProviderName="<%$ ConnectionStrings:MySQLCN.ProviderName %>" 
                SelectCommand="SELECT n_fn, adr_one_street, adr_one_street2, adr_one_locality, adr_one_region, adr_one_postalcode, aspnet_id FROM egw_addressbook WHERE (aspnet_id = ?)">
                <SelectParameters>
                    <asp:SessionParameter Name="aspnet_id" SessionField="UserID" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>

            <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySQLCN %>" 
                ProviderName="<%$ ConnectionStrings:MySQLCN.ProviderName %>" 
                SelectCommand="SELECT n_fn, adr_one_street, adr_one_street2, adr_one_locality, adr_one_region, adr_one_postalcode, aspnet_id FROM addressbook WHERE (aspnet_id = ?)">
                <SelectParameters>
                    <asp:SessionParameter Name="aspnet_id" SessionField="UserID" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line 1

    Web Application Engineer
  • Re: Unable to use session variables in my queries

    11-16-2009, 9:55 AM
    Answer
    • Member
      32 point Member
    • bbcompent1
    • Member since 07-30-2008, 9:28 AM
    • Rochester, NY
    • Posts 127

    I figured out that I missed putting the name of the parameter after the (aspnet_id = ?) so the entire query clause should read:


    <asp:SqlDataSource ID="MySQLCustData" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:MySQLCN %>" ProviderName="<%$ ConnectionStrings:MySQLCN.ProviderName %>" 
                    
                    
                    SelectCommand="SELECT egw_addressbook.n_fn, egw_addressbook.adr_one_street, egw_addressbook.adr_one_street2, egw_addressbook.adr_one_locality, egw_addressbook.adr_one_region, egw_addressbook.adr_one_postalcode FROM egw_addressbook INNER JOIN ewise_device ON egw_addressbook.aspnet_id = ewise_device.aspnet_id WHERE (ewise_device.aspnet_id =  ?aspnet_id)">
                <SelectParameters>
                <asp:SessionParameter DbType="Int32" Name="aspnet_id" SessionField="UserID" />
                </SelectParameters>
                </asp:SqlDataSource>   

    <asp:SqlDataSource ID="MySQLCustData" runat="server" ConnectionString="<%$ ConnectionStrings:MySQLCN %>" ProviderName="<%$ ConnectionStrings:MySQLCN.ProviderName %>" 

    SelectCommand="SELECT egw_addressbook.n_fn, egw_addressbook.adr_one_street, egw_addressbook.adr_one_street2, egw_addressbook.adr_one_locality, egw_addressbook.adr_one_region, egw_addressbook.adr_one_postalcode FROM egw_addressbook INNER JOIN ewise_device ON egw_addressbook.aspnet_id = ewise_device.aspnet_id WHERE (ewise_device.aspnet_id =  ?aspnet_id)">          
    <SelectParameters>
                <asp:SessionParameter DbType="Int32" Name="aspnet_id" SessionField="UserID" />
    </SelectParameters>
    </asp:SqlDataSource> 

    Of course, declare the session variable UserID in code behind like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    using System.Text;

    public partial class home : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

           Session["UserID"] = Membership.GetUser().ProviderUserKey;

        }

    }

    Web Application Engineer
Page 1 of 1 (8 items)