0

I have the sqldatasource in aspx page and in the query I want to use one parameter i.e. coming from session.

Below is my code.Please help me out.

<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename  WHERE id="Here I want to use session variable"" />
1
  • Please edit your question title!!! does it really mean anything? Commented Jul 25, 2012 at 12:45

5 Answers 5

2

Try this

<asp:SqlDataSource runat="server" ID="MySQLData2"
    ConnectionString='<%$ConnectionStrings:ConnectionString %>'
    ProviderName="MySql.Data.MySqlClient"
    SelectCommand="SELECT * FROM tablename  WHERE id=@SessionVar">
  <SelectParameters>
     <asp:SessionParameter Name="SessionVar" SessionField="SessionVariableName" ConvertEmptyStringToNull="true" />
  </SelectParameters>
</asp:SqlDataSource>
Sign up to request clarification or add additional context in comments.

3 Comments

Are you sure, this will work? How does the data source associate @SessionVar with the SessionParameter?
Should SessionVar and SessionVariableName be different?
I think it should be Name="SessionVar" SessionField="SessionVariableName" or you just use ? like mreyeros pointed out in his answer.
1

This MSDN article should get you what you need. Basically you would define your SelectCommand with the parameter placeholder, "?", and then define your SelectParameters collection with an entry for your SessionParameter.

Comments

0

Using parameters is rather simple:

 <asp:SqlDataSource id="Employees" runat="server"
  ConnectionString="<%$ ConnectionStrings:Northwind%>"
  SelectCommand="SELECT LastName FROM Employees WHERE Title = @Title">
  <SelectParameters>
    <asp:ControlParameter Name="Title" 
      ControlID="DropDownList1"
      PropertyName="SelectedValue"/>
  </SelectParameters>
</asp:sqldatasource>

Just replace the value of parameter with your variable:

 <%= Sessiom[variable_name] %>

1 Comment

Thanks John..But this is not working as it is giving me runtime exception that you have problem in query.My query is right as it is running fine when I am running it replacing a constant value with session variable.
0
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=LAZY-PC;Initial Catalog=Test;Integrated Security=True"
        ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [C] WHERE ([C#] = @column1)">
        <SelectParameters>
            <asp:SessionParameter Name="column1" SessionField="id" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

Comments

0

Johnny_D's answer is pretty much spot on, but I'd like to point out that there's a SessionParameter class that you can use for this:

<asp:SqlDataSource runat="server" ID="MySQLData2"
    ConnectionString='<%$ConnectionStrings:ConnectionString %>'
    ProviderName="MySql.Data.MySqlClient"
    SelectCommand="SELECT * FROM tablename WHERE id= ?" />
      <SelectParameters>
          <asp:SessionParameter
            Name="id"
            SessionField="SessionVariableName"
            DefaultValue="0" />
      </SelectParameters>
  </asp:SqlDataSource>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.