View Full Version : DataGrid Update Problem

Jul 3rd, 2002, 08:09 PM
I've got my databound grid setup and working properly. I've got the Edit, Update, Cancel stuff all setup and working almost.

The only problem is on the 'Update'. The database is being updated properly, but after the update my page isn't being refreshed and I'm at a loss as to why this is happening. I apologize if the following code is too long, but I figured I should put it all here:

----------------------------BEGIN CODE--------------------------------------------------------
Public Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
End If
End Sub

Public Sub BindData()
'Specify our SQL statement
Dim strSQL as String = "SELECT ID, Name, UserName, ReadOnly, ReadSubmit FROM tblUsers ORDER BY UserName"
Dim objConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("pcr.mdb"))
Dim objCommand As New OleDbDataAdapter(strSQL, objConnection)

Dim ds As DataSet = New DataSet()

objCommand.Fill(ds, "tblUsers")
UserInfo.DataSource = ds.Tables("tblUsers").DefaultView
End Sub

Public Sub DataGrid_Update(Source As Object, E As DataGridCommandEventArgs)
Dim connString as String
connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _

Dim objConnection as OleDbConnection
objConnection = New OleDbConnection(connString)
objConnection.Open() 'open the connection

Dim txtName As TextBox = E.Item.Cells(2).Controls(0)
Dim txtUserName As TextBox = E.Item.Cells(3).Controls(0)
Dim txtReadOnly As TextBox = E.Item.Cells(4).Controls(0)
Dim txtReadSubmit As TextBox = E.Item.Cells(5).Controls(0)
Dim strUpdateStmt As String
Dim rOnly, rSubmit

'Used to change the Text property for the numeric value needed for the Yes/No field
If txtReadOnly.Text = "True" Then
rOnly = -1
ElseIf txtReadOnly.Text = "False" Then
rOnly = 0
End If

'Used to change the Text property for the numeric value needed for the Yes/No field
If txtReadSubmit.Text = "True" Then
rSubmit = -1
ElseIf txtReadSubmit.Text = "False" Then
rSubmit = 0
End If

'Update SQL Statement
strUpdateStmt = "UPDATE tblUsers SET " & _
"UserName = '" & txtUserName.Text & "', " & _
"Name = '" & txtName.Text & "', " & _
"ReadOnly = " & rOnly & ", " & _
"ReadSubmit = " & rSubmit & " " & _
"WHERE ID = " & E.Item.Cells(1).Text

'Create the Command object
Dim objCommand as OleDbCommand
objCommand = New OleDbCommand(strUpdateStmt, objConnection)


UserInfo.EditItemIndex = -1
End Sub

Jul 4th, 2002, 03:00 PM
Nevermind. Through searching, trial and error I got it figured out. I had to add an objConnection.Close() statement right after my ExecuteNonQuery() statement and the datagrid now updates and refreshes properly.