G
Guest
When trying to update a record in an editable datagrid I ran into a few
problems.
My update procedure is just not working.
Can someone please have a look at my code and see what am I doing wrong.
I had to set the datagrid's DataKeyField to MailSubscriberID manually since
vs does not do it automatically.
Thanks
This is what my table looks like in SQL 2000:
CREATE TABLE [dbo].[tabMailSubscribers] (
[MailSubscriberID] uniqueidentifier ROWGUIDCOL NOT NULL ,
[HotelID] [int] NOT NULL ,
[Name] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Surname] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Designation] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[EmailAddress] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[UpdatedBy] [int] NOT NULL ,
[DateUpdated] [datetime] NOT NULL
) ON [PRIMARY]
GO
This is the update procedure:
CREATE PROCEDURE dbo.proc_tabMailSubscibers_Update
(
@MailSubscriberID uniqueidentifier,
@HotelID int,
@Name varchar(50),
@Surname varchar(50),
@Designation varchar(50),
@EmailAddress varchar(50),
@UpdatedBy int
)
AS
SET NOCOUNT OFF;
UPDATE tabMailSubscribers SET HotelID = @HotelID, Name = @Name, Surname =
@Surname, Designation = @Designation, EmailAddress = @EmailAddress, UpdatedBy
= @UpdatedBy, DateUpdated = (GetDate()) WHERE (MailSubscriberID =
@MailSubscriberID) ;
SELECT MailSubscriberID, HotelID, Name, Surname, Designation, EmailAddress,
UpdatedBy, DateUpdated FROM tabMailSubscribers WHERE (MailSubscriberID =
@MailSubscriberID)
GO
This is my code to update the record:
Private Sub DGMailSubscribers_UpdateCommand(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DGMailSubscribers.UpdateCommand
CType(e.Item.Cells(8).Controls(0), TextBox).Text = _
CType(e.Item.FindControl("ddlHotelID"), DropDownList).SelectedValue
' Step two: Fill DataSet and identify row to edit
SqlDAMailSubscribers.Fill(DsMailSubscribers1)
DAHotel.Fill(DsHotel1)
'DGMailSubscribers.DataBind()
Dim objEditRow As DataRow =
DsMailSubscribers1.Tables(0).Rows.Find(CType(e.Item.Cells(0).Controls(0),
TextBox).Text)
' Step three: Cycle through valid "data" cells and put
' information back in underlying DataSet
SqlConn.Open()
Dim cnn As SqlClient.SqlConnection = SqlConn
Dim cmdUpdate As New SqlClient.SqlCommand
cmdUpdate = cnn.CreateCommand
cmdUpdate.CommandText = "proc_tabMailSubscribers_Update"
cmdUpdate.CommandType = CommandType.StoredProcedure
cmdUpdate.Parameters.Add("@MailSubscriberID",
SqlDbType.UniqueIdentifier)
cmdUpdate.Parameters.Add("@HotelID", SqlDbType.Int)
cmdUpdate.Parameters.Add("@Name", SqlDbType.VarChar)
cmdUpdate.Parameters.Add("@Surname", SqlDbType.VarChar)
cmdUpdate.Parameters.Add("@Designation", SqlDbType.VarChar)
cmdUpdate.Parameters.Add("@EmailAddress", SqlDbType.VarChar)
cmdUpdate.Parameters.Add("@UpdatedBy", SqlDbType.Int)
'Update
cmdUpdate.Parameters("@MailSubscriberID").Value =
DGMailSubscribers.DataKeys(e.Item.ItemIndex)
cmdUpdate.Parameters("@HotelID").Value =
CType(e.Item.FindControl("ddlHotelID"), DropDownList).SelectedItem.Value()
cmdUpdate.Parameters("@Name").Value =
CType(e.Item.Cells(0).Controls(0), TextBox).Text
cmdUpdate.Parameters("@Surname").Value =
CType(e.Item.Cells(1).Controls(0), TextBox).Text
cmdUpdate.Parameters("@Designation").Value =
CType(e.Item.Cells(2).Controls(0), TextBox).Text
cmdUpdate.Parameters("@EmailAddress").Value =
CType(e.Item.Cells(3).Controls(0), TextBox).Text
cmdUpdate.Parameters("@UpdatedBy").Value = Session("LoginID")
Try
cmdUpdate.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Updated.</b><br>"
'DGMailSubscribers.EditItemIndex = -5
Catch ex As SqlException
Message.InnerHtml = "ERROR: Could not update record," _
& " please ensure the fields are correctly filled out."
Message.Style("color") = "red"
End Try
problems.
My update procedure is just not working.
Can someone please have a look at my code and see what am I doing wrong.
I had to set the datagrid's DataKeyField to MailSubscriberID manually since
vs does not do it automatically.
Thanks
This is what my table looks like in SQL 2000:
CREATE TABLE [dbo].[tabMailSubscribers] (
[MailSubscriberID] uniqueidentifier ROWGUIDCOL NOT NULL ,
[HotelID] [int] NOT NULL ,
[Name] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Surname] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[Designation] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[EmailAddress] [varchar] (50) COLLATE Latin1_General_CI_AS NOT NULL ,
[UpdatedBy] [int] NOT NULL ,
[DateUpdated] [datetime] NOT NULL
) ON [PRIMARY]
GO
This is the update procedure:
CREATE PROCEDURE dbo.proc_tabMailSubscibers_Update
(
@MailSubscriberID uniqueidentifier,
@HotelID int,
@Name varchar(50),
@Surname varchar(50),
@Designation varchar(50),
@EmailAddress varchar(50),
@UpdatedBy int
)
AS
SET NOCOUNT OFF;
UPDATE tabMailSubscribers SET HotelID = @HotelID, Name = @Name, Surname =
@Surname, Designation = @Designation, EmailAddress = @EmailAddress, UpdatedBy
= @UpdatedBy, DateUpdated = (GetDate()) WHERE (MailSubscriberID =
@MailSubscriberID) ;
SELECT MailSubscriberID, HotelID, Name, Surname, Designation, EmailAddress,
UpdatedBy, DateUpdated FROM tabMailSubscribers WHERE (MailSubscriberID =
@MailSubscriberID)
GO
This is my code to update the record:
Private Sub DGMailSubscribers_UpdateCommand(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DGMailSubscribers.UpdateCommand
CType(e.Item.Cells(8).Controls(0), TextBox).Text = _
CType(e.Item.FindControl("ddlHotelID"), DropDownList).SelectedValue
' Step two: Fill DataSet and identify row to edit
SqlDAMailSubscribers.Fill(DsMailSubscribers1)
DAHotel.Fill(DsHotel1)
'DGMailSubscribers.DataBind()
Dim objEditRow As DataRow =
DsMailSubscribers1.Tables(0).Rows.Find(CType(e.Item.Cells(0).Controls(0),
TextBox).Text)
' Step three: Cycle through valid "data" cells and put
' information back in underlying DataSet
SqlConn.Open()
Dim cnn As SqlClient.SqlConnection = SqlConn
Dim cmdUpdate As New SqlClient.SqlCommand
cmdUpdate = cnn.CreateCommand
cmdUpdate.CommandText = "proc_tabMailSubscribers_Update"
cmdUpdate.CommandType = CommandType.StoredProcedure
cmdUpdate.Parameters.Add("@MailSubscriberID",
SqlDbType.UniqueIdentifier)
cmdUpdate.Parameters.Add("@HotelID", SqlDbType.Int)
cmdUpdate.Parameters.Add("@Name", SqlDbType.VarChar)
cmdUpdate.Parameters.Add("@Surname", SqlDbType.VarChar)
cmdUpdate.Parameters.Add("@Designation", SqlDbType.VarChar)
cmdUpdate.Parameters.Add("@EmailAddress", SqlDbType.VarChar)
cmdUpdate.Parameters.Add("@UpdatedBy", SqlDbType.Int)
'Update
cmdUpdate.Parameters("@MailSubscriberID").Value =
DGMailSubscribers.DataKeys(e.Item.ItemIndex)
cmdUpdate.Parameters("@HotelID").Value =
CType(e.Item.FindControl("ddlHotelID"), DropDownList).SelectedItem.Value()
cmdUpdate.Parameters("@Name").Value =
CType(e.Item.Cells(0).Controls(0), TextBox).Text
cmdUpdate.Parameters("@Surname").Value =
CType(e.Item.Cells(1).Controls(0), TextBox).Text
cmdUpdate.Parameters("@Designation").Value =
CType(e.Item.Cells(2).Controls(0), TextBox).Text
cmdUpdate.Parameters("@EmailAddress").Value =
CType(e.Item.Cells(3).Controls(0), TextBox).Text
cmdUpdate.Parameters("@UpdatedBy").Value = Session("LoginID")
Try
cmdUpdate.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Updated.</b><br>"
'DGMailSubscribers.EditItemIndex = -5
Catch ex As SqlException
Message.InnerHtml = "ERROR: Could not update record," _
& " please ensure the fields are correctly filled out."
Message.Style("color") = "red"
End Try