locked
SqlDataSource Session Variable RRS feed

  • Question

  • User-1004490012 posted

    I am having a hard time passing data as a session variable from a SqlDataSource on a masterpage to another sqldatasource on the default page.

     

    <code>

    Site2.master
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                                SelectCommand="SELECT Song.Type, Song.Artist, Song.Title, Song.User# From Song"> 
                            </asp:SqlDataSource>
    
    
    Site2.master.cs
    
    protected void Page_Load(object sender, EventArgs e)
        {
    Response.Redirect("CB_Default.aspx", true);
    }
    
    
    Default.aspx
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        
            SelectCommand="SELECT Note.Note#, Note.NoteTitle, Note.NoteText, Note.User# FROM Note WHERE (Note.Note# = @column1)">
        <SelectParameters>
            <asp:SessionParameter Name="column1" SessionField="songNum" />
        </SelectParameters>
    </asp:SqlDataSource>

     

    </code>

    Wednesday, March 27, 2013 1:17 PM

Answers

All replies

  • User-1528094326 posted

    Where are you actually adding songNum to session? I don't see it on your code. From what you posted you just have two SqlDataSources one on the Master and one on the Default page, and the Default is trying to find a non-existing session variable.

    Wednesday, March 27, 2013 4:26 PM
  • User-1004490012 posted

    Sorry. In the Site2.master.cs.

    <code>

    protected void Page_Load(object sender, EventArgs e)
        {
    Session ["songNum"] = SqlDataSource1.ID.Equals("Song#");
    Response.Redirect("CB_Default.aspx", true);
    }

     

    </code>

    Wednesday, March 27, 2013 5:14 PM
  • User-1528094326 posted

    What SQL Data Type is Note.Note#? Right now you are passing it a bool value.

    Wednesday, March 27, 2013 5:23 PM
  • User-1004490012 posted

    integer

    Wednesday, March 27, 2013 5:45 PM
  • User-1528094326 posted

    It's not going to work then becuase you are feeding it a boolean value. Are you just storing 1 or 0 in the database? If so you can do...

    Session["songNum"] = SqlDataSource1.ID.Equals("Song#") ? 1 : 0;



    Wednesday, March 27, 2013 6:28 PM
  • User-1004490012 posted

    No I am not just storing 1 or 0. I need something that would hold any integer.

    Wednesday, March 27, 2013 8:11 PM
  • User-1528094326 posted

    Okay, then you have a couple problems.

    1) You are not doing what you think you are doing with the following code:

    Session ["songNum"] = SqlDataSource1.ID.Equals("Song#");

    That code is going to check and see if the SqlDataSource1's ID field is equal to the text "Song#". I imagine what you are THINKING it will do, is grab some selected Song primary key number from the selected Song table am I right? That will not do that.

    2) The session value you are saving is a Bool value because of the .Equals() method, not an Integer so it will not work with the datasource on the page as SQL will not recognize it.

    I'm guessing what you are attempting to actually do is have a page that loads some Song records, you want the user to click on a Song and then load that data from the Notes table in the Default page using the Song primary key probably "Song#" that was stored in Session from the previous page. Am I guessing this right?

    Wednesday, March 27, 2013 8:30 PM
  • User-1004490012 posted

    Yes, except the songNum session variable is being passed from the masterpage to the default.

    Wednesday, March 27, 2013 9:18 PM
  • User-1528094326 posted

    The Session is actually a global variable to your entire HttpContext. When someone goes to your webpage, it hits IIS. IIS creates a brand new Session for that browser connecting to your web server, and that Session is what you are saving against. Passing it to the Default page would be using QueryStrings, Session is available to any page period. That songNum Session variable will remain in Session until that users Session expires, even if they are looking at other parts of the site, just an FYI in case they try to automatically load up that Default page WITHOUT having chosen anything on the Master page. If you don't want them to be able to do that, then you will want to make sure and clear that Session variable.

    How does the user choose a Song on the masterpage? Do you have a GridView or DataList or?

    Wednesday, March 27, 2013 10:33 PM
  • User-1004490012 posted

    The song is being picked by me on the default so there is no fear of the user deviating. I just dont want to pass it using a querystring because of security reasons.

    Thursday, March 28, 2013 1:45 AM
  • User1938476581 posted

    Hi,

    You can access the sqldatasource fields like this

    DataView dvSql = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        foreach (DataRowView drvSql in dvSql)
        {
            Label1.Text = drvSql["FirstName"].ToString();
        }
    

    For detailed information, you can refer here

    http://www.mikesdotnetting.com/Article/64/Bind-Data-From-a-SqlDataSource-to-a-Label

    http://www.mikesdotnetting.com/Article/45/Programmatically-accessing-data-from-DataSource-controls

    Hope it can help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 28, 2013 2:04 AM