By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 1,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

Gridview Hidden DateTime column value update problem

P: n/a
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="<%$ 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;
}
--
BabuMan
Feb 1 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
BabuMan,

Make sure that the column type in your SQL database is DATETIME rather than
SMALLDATETIME.

Is that the case?

--
Regards,
Robson Siqueira
Enterprise Architect
"BabuMan" <Ba*****@discussions.microsoft.comwrote in message
news:CB**********************************@microsof t.com...
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="<%$ 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;
}
--
BabuMan

Feb 1 '07 #2

P: n/a
Thanks for the reply Robson. The column type in the SQL Database table is
datetime (not smalldatetime). I have included the Table structure as sql
script in the post. I hope this helps analyze further.
--
BabuMan
"BabuMan" wrote:
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="<%$ 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;
}
--
BabuMan
Feb 1 '07 #3

P: n/a
BabuMan,

Are you not loosing your data when converting the text data to DateTime?

Besides, why don't you receive the parameter in the stored procedure as
datetime instead of varchar? Please remember that there is a implicit cast
here and your problem may be here as well.

--
Regards,
Robson Siqueira
Enterprise Architect
"BabuMan" <Ba*****@discussions.microsoft.comwrote in message
news:84**********************************@microsof t.com...
Thanks for the reply Robson. The column type in the SQL Database table is
datetime (not smalldatetime). I have included the Table structure as sql
script in the post. I hope this helps analyze further.
--
BabuMan
"BabuMan" wrote:
>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="<%$ 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("lblMod ified")).Text);
e.Command.Parameters["@Modified"].Value = modified;
}
--
BabuMan

Feb 1 '07 #4

P: n/a
Thanks Again. Yes. That is exactly my problem. I am loosing the data
partially when recieved by the stored procedure. But I have no other choice
since the date is stored as text when it is initially retrieved. That again
is compulsion and not a choice.

Anyway, I think I am very close to finding an efficient and fairly "best
practice" solution for this problem. But thank you very much for your
effort. I really appreciate it.
--
BabuMan
"Robson Siqueira" wrote:
BabuMan,

Are you not loosing your data when converting the text data to DateTime?

Besides, why don't you receive the parameter in the stored procedure as
datetime instead of varchar? Please remember that there is a implicit cast
here and your problem may be here as well.

--
Regards,
Robson Siqueira
Enterprise Architect
"BabuMan" <Ba*****@discussions.microsoft.comwrote in message
news:84**********************************@microsof t.com...
Thanks for the reply Robson. The column type in the SQL Database table is
datetime (not smalldatetime). I have included the Table structure as sql
script in the post. I hope this helps analyze further.
--
BabuMan
"BabuMan" wrote:
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="<%$ 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;
}
--
BabuMan


Feb 2 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.