locked
how to format a SQL query string using asp:loginname in the WHERE clause RRS feed

  • Question

  • User421838227 posted

     ASP.NET 2.0/ SQL Server 2005/ Login Controls

    I am a beginner of ASP.NET and has a problem when I do the query.

    Problem:  It did not show any record after the user login.  After user login, it will query the table call match with condition Player_A = user_name ( login username) or Player_B =user_name (login username)

    Does anyone know how to format a SQL query string using asp:loginname
    in the WHERE clause?

    When I clicked the query button on the DESIGN view on microsoft visual studio, it worked fine and display 3 records.

    But it did not display any record when I actually run the program.

    Do I need to store the login name upon login w/in a
    Session variable?

    Can someone give me an example?

    Thanks,

     

    Frank

     

    ######################################################

     <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Member_Default" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MessageContentPlaceHolder" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" Runat="Server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Match_Id" HeaderText="Match_Id" SortExpression="Match_Id" />
                <asp:BoundField DataField="Player_A" HeaderText="Player_A" SortExpression="Player_A" />
                <asp:BoundField DataField="Player_B" HeaderText="Player_B" SortExpression="Player_B" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:masterConnectionString %>"
            SelectCommand="SELECT * FROM Match WHERE (Player_A = @Player_A) OR (Player_B = @Player_B)">
            <SelectParameters>
                <asp:SessionParameter Name="Player_A" SessionField="LOGON_USER" Type="String" />

                <asp:SessionParameter Name="Player_B" SessionField="LOGON_USER" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    </asp:Content>

    Wednesday, December 24, 2008 11:43 AM

Answers

All replies

  • User783383449 posted

    Hi Frankon,

    Have you declared the LOGON_USER session variable?

     

    //Declare the Default Session
    if (Session["LOGON_USER"] == null)
       Session["LOGON_USER"] = "DEFAULT_USER";
    
    //On Login Replace the Session Value with Username
    Session["LOGON_USER"] = "username";
    
    

     

    Hope this helps.

    Wednesday, December 24, 2008 12:32 PM
  • User-1199946673 posted

    Have you declared the LOGON_USER session variable?

    That's one option, another option is to create a custom parameter for a datasource control

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 24, 2008 6:06 PM
  • User421838227 posted

     Hello,  Sorry..I am really a beginner.  Where should I declare the session variable?  on .cs or .aspx

    If on .cs.  How to call the function from the .aspx

    Thanks,

     

    Frankon

     

    Thursday, December 25, 2008 12:06 AM
  • User421838227 posted

     Hi Hans,

     

    The link you entered is very helpful.  I was kind of slow at the beginning and tried to figure it out how to do it.  Now it is working.

     

    Thanks again.

     

    Frankon

    Thursday, December 25, 2008 12:55 PM
  • User983614030 posted

    I tried several of the method described in the forums to do just this. Your suggestion is the only one that would at least try to run. However when I have tried the example you supplied on the blog, using VS 2010 C# and I get the following error when I try to run:

    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
    
    Compiler Error Message: CS0433: The type 'CustomControls.UserParameter' exists in both 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\b7363e53\4bb6a642\assembly\dl3\2923b6a7\84907d1e_e26dce01\LAVS_2010.DLL' and 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\b7363e53\4bb6a642\App_Code.bqucpfar.dll'
    
    Source Error:
    
     
    
    Line 490:
    Line 491:        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    Line 492:        private global::CustomControls.UserParameter @__BuildControl__control11() {
    Line 493:            global::CustomControls.UserParameter @__ctrl;
    Line 494:



    I copied the code to a file named CustomControls.cs and placed that file in App_Code. That folder is the only place the code is placed in my project.

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeBehind="Default.aspx.cs" Inherits="LAVS_2010._Default" %>
    <%@ Register Namespace="CustomControls" TagPrefix="cc1" %>
    



    The only change I made was to add "using System.Web;"

    using System.Web;
    
    namespace CustomControls
    {
        /// <summary>
        /// Summary description for UserParameter
        /// </summary>
        public class UserParameter : System.Web.UI.WebControls.Parameter
        {
            protected override object Evaluate(HttpContext context, System.Web.UI.Control control)
            {
                if (context.User != null && !string.IsNullOrEmpty(context.User.Identity.Name))
                {
                    return context.User.Identity.Name;
                }
    
                return null;
            }
        }
    }
    

    I am new to asp,net so any additional suggestions would be greatly appreciated.

    Thursday, June 20, 2013 2:24 PM