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 MailSubscriberI D manually since
vs does not do it automatically.
Thanks
This is what my table looks like in SQL 2000:
CREATE TABLE [dbo].[tabMailSubscrib ers] (
[MailSubscriberI D] uniqueidentifie r 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_tabMai lSubscibers_Upd ate
(
@MailSubscriber ID uniqueidentifie r,
@HotelID int,
@Name varchar(50),
@Surname varchar(50),
@Designation varchar(50),
@EmailAddress varchar(50),
@UpdatedBy int
)
AS
SET NOCOUNT OFF;
UPDATE tabMailSubscrib ers SET HotelID = @HotelID, Name = @Name, Surname =
@Surname, Designation = @Designation, EmailAddress = @EmailAddress, UpdatedBy
= @UpdatedBy, DateUpdated = (GetDate()) WHERE (MailSubscriber ID =
@MailSubscriber ID) ;
SELECT MailSubscriberI D, HotelID, Name, Surname, Designation, EmailAddress,
UpdatedBy, DateUpdated FROM tabMailSubscrib ers WHERE (MailSubscriber ID =
@MailSubscriber ID)
GO
This is my code to update the record:
Private Sub DGMailSubscribe rs_UpdateComman d(ByVal source As Object,
ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles
DGMailSubscribe rs.UpdateComman d
CType(e.Item.Ce lls(8).Controls (0), TextBox).Text = _
CType(e.Item.Fi ndControl("ddlH otelID"), DropDownList).S electedValue
' Step two: Fill DataSet and identify row to edit
SqlDAMailSubscr ibers.Fill(DsMa ilSubscribers1)
DAHotel.Fill(Ds Hotel1)
'DGMailSubscrib ers.DataBind()
Dim objEditRow As DataRow =
DsMailSubscribe rs1.Tables(0).R ows.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.SqlCo nnection = SqlConn
Dim cmdUpdate As New SqlClient.SqlCo mmand
cmdUpdate = cnn.CreateComma nd
cmdUpdate.Comma ndText = "proc_tabMailSu bscribers_Updat e"
cmdUpdate.Comma ndType = CommandType.Sto redProcedure
cmdUpdate.Param eters.Add("@Mai lSubscriberID",
SqlDbType.Uniqu eIdentifier)
cmdUpdate.Param eters.Add("@Hot elID", SqlDbType.Int)
cmdUpdate.Param eters.Add("@Nam e", SqlDbType.VarCh ar)
cmdUpdate.Param eters.Add("@Sur name", SqlDbType.VarCh ar)
cmdUpdate.Param eters.Add("@Des ignation", SqlDbType.VarCh ar)
cmdUpdate.Param eters.Add("@Ema ilAddress", SqlDbType.VarCh ar)
cmdUpdate.Param eters.Add("@Upd atedBy", SqlDbType.Int)
'Update
cmdUpdate.Param eters("@MailSub scriberID").Val ue =
DGMailSubscribe rs.DataKeys(e.I tem.ItemIndex)
cmdUpdate.Param eters("@HotelID ").Value =
CType(e.Item.Fi ndControl("ddlH otelID"), DropDownList).S electedItem.Val ue()
cmdUpdate.Param eters("@Name"). Value =
CType(e.Item.Ce lls(0).Controls (0), TextBox).Text
cmdUpdate.Param eters("@Surname ").Value =
CType(e.Item.Ce lls(1).Controls (0), TextBox).Text
cmdUpdate.Param eters("@Designa tion").Value =
CType(e.Item.Ce lls(2).Controls (0), TextBox).Text
cmdUpdate.Param eters("@EmailAd dress").Value =
CType(e.Item.Ce lls(3).Controls (0), TextBox).Text
cmdUpdate.Param eters("@Updated By").Value = Session("LoginI D")
Try
cmdUpdate.Execu teNonQuery()
Message.InnerHt ml = "<b>Record Updated.</b><br>"
'DGMailSubscrib ers.EditItemInd ex = -5
Catch ex As SqlException
Message.InnerHt ml = "ERROR: Could not update record," _
& " please ensure the fields are correctly filled out."
Message.Style(" color") = "red"
End Try 5 1871
Hi Hennie,
Do you bind datasource of datagrid every time, no matter
it is in the first time or in postback. If that, change it
to only bind in IsPostback = false.
HTH
Elton Wang el********@hotm ail.com -----Original Message----- When trying to update a record in an editable datagrid I
ran into a fewproblems. 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
MailSubscriberI D manually sincevs does not do it automatically.
Thanks
This is what my table looks like in SQL 2000: CREATE TABLE [dbo].[tabMailSubscrib ers] ( [MailSubscriberI D] uniqueidentifie r 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_tabMai lSubscibers_Upd ate ( @MailSubscriber ID uniqueidentifie r, @HotelID int, @Name varchar(50), @Surname varchar(50), @Designation varchar(50), @EmailAddress varchar(50), @UpdatedBy int ) AS SET NOCOUNT OFF; UPDATE tabMailSubscrib ers SET HotelID = @HotelID, Name =
@Name, Surname =@Surname, Designation = @Designation, EmailAddress =
@EmailAddress, UpdatedBy= @UpdatedBy, DateUpdated = (GetDate()) WHERE
(MailSubscriber ID =@MailSubscribe rID) ; SELECT MailSubscriberI D, HotelID, Name, Surname,
Designation, EmailAddress,UpdatedBy, DateUpdated FROM tabMailSubscrib ers WHERE
(MailSubscriber ID =@MailSubscribe rID) GO
This is my code to update the record:
Private Sub DGMailSubscribe rs_UpdateComman d(ByVal
source As Object,ByVal e As
System.Web.UI.W ebControls.Data GridCommandEven tArgs)
HandlesDGMailSubscrib ers.UpdateComma nd CType(e.Item.Ce lls(8).Controls (0), TextBox).Text
= _ CType(e.Item.Fi ndControl("ddlH otelID"),
DropDownList).S electedValue ' Step two: Fill DataSet and identify row to edit
SqlDAMailSubscr ibers.Fill(DsMa ilSubscribers1) DAHotel.Fill(Ds Hotel1) 'DGMailSubscrib ers.DataBind() Dim objEditRow As DataRow = DsMailSubscrib ers1.Tables(0). Rows.Find(CType (e.Item.Cells
(0).Controls(0) ,TextBox).Tex t) ' Step three: Cycle through valid "data" cells
and put ' information back in underlying
DataSet SqlConn.Open() Dim cnn As SqlClient.SqlCo nnection = SqlConn Dim cmdUpdate As New SqlClient.SqlCo mmand cmdUpdate = cnn.CreateComma nd cmdUpdate.Comma ndText
= "proc_tabMailSu bscribers_Updat e" cmdUpdate.Comma ndType =
CommandType.Sto redProcedure cmdUpdate.Param eters.Add("@Mai lSubscriberID", SqlDbType.Uniq ueIdentifier) cmdUpdate.Param eters.Add("@Hot elID",
SqlDbType.Int) cmdUpdate.Param eters.Add("@Nam e",
SqlDbType.VarCh ar) cmdUpdate.Param eters.Add("@Sur name",
SqlDbType.VarCh ar) cmdUpdate.Param eters.Add("@Des ignation",
SqlDbType.VarCh ar) cmdUpdate.Param eters.Add("@Ema ilAddress",
SqlDbType.VarCh ar) cmdUpdate.Param eters.Add("@Upd atedBy",
SqlDbType.Int) 'Update cmdUpdate.Param eters("@MailSub scriberID").Val ue = DGMailSubscrib ers.DataKeys(e. Item.ItemIndex) cmdUpdate.Param eters("@HotelID ").Value = CType(e.Item.F indControl("ddl HotelID"),
DropDownList).S electedItem.Val ue() cmdUpdate.Param eters("@Name"). Value = CType(e.Item.C ells(0).Control s(0), TextBox).Text cmdUpdate.Param eters("@Surname ").Value = CType(e.Item.C ells(1).Control s(0), TextBox).Text cmdUpdate.Param eters("@Designa tion").Value = CType(e.Item.C ells(2).Control s(0), TextBox).Text cmdUpdate.Param eters("@EmailAd dress").Value = CType(e.Item.C ells(3).Control s(0), TextBox).Text cmdUpdate.Param eters("@Updated By").Value = Session
("LoginID") Try cmdUpdate.Execu teNonQuery() Message.InnerHt ml = "<b>Record
Updated.</b><br>" 'DGMailSubscrib ers.EditItemInd ex = -5 Catch ex As SqlException Message.InnerHt ml = "ERROR: Could not update
record," _ & " please ensure the fields are correctly filled out." Message.Style(" color") = "red"
End Try .
Hi Elton
Thanks for your interest.
I only bind in the Page_Load event.
Hennie
"Elton Wang" wrote: Hi Hennie,
Do you bind datasource of datagrid every time, no matter it is in the first time or in postback. If that, change it to only bind in IsPostback = false.
HTH
Elton Wang el********@hotm ail.com
-----Original Message----- When trying to update a record in an editable datagrid I ran into a fewproblems. 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
MailSubscriberI D manually sincevs does not do it automatically.
Thanks
This is what my table looks like in SQL 2000: CREATE TABLE [dbo].[tabMailSubscrib ers] ( [MailSubscriberI D] uniqueidentifie r 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_tabMai lSubscibers_Upd ate ( @MailSubscriber ID uniqueidentifie r, @HotelID int, @Name varchar(50), @Surname varchar(50), @Designation varchar(50), @EmailAddress varchar(50), @UpdatedBy int ) AS SET NOCOUNT OFF; UPDATE tabMailSubscrib ers SET HotelID = @HotelID, Name = @Name, Surname =@Surname, Designation = @Designation, EmailAddress = @EmailAddress, UpdatedBy= @UpdatedBy, DateUpdated = (GetDate()) WHERE (MailSubscriber ID =@MailSubscribe rID) ; SELECT MailSubscriberI D, HotelID, Name, Surname, Designation, EmailAddress,UpdatedBy, DateUpdated FROM tabMailSubscrib ers WHERE (MailSubscriber ID =@MailSubscribe rID) GO
This is my code to update the record:
Private Sub DGMailSubscribe rs_UpdateComman d(ByVal source As Object,ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) HandlesDGMailSubscrib ers.UpdateComma nd CType(e.Item.Ce lls(8).Controls (0), TextBox).Text = _ CType(e.Item.Fi ndControl("ddlH otelID"), DropDownList).S electedValue ' Step two: Fill DataSet and identify row to edit
SqlDAMailSubscr ibers.Fill(DsMa ilSubscribers1) DAHotel.Fill(Ds Hotel1) 'DGMailSubscrib ers.DataBind() Dim objEditRow As DataRow = DsMailSubscrib ers1.Tables(0). Rows.Find(CType (e.Item.Cells
(0).Controls(0) ,TextBox).Tex t) ' Step three: Cycle through valid "data" cells and put ' information back in underlying DataSet SqlConn.Open() Dim cnn As SqlClient.SqlCo nnection = SqlConn Dim cmdUpdate As New SqlClient.SqlCo mmand cmdUpdate = cnn.CreateComma nd cmdUpdate.Comma ndText
= "proc_tabMailSu bscribers_Updat e" cmdUpdate.Comma ndType = CommandType.Sto redProcedure cmdUpdate.Param eters.Add("@Mai lSubscriberID", SqlDbType.Uniq ueIdentifier) cmdUpdate.Param eters.Add("@Hot elID", SqlDbType.Int) cmdUpdate.Param eters.Add("@Nam e", SqlDbType.VarCh ar) cmdUpdate.Param eters.Add("@Sur name", SqlDbType.VarCh ar) cmdUpdate.Param eters.Add("@Des ignation", SqlDbType.VarCh ar) cmdUpdate.Param eters.Add("@Ema ilAddress", SqlDbType.VarCh ar) cmdUpdate.Param eters.Add("@Upd atedBy", SqlDbType.Int) 'Update cmdUpdate.Param eters("@MailSub scriberID").Val ue = DGMailSubscrib ers.DataKeys(e. Item.ItemIndex) cmdUpdate.Param eters("@HotelID ").Value = CType(e.Item.F indControl("ddl HotelID"),
DropDownList).S electedItem.Val ue() cmdUpdate.Param eters("@Name"). Value = CType(e.Item.C ells(0).Control s(0), TextBox).Text cmdUpdate.Param eters("@Surname ").Value = CType(e.Item.C ells(1).Control s(0), TextBox).Text cmdUpdate.Param eters("@Designa tion").Value = CType(e.Item.C ells(2).Control s(0), TextBox).Text cmdUpdate.Param eters("@EmailAd dress").Value = CType(e.Item.C ells(3).Control s(0), TextBox).Text cmdUpdate.Param eters("@Updated By").Value = Session ("LoginID") Try cmdUpdate.Execu teNonQuery() Message.InnerHt ml = "<b>Record
Updated.</b><br>" 'DGMailSubscrib ers.EditItemInd ex = -5 Catch ex As SqlException Message.InnerHt ml = "ERROR: Could not update
record," _ & " please ensure the fields are correctly filled out." Message.Style(" color") = "red"
End Try .
That's right. And you also need
If Not IsPostBack Then
' Bind datagrid
End If -----Original Message----- Hi Elton Thanks for your interest.
I only bind in the Page_Load event.
Hennie
"Elton Wang" wrote:
Hi Hennie,
Do you bind datasource of datagrid every time, no
matter it is in the first time or in postback. If that, change
it to only bind in IsPostback = false.
HTH
Elton Wang el********@hotm ail.com
>-----Original Message----- >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 MailSubscriberI D manually since >vs does not do it automatically. > >Thanks > >This is what my table looks like in SQL 2000: >CREATE TABLE [dbo].[tabMailSubscrib ers] ( > [MailSubscriberI D] uniqueidentifie r 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_tabMai lSubscibers_Upd ate >( > @MailSubscriber ID uniqueidentifie r, > @HotelID int, > @Name varchar(50), > @Surname varchar(50), > @Designation varchar(50), > @EmailAddress varchar(50), > @UpdatedBy int >) >AS > SET NOCOUNT OFF; >UPDATE tabMailSubscrib ers SET HotelID = @HotelID, Name
= @Name, Surname = >@Surname, Designation = @Designation, EmailAddress = @EmailAddress, UpdatedBy >= @UpdatedBy, DateUpdated = (GetDate()) WHERE (MailSubscriber ID = >@MailSubscribe rID) ; > SELECT MailSubscriberI D, HotelID, Name, Surname, Designation, EmailAddress, >UpdatedBy, DateUpdated FROM tabMailSubscrib ers WHERE (MailSubscriber ID = >@MailSubscribe rID) >GO > >This is my code to update the record: > > Private Sub DGMailSubscribe rs_UpdateComman d(ByVal source As Object, >ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles >DGMailSubscrib ers.UpdateComma nd > CType(e.Item.Ce lls(8).Controls (0),
TextBox).Text = _ > CType(e.Item.Fi ndControl("ddlH otelID"), DropDownList).S electedValue > > ' Step two: Fill DataSet and identify row to
edit > > SqlDAMailSubscr ibers.Fill(DsMa ilSubscribers1) > DAHotel.Fill(Ds Hotel1) > 'DGMailSubscrib ers.DataBind() > Dim objEditRow As DataRow = >DsMailSubscrib ers1.Tables(0). Rows.Find(CType
(e.Item.Cells (0).Controls(0) , >TextBox).Tex t) > ' Step three: Cycle through valid "data" cells and put > ' information back in underlying DataSet > > SqlConn.Open() > Dim cnn As SqlClient.SqlCo nnection = SqlConn > Dim cmdUpdate As New SqlClient.SqlCo mmand > cmdUpdate = cnn.CreateComma nd > cmdUpdate.Comma ndText = "proc_tabMailSu bscribers_Updat e" > cmdUpdate.Comma ndType = CommandType.Sto redProcedure > cmdUpdate.Param eters.Add("@Mai lSubscriberID", >SqlDbType.Uniq ueIdentifier) > cmdUpdate.Param eters.Add("@Hot elID", SqlDbType.Int) > cmdUpdate.Param eters.Add("@Nam e", SqlDbType.VarCh ar) > cmdUpdate.Param eters.Add("@Sur name", SqlDbType.VarCh ar) > cmdUpdate.Param eters.Add("@Des ignation", SqlDbType.VarCh ar) > cmdUpdate.Param eters.Add("@Ema ilAddress", SqlDbType.VarCh ar) > cmdUpdate.Param eters.Add("@Upd atedBy", SqlDbType.Int) > > 'Update > cmdUpdate.Param eters
("@MailSubscrib erID").Value = >DGMailSubscrib ers.DataKeys(e. Item.ItemIndex) > cmdUpdate.Param eters("@HotelID ").Value = >CType(e.Item.F indControl("ddl HotelID"), DropDownList).S electedItem.Val ue() > cmdUpdate.Param eters("@Name"). Value = >CType(e.Item.C ells(0).Control s(0), TextBox).Text > cmdUpdate.Param eters("@Surname ").Value = >CType(e.Item.C ells(1).Control s(0), TextBox).Text > cmdUpdate.Param eters("@Designa tion").Value = >CType(e.Item.C ells(2).Control s(0), TextBox).Text > cmdUpdate.Param eters("@EmailAd dress").Value = >CType(e.Item.C ells(3).Control s(0), TextBox).Text > cmdUpdate.Param eters("@Updated By").Value =
Session ("LoginID") > > Try > cmdUpdate.Execu teNonQuery() > Message.InnerHt ml = "<b>Record Updated.</b><br>" > > 'DGMailSubscrib ers.EditItemInd ex = -5 > Catch ex As SqlException > Message.InnerHt ml = "ERROR: Could not
update record," _ > & " please ensure the fields are correctly filled
out." > Message.Style(" color") = "red" > > End Try >. > .
OK, So I am doing everything correct, so why is it not working?
"Elton Wang" wrote: That's right. And you also need
If Not IsPostBack Then ' Bind datagrid End If
-----Original Message----- Hi Elton Thanks for your interest.
I only bind in the Page_Load event.
Hennie
"Elton Wang" wrote:
Hi Hennie,
Do you bind datasource of datagrid every time, no matter it is in the first time or in postback. If that, change it to only bind in IsPostback = false.
HTH
Elton Wang el********@hotm ail.com
>-----Original Message----- >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 MailSubscriberI D manually since >vs does not do it automatically. > >Thanks > >This is what my table looks like in SQL 2000: >CREATE TABLE [dbo].[tabMailSubscrib ers] ( > [MailSubscriberI D] uniqueidentifie r 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_tabMai lSubscibers_Upd ate >( > @MailSubscriber ID uniqueidentifie r, > @HotelID int, > @Name varchar(50), > @Surname varchar(50), > @Designation varchar(50), > @EmailAddress varchar(50), > @UpdatedBy int >) >AS > SET NOCOUNT OFF; >UPDATE tabMailSubscrib ers SET HotelID = @HotelID, Name = @Name, Surname = >@Surname, Designation = @Designation, EmailAddress = @EmailAddress, UpdatedBy >= @UpdatedBy, DateUpdated = (GetDate()) WHERE (MailSubscriber ID = >@MailSubscribe rID) ; > SELECT MailSubscriberI D, HotelID, Name, Surname, Designation, EmailAddress, >UpdatedBy, DateUpdated FROM tabMailSubscrib ers WHERE (MailSubscriber ID = >@MailSubscribe rID) >GO > >This is my code to update the record: > > Private Sub DGMailSubscribe rs_UpdateComman d(ByVal source As Object, >ByVal e As System.Web.UI.W ebControls.Data GridCommandEven tArgs) Handles >DGMailSubscrib ers.UpdateComma nd > CType(e.Item.Ce lls(8).Controls (0), TextBox).Text = _ > CType(e.Item.Fi ndControl("ddlH otelID"), DropDownList).S electedValue > > ' Step two: Fill DataSet and identify row to edit > > SqlDAMailSubscr ibers.Fill(DsMa ilSubscribers1) > DAHotel.Fill(Ds Hotel1) > 'DGMailSubscrib ers.DataBind() > Dim objEditRow As DataRow = >DsMailSubscrib ers1.Tables(0). Rows.Find(CType (e.Item.Cells (0).Controls(0) , >TextBox).Tex t) > ' Step three: Cycle through valid "data" cells and put > ' information back in underlying DataSet > > SqlConn.Open() > Dim cnn As SqlClient.SqlCo nnection = SqlConn > Dim cmdUpdate As New SqlClient.SqlCo mmand > cmdUpdate = cnn.CreateComma nd > cmdUpdate.Comma ndText = "proc_tabMailSu bscribers_Updat e" > cmdUpdate.Comma ndType = CommandType.Sto redProcedure > cmdUpdate.Param eters.Add("@Mai lSubscriberID", >SqlDbType.Uniq ueIdentifier) > cmdUpdate.Param eters.Add("@Hot elID", SqlDbType.Int) > cmdUpdate.Param eters.Add("@Nam e", SqlDbType.VarCh ar) > cmdUpdate.Param eters.Add("@Sur name", SqlDbType.VarCh ar) > cmdUpdate.Param eters.Add("@Des ignation", SqlDbType.VarCh ar) > cmdUpdate.Param eters.Add("@Ema ilAddress", SqlDbType.VarCh ar) > cmdUpdate.Param eters.Add("@Upd atedBy", SqlDbType.Int) > > 'Update > cmdUpdate.Param eters ("@MailSubscrib erID").Value = >DGMailSubscrib ers.DataKeys(e. Item.ItemIndex) > cmdUpdate.Param eters("@HotelID ").Value = >CType(e.Item.F indControl("ddl HotelID"), DropDownList).S electedItem.Val ue() > cmdUpdate.Param eters("@Name"). Value = >CType(e.Item.C ells(0).Control s(0), TextBox).Text > cmdUpdate.Param eters("@Surname ").Value = >CType(e.Item.C ells(1).Control s(0), TextBox).Text > cmdUpdate.Param eters("@Designa tion").Value = >CType(e.Item.C ells(2).Control s(0), TextBox).Text > cmdUpdate.Param eters("@EmailAd dress").Value = >CType(e.Item.C ells(3).Control s(0), TextBox).Text > cmdUpdate.Param eters("@Updated By").Value = Session ("LoginID") > > Try > cmdUpdate.Execu teNonQuery() > Message.InnerHt ml = "<b>Record Updated.</b><br>" > > 'DGMailSubscrib ers.EditItemInd ex = -5 > Catch ex As SqlException > Message.InnerHt ml = "ERROR: Could not update record," _ > & " please ensure the fields are correctly filled out." > Message.Style(" color") = "red" > > End Try >. > .
You can do three steps to find out error:
Step 1. Does SP works? Directly run SP from QA. If it works
Step 2. Does update code works? Directly run update code,
e.g. use a test program and hard code assign values to
parameters. If it works
Step 3. In UpdateCommand event the parameters are assigned
correct values? Set breakpoint in the event and trace the
running process to see if all things are correct.
I think you should be able to find out error.
HTH
Elton Wang -----Original Message----- OK, So I am doing everything correct, so why is it not
working? "Elton Wang" wrote:
That's right. And you also need
If Not IsPostBack Then ' Bind datagrid End If
>-----Original Message----- >Hi Elton >Thanks for your interest. > >I only bind in the Page_Load event. > >Hennie > >"Elton Wang" wrote: > >> Hi Hennie, >> >> Do you bind datasource of datagrid every time, no matter >> it is in the first time or in postback. If that,
change it >> to only bind in IsPostback = false. >> >> HTH >> >> Elton Wang >> el********@hotm ail.com >> >> >> >-----Original Message----- >> >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 >> MailSubscriberI D manually since >> >vs does not do it automatically. >> > >> >Thanks >> > >> >This is what my table looks like in SQL 2000: >> >CREATE TABLE [dbo].[tabMailSubscrib ers] ( >> > [MailSubscriberI D] uniqueidentifie r 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_tabMai lSubscibers_Upd ate >> >( >> > @MailSubscriber ID uniqueidentifie r, >> > @HotelID int, >> > @Name varchar(50), >> > @Surname varchar(50), >> > @Designation varchar(50), >> > @EmailAddress varchar(50), >> > @UpdatedBy int >> >) >> >AS >> > SET NOCOUNT OFF; >> >UPDATE tabMailSubscrib ers SET HotelID = @HotelID,
Name = >> @Name, Surname = >> >@Surname, Designation = @Designation, EmailAddress
= >> @EmailAddress, UpdatedBy >> >= @UpdatedBy, DateUpdated = (GetDate()) WHERE >> (MailSubscriber ID = >> >@MailSubscribe rID) ; >> > SELECT MailSubscriberI D, HotelID, Name, Surname, >> Designation, EmailAddress, >> >UpdatedBy, DateUpdated FROM tabMailSubscrib ers
WHERE >> (MailSubscriber ID = >> >@MailSubscribe rID) >> >GO >> > >> >This is my code to update the record: >> > >> > Private Sub DGMailSubscribe rs_UpdateComman d
(ByVal >> source As Object, >> >ByVal e As >> System.Web.UI.W ebControls.Data GridCommandEven tArgs) >> Handles >> >DGMailSubscrib ers.UpdateComma nd >> > CType(e.Item.Ce lls(8).Controls (0), TextBox).Text >> = _ >> > CType(e.Item.Fi ndControl("ddlH otelID"), >> DropDownList).S electedValue >> > >> > ' Step two: Fill DataSet and identify row
to edit >> > >> > SqlDAMailSubscr ibers.Fill
(DsMailSubscrib ers1) >> > DAHotel.Fill(Ds Hotel1) >> > 'DGMailSubscrib ers.DataBind() >> > Dim objEditRow As DataRow = >> >DsMailSubscrib ers1.Tables(0). Rows.Find(CType (e.Item.Cells >> (0).Controls(0) , >> >TextBox).Tex t) >> > ' Step three: Cycle through valid "data"
cells >> and put >> > ' information back in
underlying >> DataSet >> > >> > SqlConn.Open() >> > Dim cnn As SqlClient.SqlCo nnection = SqlConn >> > Dim cmdUpdate As New SqlClient.SqlCo mmand >> > cmdUpdate = cnn.CreateComma nd >> > cmdUpdate.Comma ndText >> = "proc_tabMailSu bscribers_Updat e" >> > cmdUpdate.Comma ndType = >> CommandType.Sto redProcedure >> > cmdUpdate.Param eters.Add
("@MailSubscrib erID", >> >SqlDbType.Uniq ueIdentifier) >> > cmdUpdate.Param eters.Add("@Hot elID", >> SqlDbType.Int) >> > cmdUpdate.Param eters.Add("@Nam e", >> SqlDbType.VarCh ar) >> > cmdUpdate.Param eters.Add("@Sur name", >> SqlDbType.VarCh ar) >> > cmdUpdate.Param eters.Add("@Des ignation", >> SqlDbType.VarCh ar) >> > cmdUpdate.Param eters.Add("@Ema ilAddress", >> SqlDbType.VarCh ar) >> > cmdUpdate.Param eters.Add("@Upd atedBy", >> SqlDbType.Int) >> > >> > 'Update >> > cmdUpdate.Param eters ("@MailSubscrib erID").Value = >> >DGMailSubscrib ers.DataKeys(e. Item.ItemIndex) >> > cmdUpdate.Param eters("@HotelID ").Value = >> >CType(e.Item.F indControl("ddl HotelID"), >> DropDownList).S electedItem.Val ue() >> > cmdUpdate.Param eters("@Name"). Value = >> >CType(e.Item.C ells(0).Control s(0), TextBox).Text >> > cmdUpdate.Param eters("@Surname ").Value = >> >CType(e.Item.C ells(1).Control s(0), TextBox).Text >> > cmdUpdate.Param eters("@Designa tion").Value
= >> >CType(e.Item.C ells(2).Control s(0), TextBox).Text >> > cmdUpdate.Param eters("@EmailAd dress").Value
= >> >CType(e.Item.C ells(3).Control s(0), TextBox).Text >> > cmdUpdate.Param eters("@Updated By").Value = Session >> ("LoginID") >> > >> > Try >> > cmdUpdate.Execu teNonQuery() >> > Message.InnerHt ml = "<b>Record >> Updated.</b><br>" >> > >> > 'DGMailSubscrib ers.EditItemInd ex = -5 >> > Catch ex As SqlException >> > Message.InnerHt ml = "ERROR: Could not update >> record," _ >> > & " please ensure the fields are correctly
filled out." >> > Message.Style(" color") = "red" >> > >> > End Try >> >. >> > >> >. > . This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: serge |
last post by:
I have a scenario where two tables are in a One-to-Many relationship
and I need to move the data from the Many table to the One table so
that it becomes a One-to-One relationship.
I need to salvage the records from the many table and without going
into detail, one of the reasons I can't do the opposite as
there are records in the ONE table that I need to keep even if they
don't have any child records in the MANY table.
Below I created...
|
by: Philip Boonzaaier |
last post by:
I want to be able to generate SQL statements that will go through a list of
data, effectively row by row, enquire on the database if this exists in the
selected table- If it exists, then the colums must be UPDATED, if not, they
must be INSERTED.
Logically then, I would like to SELECT * FROM <TABLE>
WHERE ....<Values entered here>, and then IF FOUND
UPDATE <TABLE> SET .... <Values entered here> ELSE
INSERT INTO <TABLE> VALUES <Values...
|
by: pmud |
last post by:
Hi,
I have an ASP.NET application using C# code. I am using a datagrid to
display records from a database based on a user input, i.e a user enters a
compnay name in text box & when he clicks a "Submit" button, only those
records are displayed where company name matches with user input.
I need the datagrid to be editable. For this i used the walkthrough from
..NET "Using a datagrid for reading & writing data to database" or it was some...
|
by: Reney |
last post by:
I have a very weird problem in updating my datagrid. Please help me to solve
it. The datagrid is tied to a dataset table with five columns. Three of them
are primary key and the other two columns are the ones that are of interest.
These
two columns have "date" type values and shows short time (i.e. 11:39).
The program updates the database if I change the values in these columns, or
add a new record, or
delete a record. But, if one of...
|
by: Manish |
last post by:
Hello Everyone
I am having weird problem in my datagrid bounded to datatable. My
datatable is populated from SQLServer database. DataGrid has
Calculated column Week% and Calculated record, SPLH.
An example of datagrid is
Category Amount Week%
Revenue 200
Cost 60 30
Hours 100
| |
by: Agnes |
last post by:
I got a table which without primary key.
case 1) I can process insertcommand and daMytable.update..etc
case 2) However, if i process updatecommand , As I run daMytable.update .It
returns error said..."cannot process update command without .... key"
I understand I didn't set primary key (is my fault).But why case 1) can
process very well ???
|
by: Larry Woods |
last post by:
I have a datagrid that is carrying all fields of a record...except one. Now
I want to update the underlying database via a dataadapter. The update is
working but the field that is "left out" is not there, of course. How do I
get that field back into the datatable for the database update?
TIA,
Larry Woods
|
by: mursyidatun ismail |
last post by:
Dear all,
database use: Ms Access.
platform: .Net
i'm trying to update a record/records in a table called t_doctors by
clicking da edit link provided in the database.
when i ran through da browsers and click update it gave me this error:
Specified argument was out of the range of valid values. Parameter name:
|
by: Grant McLean |
last post by:
Hi
First a simple question ...
I have a table "access_log" that has foreign keys "app_id" and
"app_user_id" that reference the "application_type" and "app_user"
tables. When I insert into "access_log", the referential integrity
triggers generate these queries:
SELECT 1 FROM ONLY "public"."application_type" x
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |