I am fairly new to asp's and .Net and I would like to use a session
variable when defining the Select Command for a gridview but it doesnt
seem to like it (see code sample below). Does anyone have any
suggestions?
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$
ConnectionStrings:ProgressConnectionString %>"
SelectCommand= "SELECT [SONo], [SOContact], [SOCustOrder],
[SODate], [SOContractNo], [SOSalesRep] FROM [SalesOrder] WHERE
[SOCustID ] = '" & Session("myAccount") & "' ORDER BY [SONo],
[SODate];">
</asp:SqlDataSource>
Previously before I had populated the grid on the page load which
worked but this wouldnt allow me to allow paging and sorting on the
grid (see below)
<%@ Page Language="VB" MasterPageFile="~/Progress.master"
Title="Untitled Page" %>
<script runat="server">
Dim strConn As String =
ConfigurationManager.ConnectionStrings("ProgressConnectionString").ConnectionString
Dim myReader As SqlDataReader
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Using con As New SqlConnection(strConn)
con.Open()
Dim strSQL As String = "SELECT [SONo], [SOContact],
[SOCustOrder], " _
& "[SODate], [SOContractNo], [SOSalesRep] " _
& "FROM [SalesOrder] " _
& "WHERE [SOCustID ] = '" & CStr(Session("myAccount"))
& "' " _
& "ORDER BY [SONo], [SODate];"
' Create command
Dim myCommand As New SqlCommand(strSQL, con)
myReader = myCommand.ExecuteReader()
If myReader.HasRows Then
Me.CustomerOrdersGrid.DataSource = myReader
Me.CustomerOrdersGrid.DataBind()
End If
End Using
End Sub
</script>
Many thanks in advance
Paul
alvinzc@gmail.com - 26 Oct 2006 03:31 GMT
Hi,
It can be done by specifying the SessionParameter control in the
<SelectParameters> of SqlDataSource. For instance,
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$
ConnectionStrings:ProgressConnectionString %>"
SelectCommand= "SELECT [SONo], [SOContact], [SOCustOrder],
[SODate], [SOContractNo], [SOSalesRep] FROM [SalesOrder] WHERE
[SOCustID ] = @MyAccount ORDER BY [SONo],
[SODate];">
<SelectParameters>
<asp:SessionParameter Name="MyAccount" DefaultValue="myAccount"
....../>
</asp:SqlDataSource>
Hope this helps..
Regards,
Alvin Chooi
Microsoft ASP.NET Enthusiast
http://alvinzc.blogspot.com
alvinzc@gmail.com - 26 Oct 2006 03:35 GMT
Hi, it can be done by specifying the value to the SessionParameter
control in the <SelectParameters> of SqlDataSource. For instance,
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$
ConnectionStrings:ProgressConnectionString %>"
SelectCommand= "SELECT [SONo], [SOContact], [SOCustOrder],
[SODate], [SOContractNo], [SOSalesRep] FROM [SalesOrder] WHERE
[SOCustID ] = @MyAccount ORDER BY [SONo],
[SODate];">
<SelectParameters>
<asp:SessionParameter Name="MyAccount" DefaultValue="myAccount"
......../>
</SelectParameters>
</asp:SqlDataSource>
Hope this helps..
Regards,
Alvin Chooi
Microsoft ASP.NET Enthusiast
http://alvinzc.blogspot.com