G
Guest
Hi,
I have a GridView and a SqlDataSource controls on a page. The SqlDataSource
object uses stored procedures to do the CRUD operations. The DataSource has
three columns one of which - "Modified" of type DateTime - is hidden since it
should not be edited by a user. The system handles the update for this
column. So, I have hidden (Visible=false) this column on the grid. In order
to access the value in this field, I have created a template column and
assigned the "Modified" column to it. The Update Stored Procedure does the
cocurrency checks by looking at the value in the Modified column which holds
the last modified datetime. Everything seems to work fine except that the
datetime value that is held in the template column does not equal the
"Modified" column in the table when the stored procedure compares it.
Obviously, when I store the "Modified" column value in the template field's
text property, somehow it looses the information partly. In other words, may
be, it is dropping the milli seconds. How can I make these two values compare
properly. I am pasting the 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,
@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>
<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="<%$ ConnectionStringsataNozzleDevConnectionString %>"
DeleteCommand="ProperNamesDelete" DeleteCommandType="StoredProcedure"
InsertCommand="ProperNamesInsert"
InsertCommandType="StoredProcedure" SelectCommand="ProperNamesSelectAll"
SelectCommandType="StoredProcedure"
UpdateCommand="ProperNamesUpdate" UpdateCommandType="StoredProcedure"
OnUpdating="SqlDataSource1_Updating">
<DeleteParameters>
<asparameter Name="PKId" Type="Decimal" />
</DeleteParameters>
<UpdateParameters>
<asparameter Direction="InputOutput" Name="PKId" Type="Decimal" />
<asparameter Name="FirstName" Type="String" />
<asparameter Name="LastName" Type="String" />
<asparameter Name="Modified" Type="String" />
</UpdateParameters>
<InsertParameters>
<asparameter Direction="InputOutput" Name="PKId" Type="Decimal" />
<asparameter Name="FirstName" Type="String" />
<asparameter 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("lblModified")).Text);
e.Command.Parameters["@Modified"].Value = modified;
}
I have a GridView and a SqlDataSource controls on a page. The SqlDataSource
object uses stored procedures to do the CRUD operations. The DataSource has
three columns one of which - "Modified" of type DateTime - is hidden since it
should not be edited by a user. The system handles the update for this
column. So, I have hidden (Visible=false) this column on the grid. In order
to access the value in this field, I have created a template column and
assigned the "Modified" column to it. The Update Stored Procedure does the
cocurrency checks by looking at the value in the Modified column which holds
the last modified datetime. Everything seems to work fine except that the
datetime value that is held in the template column does not equal the
"Modified" column in the table when the stored procedure compares it.
Obviously, when I store the "Modified" column value in the template field's
text property, somehow it looses the information partly. In other words, may
be, it is dropping the milli seconds. How can I make these two values compare
properly. I am pasting the 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,
@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>
<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="<%$ ConnectionStringsataNozzleDevConnectionString %>"
DeleteCommand="ProperNamesDelete" DeleteCommandType="StoredProcedure"
InsertCommand="ProperNamesInsert"
InsertCommandType="StoredProcedure" SelectCommand="ProperNamesSelectAll"
SelectCommandType="StoredProcedure"
UpdateCommand="ProperNamesUpdate" UpdateCommandType="StoredProcedure"
OnUpdating="SqlDataSource1_Updating">
<DeleteParameters>
<asparameter Name="PKId" Type="Decimal" />
</DeleteParameters>
<UpdateParameters>
<asparameter Direction="InputOutput" Name="PKId" Type="Decimal" />
<asparameter Name="FirstName" Type="String" />
<asparameter Name="LastName" Type="String" />
<asparameter Name="Modified" Type="String" />
</UpdateParameters>
<InsertParameters>
<asparameter Direction="InputOutput" Name="PKId" Type="Decimal" />
<asparameter Name="FirstName" Type="String" />
<asparameter 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("lblModified")).Text);
e.Command.Parameters["@Modified"].Value = modified;
}