D
D. Shane Fowlkes
I have a Datagrid which in theory, should allow you to edit and update the
records. I've stripped my test page down so that it's only attempting to
update one field - "description". Yet when I make the changes and click
Update, it reverts back to the old value. I'm stumped! I've also checked
the db table and it is infact not updating. I'm attempting to follow and
example right out of ASP.NET Unleashed but not having much luck. Any
pointers would be appreciated.
My Update Sub Routine:
**************************************************
Sub dgLineItems_UpdateCommand(S As Object, E As DataGridCommandEventArgs)
Dim intReqID As Integer
intReqID = CInt(Request.QueryString("id"))
If intReqID < 1 Then
Response.Redirect("reqform.aspx")
End If
Dim conSqlServer As SqlConnection
Dim strUpdateSQL As String
Dim cmdUpdate As SqlCommand
Dim strConnectString As String
Dim intLineItemID As Integer
Dim txtDescription As TextBox
Dim strDescription As String
intLineItemID = dgLineItems.DataKeys(E.Item.ItemIndex)
txtDescription = E.Item.Cells(1).Controls(0)
strDescription = txtDescription.Text
strConnectString = "server=drpt-server3; database=DRPT-TEST; uid=xxx"
strUpdateSQL = "UPDATE POItems SET Description = '" & strDescription & "'
WHERE ID = " & intLineItemID
conSqlServer = New SqlConnection(strConnectString)
cmdUpdate = New SqlCommand(strUpdateSQL, conSqlServer)
conSqlServer.Open()
cmdUpdate.ExecuteNonQuery()
conSqlServer.Close()
dgLineItems.EditItemIndex = -1
CreateLineItemDisplay(intReqID)
lblTest.Text = strDescription
End Sub
**************************************************
My DataGrid:
**************************************************
<aspataGrid id="dgLineItems"
runat="server"
AutoGenerateColumns="false"
BorderWidth="1"
BorderColor="#36667C"
CellPadding="3"
HeaderStyle-CssClass="colheader"
Width="100%"
AlternatingItemStyle-BackColor="#CCCCCC"
DataKeyField="ID"
ItemStyle-CssClass="caption"
OnEditCommand="dgLineItems_EditCommand"
OnCancelCommand="dgLineItems_CancelCommand"
OnDeleteCommand="dgLineItems_DeleteCommand"
OnUpdateCommand="dgLineItems_UpdateCommand">
<columns>
<asp:BoundColumn HeaderText=" " ItemStyle-Width="5%"
ReadOnly="true" />
<asp:BoundColumn HeaderText="Brief Description" DataField="Description"
ItemStyle-Width="35%" ItemStyle-CssClass="caption" />
<asp:BoundColumn HeaderText="QTY" DataFormatString="{0:N0}"
DataField="Quantity" ItemStyle-Width="5%" ItemStyle-CssClass="caption" />
<asp:BoundColumn HeaderText="Unit of Measure" DataField="UOM"
ItemStyle-Width="15%" ItemStyle-CssClass="caption" />
<asp:BoundColumn HeaderText="Unit Price" DataFormatString="{0:C2}"
DataField="UnitPrice" ItemStyle-Width="15%" ItemStyle-CssClass="caption" />
<asp:BoundColumn HeaderText="Total Cost" DataFormatString="{0:C2}"
DataField="AllocAmt" ItemStyle-Width="15%" ItemStyle-CssClass="caption"
ReadOnly="true" />
<asp:EditCommandColumn HeaderText="Edit" EditText="Edit"
UpdateText="Update" CancelText="Cancel" />
<asp:BoundColumn HeaderText=" " ItemStyle-Width="5%"
ReadOnly="true" />
</columns>
</aspataGrid>
**************************************************
records. I've stripped my test page down so that it's only attempting to
update one field - "description". Yet when I make the changes and click
Update, it reverts back to the old value. I'm stumped! I've also checked
the db table and it is infact not updating. I'm attempting to follow and
example right out of ASP.NET Unleashed but not having much luck. Any
pointers would be appreciated.
My Update Sub Routine:
**************************************************
Sub dgLineItems_UpdateCommand(S As Object, E As DataGridCommandEventArgs)
Dim intReqID As Integer
intReqID = CInt(Request.QueryString("id"))
If intReqID < 1 Then
Response.Redirect("reqform.aspx")
End If
Dim conSqlServer As SqlConnection
Dim strUpdateSQL As String
Dim cmdUpdate As SqlCommand
Dim strConnectString As String
Dim intLineItemID As Integer
Dim txtDescription As TextBox
Dim strDescription As String
intLineItemID = dgLineItems.DataKeys(E.Item.ItemIndex)
txtDescription = E.Item.Cells(1).Controls(0)
strDescription = txtDescription.Text
strConnectString = "server=drpt-server3; database=DRPT-TEST; uid=xxx"
strUpdateSQL = "UPDATE POItems SET Description = '" & strDescription & "'
WHERE ID = " & intLineItemID
conSqlServer = New SqlConnection(strConnectString)
cmdUpdate = New SqlCommand(strUpdateSQL, conSqlServer)
conSqlServer.Open()
cmdUpdate.ExecuteNonQuery()
conSqlServer.Close()
dgLineItems.EditItemIndex = -1
CreateLineItemDisplay(intReqID)
lblTest.Text = strDescription
End Sub
**************************************************
My DataGrid:
**************************************************
<aspataGrid id="dgLineItems"
runat="server"
AutoGenerateColumns="false"
BorderWidth="1"
BorderColor="#36667C"
CellPadding="3"
HeaderStyle-CssClass="colheader"
Width="100%"
AlternatingItemStyle-BackColor="#CCCCCC"
DataKeyField="ID"
ItemStyle-CssClass="caption"
OnEditCommand="dgLineItems_EditCommand"
OnCancelCommand="dgLineItems_CancelCommand"
OnDeleteCommand="dgLineItems_DeleteCommand"
OnUpdateCommand="dgLineItems_UpdateCommand">
<columns>
<asp:BoundColumn HeaderText=" " ItemStyle-Width="5%"
ReadOnly="true" />
<asp:BoundColumn HeaderText="Brief Description" DataField="Description"
ItemStyle-Width="35%" ItemStyle-CssClass="caption" />
<asp:BoundColumn HeaderText="QTY" DataFormatString="{0:N0}"
DataField="Quantity" ItemStyle-Width="5%" ItemStyle-CssClass="caption" />
<asp:BoundColumn HeaderText="Unit of Measure" DataField="UOM"
ItemStyle-Width="15%" ItemStyle-CssClass="caption" />
<asp:BoundColumn HeaderText="Unit Price" DataFormatString="{0:C2}"
DataField="UnitPrice" ItemStyle-Width="15%" ItemStyle-CssClass="caption" />
<asp:BoundColumn HeaderText="Total Cost" DataFormatString="{0:C2}"
DataField="AllocAmt" ItemStyle-Width="15%" ItemStyle-CssClass="caption"
ReadOnly="true" />
<asp:EditCommandColumn HeaderText="Edit" EditText="Edit"
UpdateText="Update" CancelText="Cancel" />
<asp:BoundColumn HeaderText=" " ItemStyle-Width="5%"
ReadOnly="true" />
</columns>
</aspataGrid>
**************************************************