Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2007, 06:22 PM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default GridView Hidden DateTime Column date conversion

Hi,

I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD (create, update, delete) operations. The Table has three columns, one of which - "Modified" of type DateTime - is used to check the concurrency of the row for edits. This column is hidden on the Grid since it should not be edited by the user. In order to access the value of this field, I have created a template column and assigned the "Modified" column to it. The Update Stored Procedure requires the original value of this field to do the cocurrency checks, the last modified datetime. Everything seems to work fine except that the datetime value that is retrieved from the table and held in the template column - as a text field - does not match the "Modified" column in the table when the Update stored procedure receives it. Obviously, when I store the "Modified" column value in the template field's text property, somehow it looses the information partially. In other words, may be, it is dropping the milli seconds. How can I make these two values compare properly. I am pasting my page code, the table structure and the Update stored procedure here. Can somebody please help me figure this one out.

Babu.



Table:
CREATE TABLE [dbo].[ProperNames](
    [PKId] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
    [FirstName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_ProperNames_FirstName] DEFAULT ('Unknown'),
    [LastName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF_ProperNames_LastName] DEFAULT ('Unknown'),
    [Modified] [datetime] NOT NULL CONSTRAINT [DF_ProperNames_Modified] DEFAULT (getdate()),
 CONSTRAINT [PK_ProperNames] PRIMARY KEY CLUSTERED
(
    [PKId] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]



ALTER PROCEDURE [dbo].[ProperNamesUpdate]
(
    @PKId numeric output,
    @FirstName [varchar](50),
    @LastName [varchar](50),
    @Modified [varchar](50)
)
AS
UPDATE ProperNames SET FirstName=@FirstName, LastName=@LastName,Modified=getdate()
WHERE PKId = @PKId and modified = @Modified



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         &nbsp;<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="PKId"
             DataSourceID="SqlDataSource1">
             <Columns>
                 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                 <asp:BoundField DataField="PKId" HeaderText="PKId" InsertVisible="False" ReadOnly="True"
                     SortExpression="PKId" />
                 <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                 <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                 <asp:BoundField DataField="Modified" HeaderText="Modified" SortExpression="Modified"
                     Visible="False" />
                 <asp:TemplateField Visible="False">
                 <ItemTemplate>
                    <asp:Label ID="lblModified" runat="server" Text='<%# Eval("Modified") %>' />
                 </ItemTemplate>
                 </asp:TemplateField>
             </Columns>
         </asp:GridView>
     </div>
         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DataNozzleDevConnectionString %>"
             DeleteCommand="ProperNamesDelete" DeleteCommandType="StoredProcedure" InsertCommand="ProperNamesInsert"
             InsertCommandType="StoredProcedure" SelectCommand="ProperNamesSelectAll" SelectCommandType="StoredProcedure"
             UpdateCommand="ProperNamesUpdate" UpdateCommandType="StoredProcedure" OnUpdating="SqlDataSource1_Updating">
             <DeleteParameters>
                 <asp:Parameter Name="PKId" Type="Decimal" />
             </DeleteParameters>
             <UpdateParameters>
                 <asp:Parameter Direction="InputOutput" Name="PKId" Type="Decimal" />
                 <asp:Parameter Name="FirstName" Type="String" />
                 <asp:Parameter Name="LastName" Type="String" />
                 <asp:Parameter Name="Modified" Type="String" />
             </UpdateParameters>
             <InsertParameters>
                 <asp:Parameter Direction="InputOutput" Name="PKId" Type="Decimal" />
                 <asp:Parameter Name="FirstName" Type="String" />
                 <asp:Parameter Name="LastName" Type="String" />
             </InsertParameters>
         </asp:SqlDataSource>
    </form>
</body>
</html>



protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    GridViewRow gr = GridView1.Rows[GridView1.EditIndex];
    DateTime modified = Convert.ToDateTime(((Label)gr.FindControl("lblModi fied")).Text);
    e.Command.Parameters["@Modified"].Value = modified;
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Default Value for DATETIME column happygv MySQL 5 August 7th, 2009 04:14 PM
Get GridView Cell Value Based on GridView Column stublair C# 2008 aka C# 3.0 0 September 4th, 2008 08:30 AM
GridView hidden field access bmains ASP.NET 2.0 Basics 2 September 21st, 2006 01:54 AM
Crystal Report DateTime conversion to hours twangsameteegoon Access 2 June 12th, 2005 11:28 AM
Accessing hidden column data badgolfer HTML Code Clinic 0 July 28th, 2004 07:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.