Whats wrong with this code ? (Datagrid Update)

pabby

Member
Joined
Nov 8, 2005
Messages
9
Programming Experience
Beginner
HI I have a datagrid that is not updating - when you click the "Update " link, it just reverts back to the data it got at Page Load and comes out of edit mode. I have tried the if not Page.IsPostBack - but it made no difference (Unless I put it in the wrong place) - it is for an ASP.NET web form. Also, I am a beginner. Can someone have a look at the code, please ? This is for the update part of it - the edit and cancel part seem to work ok.

thanks.

Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
Dim ID, DayOfTheWeek, Whereabouts As String
ID = CType(e.Item.Cells(1).Controls(0), TextBox).Text
DayOfTheWeek =
CType(e.Item.Cells(2).Controls(0), TextBox).Text
Whereabouts =
CType(e.Item.Cells(3).Controls(0), TextBox).Text

Dim r As DataSet1.tbljfoleyRow
r = DataSet11.tbljfoley.FindByID(key)
r.DayOfTheWeek = DayOfTheWeek
r.Whereabouts = Whereabouts
r.ID = ID


SqlDataAdapter1.Update(DataSet11)

DataGrid1.DataBind()





DataGrid1.EditItemIndex = -1


DataGrid1.DataBind()
 
Moved thread to ASP.Net forum. Please post in most appropriate forum and provide meaningful thread titles. "What's wrong?" isn't particularly meaningful.
 
The problem is unless you stop editing, it wont allow an update...

VB.NET:
[LEFT][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].YourBindingSource.EndEdit()
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].SqlDataAdapter.Update([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].YourDataSet)
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MsgBox(ex.ToString[/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][/LEFT]

If you are using .Net 1.1 then you will need;

VB.NET:
[LEFT][COLOR=#0000ff]Me[/COLOR][SIZE=2].BindingContext(DsYourDataSet, "DbTableName").EndCurrentEdit()[/SIZE]
[/LEFT]
 
Back
Top