Hi, I'm trying to update a gridview and its underlying sqldatabase from form controls (textbox, checkbox etc.)
I first create the records directly from the form to the database using vb.net code. After the page is refreshed the gridview shows the new entry.
My gridview allows me to select a record via a 'Select' hyperlink on each row. the code behind for this is as follows
Protected Sub gridviewExpenseList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gridviewExpenseList.SelectedIndexChanged
Dim Index As Integer
Dim Row As GridViewRow
Index = gridviewExpenseList.SelectedIndex
Row = gridviewExpenseList.Rows(Index)
'retrieve data for selected row, display it in the form view
'make delete button visible
'set the hidden field to the row index
ExpenseID.Value = Index.ToString
'load form data from selected row
textboxExpenseDescription.Text = Row.Cells(1).Text.ToString
textboxDate.Text = Row.Cells(2).Text.ToString
dropdownlistExpenseType.SelectedIndex = CInt(Row.Cells(3).Text)
textboxAmount.Text = gridviewExpenseList.Rows(Index).Cells(4).Text.ToString
textboxEmployerContribution.Text = Row.Cells(5).Text.ToString
checkboxHaveReceipt.Checked = CType(Row.Cells(6).FindControl("CheckBox1"), CheckBox).Checked
'checkboxHaveReceipt.Checked = CBool(gridviewExpenseList.Rows(Index).Cells(6)..FindControl("CheckBox1")).Checked)
buttonDelete.Visible = True
End Sub
The form can then be edited and resubmitted via a SAVE button. the event code is as follows:
Protected Sub buttonSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonSave.Click
'
' save the contents of the form to the database
' will need to check if it already exists using the ExpenseID hidden field
' if exists update if not insert. Return to the home page
'
If ExpenseID.Value = "-1" AndAlso _
textboxExpenseDescription.Text <> "" AndAlso _
textboxDate.Text <> "" AndAlso _
textboxAmount.Text <> "" AndAlso _
textboxEmployerContribution.Text <> "" Then
gridviewExpenseList.UpdateRow(CInt(ExpenseID.Value), False)
Dim oIncidentalExpense As IncidentalExpenses
oIncidentalExpense = New IncidentalExpenses(textboxExpenseDescription.Text, _
textboxDate.Text, _
dropdownlistExpenseType.SelectedIndex, _
CDec(textboxAmount.Text), _
CDec(textboxEmployerContribution.Text), _
CBool(checkboxHaveReceipt.Checked))
oIncidentalExpense.AddExpense(My.User.Name)
Else
'save form data to selected row
Dim Row As GridViewRow
Dim Index As Integer = CInt(ExpenseID.Value)
Row = gridviewExpenseList.Rows(Index)
Row.RowState = DataControlRowState.Edit
Row.Cells(1).Text = textboxExpenseDescription.Text
Row.Cells(2).Text = textboxDate.Text
Row.Cells(3).Text = dropdownlistExpenseType.SelectedIndex.ToString
Row.Cells(4).Text = textboxAmount.Text
Row.Cells(5).Text = textboxEmployerContribution.Text
CType(Row.Cells(5).FindControl("CheckBox1"), CheckBox).Checked = checkboxHaveReceipt.Checked
'gridviewExpenseList.UpdateRow(Index, False)
Row.RowState = DataControlRowState.Normal
End If
End Sub
please note the comment out gridviewExpenseList.UpdateRow(Index,False). This cause a syntax error at runtime (I am not sure why as I though all syntax errors would be caught at buildtime.
After the page is refreshed and the page load event fires:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack Then
gridviewExpenseList.DataSourceID = "IncidentalExpenses"
gridviewExpenseList.DataBind()
Else
'Expense ID -1 indicates a new record, where as a positive integer means existing record
ExpenseID.Value = "-1"
End If
End Sub
The gridview is updated. I guess this is done by the databind statement. Howver When I check the database it has not been updated with the new values and a subsequent page refresh causes the old values to be retored in the gridview.
By the way The sql datasource used to bind the gridview to the database contains both an sql SELEC Tstatement and an UPDATE statement
SELECT ExpenseDescription, ExpenseDate, ExpenseType, ExpenseAmount, EmployerContribution, HaveReceipt, IncidentalExpenseID, ClientID FROM IncidentalExpenses
UPDATE IncidentalExpenses SET ExpenseDescription =, ExpenseDate =, ExpenseType =, ExpenseAmount =, EmployerContribution =, HaveReceipt = WHERE (ClientID = @ClientID) AND (IncidentalExpenseID = @IncidentalExpensesID)
:-/
Can anyone help?