Solved

How to retreive a date from a hidden value with an SqlDataSource in ASP.NET (VB.NET)

Posted on 2009-07-15
6
509 Views
Last Modified: 2012-05-07
Hi
I am trying to run a query based on the date at which the user visits the page that will populate a datagrid. The datagrid populates through an SqlDataSource preconfigured with the code found in the code window.

Database : SQL2005 latest release
Web page : VB.NET ==> ASPX (pro 2008 edition)

The query uses parameters : @DateReturn and @UID
Parameter Sources : Controls on the webform (Hidden Value fields)

The @DateReturn parameter of the sqlDataSource is mapped to a hidden control on the page  that is set at the PageLoad event to the Date.Now value. I also tried formatting the date with the "#" , and also tried using Date.Today.

@UID gets its value from another hidden value set at the at the PageLoad event as well.

In debugging, both values are correctly there and accessible by code.

When the sqlDataSource.Databind gets fired, I get an arithmatic error caused by an overflow in the @DateReturn field.

The field to compare the date IS a datetime datatype.

SqlDataSource Select Statement :
 
SELECT * FROM tblStock
WHERE fldDateReturn > @DateReturn 
AND
fldUserID = @UID
 
'PageLoad Event
me.dateVal.value = date.now
me.uidVal.value = userid  ' retrieved trough another sub

Open in new window

0
Comment
Question by:Marty_O
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 15

Expert Comment

by:jinal
ID: 24866298
SELECT * FROM tblStock
WHERE fldDateReturn > '@DateReturn'
AND
fldUserID = @UID
0
 
LVL 15

Expert Comment

by:jinal
ID: 24866315
Try to give defalut value in <asp:ControlParameter  ..
of Sql DataSource . Error indicate that date is not supplied.
 
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 24867679
Use a SessionParameter and at the pageload, add the date to the session

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sessionparameter.aspx
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 15

Assisted Solution

by:jinal
jinal earned 250 total points
ID: 24869512

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default13.aspx.vb" Inherits="Default13" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
        </asp:GridView>
        <asp:HiddenField ID="hid" Value="" runat="server" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLEXPRESS08;Initial Catalog=AdventureWorks;Integrated Security=True" SelectCommand="Select * from TestTable Where [date]<= @Date">
            <SelectParameters>
                <asp:ControlParameter ControlID="hid" DbType="Date" PropertyName="Value" Name="Date" />
            </SelectParameters>
        </asp:SqlDataSource>    
    </div>
    
    </form>
</body>
</html>
 
/* Code Behind */
 
Partial Class Default13
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hid.Value = Date.Now
    End Sub
End Class

Open in new window

0
 
LVL 15

Expert Comment

by:jinal
ID: 24869514
In above comment just update connection string and test.
0
 

Author Closing Comment

by:Marty_O
ID: 31604094
Thanks for the input. By reading both solutions, I realized I had missed a crutial step... specify in the control properties that the type of data was a datetime for my hidden value. Since both solutions worked, I awarded equal points. Thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question