• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

use a string to define a sqldatasource parameter

I have created the following.
A sqldatasource using a parameter to retrieve data from the database which not older than todays date and display it in a gridview.

Below is the code :

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConn %>"
            SelectCommand="SELECT * FROM [calendar] WHERE ([appDate] > @appDate)">
                <asp:SessionParameter DefaultValue="01/06/2006" Name="appDate" SessionField="isToday"
                    Type="DateTime" />

On the code behind file I have created a DateTime variable called isToday
Dim isToday as DateTime = date.now()
and want to use this variable as below in the sqlDataSource Parameter.
<asp:SessionParameter DefaultValue="01/06/2006" Name="appDate" SessionField="isToday"

Should this work or am I doing this all wrong. I am very new to this so I would need all the code.
thanks vm

  • 2
  • 2
1 Solution
You must save your date value in the browser session not as structure datetime:

session("isToday") = Today
maxgallAuthor Commented:
This is not excactly what I am looking for.
Above I have the sqldatasource to retrieve data from my database. I now need to define a parameter to retrieve data which are
> than todays date.

In asp 1.0  I have just used a string and assigned the value   calendar1.todaysdate and passed this string on to the method to retrieve my data.  How can I do this now with the sqldatasource where the select command is as follows:
SelectCommand="SELECT * FROM [calendar] WHERE ([appDate] > @appDate)">
I used to be able to pass a string to the parameter @appDate.
Is there something like:
Dim myDate as String = date.now()
<asp: StringParameter Name="appDate" String="myDate"

Look at this sample, I hope it will help you:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        SqlDataSource1.SelectParameters(0).DefaultValue = TextBox1.Text
    End Sub


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                <asp:BoundField DataField="dummy" HeaderText="dummy" SortExpression="dummy" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dataConnectionString %>"
            SelectCommand="SELECT [dummy] FROM [dual] WHERE ([dummy] = @dummy)">
                <asp:Parameter DefaultValue="" Name="dummy" Type="String" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Load" />
maxgallAuthor Commented:
yes thanks that is working

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now