Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems updating from a Gridview

Status
Not open for further replies.

MzKitty

Programmer
Oct 28, 2003
254
0
0
US
Hi. I am having problems getting a record to update from a gridview. I'm using vs2008 sp 1. Here is my update query:

UPDATE VehicleTare SET NotesArea = ? WHERE (LocationID = LocationID) AND (VehicleID = VehicleID)

I have defined the two parameters LocationID and VehicleID, so it is passing the correct values. I've tried trapping the new value in NotesArea in a string, but I can't get the syntax so it passes the new value. Here is my code for it (in VB)
Private Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim count As New Integer
strLocID = ""
strVehicle = ""
Try
'Dim row As GridViewRow
count = e.RowIndex
strVehicle = (GridView1.Rows(count).Cells(2).Text)
strNotes = (GridView1.Rows(count).Cells(7).Text) ' this should be the new value)
txtVehicleID.Text = strVehicle

Catch EX As Exception
End Try

End Sub
I would appreciate any help and yes I have googled it.
Thanks
Cathy
 
How are you populating the grid? Are you using a datasource control, or manualy binding to a dataset or datatable?
 
I'm using a datasource control that is set up as a System.Data.Odbc. The backend is a PSql database.

Cathy
 
1st problem: use of datasource controls. they are no good as you cannot test or debug them.

2nd problem: inline editing. While this seems like a nice feature it creates complexity rather than reducing it.

I would recommend using the grid as a readonly view with links to edit screens. the edit screens can be just that, edit a record. If you want to inline feel you can use an ajax library to load the edit screen into a modal and preform the edit.

This means more forms, but each form has one responsibility. This will be much easier to maintain in the long run.

as a side note, with a readonly grid there is less viewstate overhead using a repeater/listview to render the html opposed to the gridview.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Since you are using a datasource control, which I strongly recommend you don't, you can use the e.NewValues and e.OldValues collections in the RowEditing event.
 
Try something like this:

Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim strVehicle As String = DirectCast(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
Dim strNotes As String = DirectCast(GridView1.Rows(e.RowIndex).Cells(7).Controls(0), TextBox).Text
Try
Con.Open()
Dim sql As New StringBuilder(1024)
Dim objcmd As New OdbcCommand(Sql.ToString, Con)
objcmd.CommandText = "UPDATE VehicleTare SET NotesArea =('" & strNotes & "')WHERE (LocationID = LocationID) AND (VehicleID = VehicleID)"
objcmd.CommandType = CommandType.Text
objcmd.ExecuteNonQuery()
Con.Close()
Catch ex As Exception
End Try
End Sub

Passing the value strNotes in this way is not recomended, but it will help to get you going.

Thanks,
Chilly442
---------------------------------------
If I lived anywhere else I'd be Sunny442
 
thanks for everyone's help, but I dumped the data source and the gridview and changed the way I was going. This learn as you go can be very frustating for and old Cobol programmer.

Thanks All.
Cathy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top