ADO/ADO.NET - GridView updating - nothing happens

Asked By Christopher Baker on 07-May-09 11:03 AM

I have a gridview. When I click the Delete button, it works. When I click the Update button - nothing happens.

< asp :GridView ID="grdTransactions" runat="server" AutoGenerateColumns="False"Font-Names="Verdana" Font-Size="Small" BorderColor="Black" BorderStyle="Solid" Caption="Transactions" AllowPaging="True" AllowSorting="True" DataKeyNames="REC_ID" DataSourceID="dsTransactions" AutoGenerateDeleteButton="false" AutoGenerateSelectButton="False" >

<AlternatingRowStyle BackColor="#E0E0E0" Font-Names="Verdana" Font-Size="Small" />

<SelectedRowStyle CssClass="gridViewSelectedRow" />

<Columns>

<asp:TemplateField HeaderText="ACCOUNT" SortExpression="ACCOUNT_NAME">

<EditItemTemplate>

<asp:DropDownList ID="lstAccountName" runat="server" DataSourceID="dsAccounts"

DataTextField="ACCOUNT_NAME" DataValueField="ACCOUNT_ID"

SelectedValue='<%# Bind("Account_ID") %>'>

</asp:DropDownList>

</EditItemTemplate>

<ItemTemplate>

<asp:label ID="lblAccountName" runat="server" Text='<%# Bind("ACCOUNT_NAME") %>' />

</ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

<ItemStyle Width="150px" />

</asp:TemplateField>

<asp:TemplateField HeaderText="TRANSACTION DATE" SortExpression="TRANSACTION_DATE">

<EditItemTemplate>

<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server"

TargetControlID="txtTransactionDate" />

<asp:TextBox ID="txtTransactionDate" runat="server"

Text='<%# Bind("TRANSACTION_DATE", "{0:MM-dd-yyyy}") %>' />

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="lblTransactionDate" runat="server" Text='<%# Bind("TRANSACTION_DATE", "{0:MM-dd-yyyy}") %>'></asp:Label>

</ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

<ItemStyle Width="150px" />

</asp:TemplateField>

<asp:TemplateField HeaderText="PAYEE" SortExpression="PAYEE_NAME">

<EditItemTemplate>

 <asp:DropDownList ID="lstPayeeName" runat="server" DataSourceID="dsPayees"

DataTextField="PAYEE_NAME" DataValueField="PAYEE_ID"

SelectedValue='<%# Bind("Payee_ID") %>'>

</asp:DropDownList>

</EditItemTemplate>

<ItemTemplate>

<asp:label ID="lblPayeeName" runat="server" Text='<%# Bind("PAYEE_NAME") %>'></asp:label>

</ItemTemplate>

<HeaderStyle HorizontalAlign="Left" />

<ItemStyle Width="150px" />

</asp:TemplateField>

<asp:TemplateField HeaderText="AMOUNT" SortExpression="AMOUNT">

<EditItemTemplate>

<asp:TextBox ID="txtEditAmount" runat="server" Text='<%# Bind("AMOUNT") %>' />

<ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender2" runat="server"

TargetControlID="txtEditAmount" Mask="99,999.99"

MessageValidatorTip=true

OnFocusCssClass="MaskedEditFocus"

OnInvalidCssClass="MaskedEditError"

MaskType="Number" DisplayMoney="Left"

ClearTextOnInvalid="true" InputDirection="RightToLeft" />

<ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator2" runat="server"

ControlToValidate="txtEditAmount"

ControlExtender="MaskedEditExtender2"

Display=dynamic ForeColor="Blue"

TooltipMessage="Enter transaction amount"

IsValidEmpty=false EmptyValueMessage="Amount is required" InvalidValueMessage="Invalid value" Font-Names="Verdana" />

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="lblEditAmount" runat="server" Text='<%# Bind("AMOUNT", "{0:$#,###.##}") %>'></asp:Label>

</ItemTemplate>

<HeaderStyle HorizontalAlign="Right" />

<ItemStyle HorizontalAlign="Right" Width="100px" />

</asp:TemplateField>

<asp:TemplateField HeaderText="PAID VIA" SortExpression="DESCRIPTION">

<EditItemTemplate>

 <asp:DropDownList ID="lstPayBasis" runat="server" DataSourceID="dsCodeNames"

DataTextField="DESCRIPTION" DataValueField="CODE"

SelectedValue='<%# Bind("PAY_BASIS") %>'>

</asp:DropDownList>

</EditItemTemplate>

<ItemTemplate>

<asp:label ID="lblDescription" runat="server" Text='<%# Bind("DESCRIPTION") %>'></asp:label>

</ItemTemplate>

<HeaderStyle HorizontalAlign="Center" />

<ItemStyle Width="100px" />

</asp:TemplateField>

<asp:CheckboxField DataField="OFF_BUDGET" HeaderText="OFF BUDGET" HeaderStyle-HorizontalAlign="Center"

ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px" SortExpression="OFF_BUDGET" >

<HeaderStyle HorizontalAlign="Center" />

<ItemStyle HorizontalAlign="Center" Width="100px" />

</asp:CheckboxField>

<asp:CheckboxField DataField="DEDUCT_FROM_BA" HeaderText="DEDUCT BA" HeaderStyle-HorizontalAlign="Center"

ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px" SortExpression="DEDUCT_FROM_BA" >

<HeaderStyle HorizontalAlign="Center" />

<ItemStyle HorizontalAlign="Center" Width="100px" />

</asp:CheckboxField>

<asp:BoundField DataField="COMMENT" HeaderText="COMMENT" SortExpression="COMMENT" />

<asp:CommandField ButtonType="Image"

EditImageUrl="~/graphics/pencil.JPG"

CancelImageUrl="~/graphics/cancel.JPG"

UpdateImageUrl="~/graphics/OK.JPG"

ShowDeleteButton="True"

DeleteImageUrl="~/graphics/delete.jpg" ShowEditButton="True" />

</Columns>

</asp:GridView>



 

Here is the DataSource to which the gridview is bound

<asp:ObjectDataSource ID="dsTransactions" runat="server" OldValuesParameterFormatString="original_{0}"

SelectMethod="GetTransactionsByDateRange" TypeName="TransactionsBLL"

DeleteMethod="DeleteTransactionsByID" UpdateMethod="UpdateTransaction">

<SelectParameters>

<asp:ControlParameter ControlID="txtStartDate" Name="pStartDate" PropertyName="Text"

Type="DateTime" />

<asp:ControlParameter ControlID="txtEndDate" Name="pEndDate" PropertyName="Text"

Type="DateTime" />

</SelectParameters>

<DeleteParameters>

<asp:Parameter Name="original_REC_ID" Type="Int32" />

</DeleteParameters>

<UpdateParameters>

<asp:Parameter Name="original_REC_ID" type="Int32" />

<asp:Parameter Name="pAccountID" Type="Int32" />

<asp:Parameter Name="pTransactionDate" Type="DateTime" />

<asp:Parameter Name="pAmount" Type="Single" />

<asp:Parameter Name="pComment" Type="String" />

<asp:Parameter Name="pPayeeID" Type="Int32" />

<asp:Parameter Name="pPayBasis" Type="Int32" />

<asp:Parameter Name="pOffBudget" Type="Byte" />

<asp:Parameter Name="pDeductBA" Type="Byte" />

</UpdateParameters>

</asp:ObjectDataSource>


Here is the BLL to which the DataSource is bound

Imports Microsoft.VisualBasic

Imports budget

<System.ComponentModel.DataObject()> _

Public Class TransactionsBLL

Private transactionsAdapter As New budgetTableAdapters.TRANSACTIONSTableAdapter

Protected ReadOnly Property Adapter() As budgetTableAdapters.TRANSACTIONSTableAdapter

Get

If transactionsAdapter Is Nothing Then

transactionsAdapter = New budgetTableAdapters.TRANSACTIONSTableAdapter

End If

Return transactionsAdapter

End Get

End Property

<System.ComponentModel.DataObjectMethodAttribute _

(System.ComponentModel.DataObjectMethodType.Select, True)> _

Public Function GetTransactionsByDateRange(ByVal pStartDate As Date, ByVal pEndDate As Date) As budget.TRANSACTIONSDataTable

Return Adapter.GetTransactionsByDateRange(pStartDate, pEndDate)

End Function

<System.ComponentModel.DataObjectMethodAttribute _

(System.ComponentModel.DataObjectMethodType.Select, False)> _

Public Function GetTransactionsByID(ByVal pTransactionID As Integer) As budget.TRANSACTIONSDataTable

Return Adapter.GetTransactionsByID(pTransactionID)

End Function

<System.ComponentModel.DataObjectMethodAttribute _

(System.ComponentModel.DataObjectMethodType.Delete, False)> _

Public Sub DeleteTransactionsByID(ByVal original_REC_ID As Integer)

Adapter.DeleteTransactionByID(original_REC_ID)

End Sub

<System.ComponentModel.DataObjectMethodAttribute _

(System.ComponentModel.DataObjectMethodType.Update, False)> _

Public Sub UpdateTransaction(ByVal original_REC_ID As Integer, ByVal pAccountID As Integer, _

ByVal pTransactionDate As DateTime, ByVal pAmount As Single, ByVal pComment As String, _

ByVal pPayeeID As Integer, ByVal pPayBasis As Integer, ByVal pOffBudget As Byte, ByVal pDeductBA As Byte)

MsgBox("Updating")

Adapter.UpdateTransaction(original_REC_ID, pAccountID, pTransactionDate, pAmount, pComment, _

pPayeeID, pPayBasis, pOffBudget, pDeductBA)

End Sub

End Class

Ravenet Rasaiyah replied to Christopher Baker on 07-May-09 11:21 AM
Hi

Is trigger the event or not updating database? because  of i look your code it look fine majorly.

thank you

try

Santhosh N replied to Christopher Baker on 07-May-09 11:22 AM
Put a break point inside the update function and check if it is invoked...

there's a msgbox there...

Christopher Baker replied to Santhosh N on 07-May-09 11:39 AM

That's why I put the MsgBox there. BTW the MsgBox never fires


Also the Cancel button works fine

here you go...
Santhosh N replied to Christopher Baker on 07-May-09 11:52 AM
check here how to use this...

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.update.aspx
i thought...
Christopher Baker replied to Santhosh N on 07-May-09 12:07 PM

...that the gridview, once enabled for editing, fully lauched the data source's update method. Is that not true? If I have to fire the Update() method, in which handler do I do so? Each row of the gridview has its own update button.


 I am confused ;)


Can you check the link and make sure if it is fine...
Santhosh N replied to Christopher Baker on 07-May-09 12:08 PM
end of post
Please try these.
[)ia6l0 iii replied to Christopher Baker on 07-May-09 12:53 PM
1. Prefix your updateTransaction method with the DataObjectMethod update property like,

<DataObjectMethod(DataObjectMethodType.Update)>
public Sub UpdateTransaction(ByVal original_REC_ID As Integer, ByVal pAccountID As Integer, _

2. You could use an instance of the TransactionsBLL class itself as a single parameter of your update method and remove all the parameters

like,
public Sub UpdateTransaction(tblobject As TransactionsBLL )

3. Please check if the parameter list is correct and complete.

I believe there is a signature difference. The Objectdatasource's update method requires the parameter list that is present in the businessobject (TransactionsBLL) and all the parameters of that object should be listed in function.
interesting
Christopher Baker replied to [)ia6l0 iii on 07-May-09 03:08 PM

The business object executes this stored procedure. Can you explain how I would code the UpdateTransaction method if I pass it a TransactionBLL?


Thanks


set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

go


ALTER PROCEDURE [dbo].[sp_UPDATE_TRANSACTION]

@REC_ID int,

@ACCOUNT_ID int,

@TRANSACTION_DATE smalldatetime,

@AMOUNT float,

@COMMENT varchar(50),

@PAYEE_ID int,

@PAY_BASIS tinyint,

@OFF_BUDGET bit,

@DEDUCT_FROM_BA bit

AS

BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from

-- interfering with SELECT statements.

SET NOCOUNT ON;

UPDATE [Budget].[dbo].[TRANSACTIONS]

SET [ACCOUNT_ID] = @ACCOUNT_ID,

[TRANSACTION_DATE] = @TRANSACTION_DATE,

[AMOUNT] = @AMOUNT,

[COMMENT] = @COMMENT,

[PAYEE_ID] = @PAYEE_ID,

[PAY_BASIS] = @PAY_BASIS,

[OFF_BUDGET] = @OFF_BUDGET,

[DEDUCT_FROM_BA] = @DEDUCT_FROM_BA

WHERE (REC_ID=@REC_ID)

END

still not working
Christopher Baker replied to Christopher Baker on 13-May-09 07:59 AM

I have been unable to work on this for a while, and it is still not working. I have replaced the edit and delete buttons command field with template fields:

(old)

<asp:CommandField ButtonType="Image"

EditImageUrl="~/graphics/pencil.JPG"

CancelImageUrl="~/graphics/cancel.JPG"

UpdateImageUrl="~/graphics/OK.JPG"

ShowDeleteButton="True"

DeleteImageUrl="~/graphics/delete.jpg" ShowEditButton="True" />

(new)

<asp:TemplateField ShowHeader="False">

<EditItemTemplate>

<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True" CommandName="Update"

ImageUrl="~/graphics/OK.JPG" />

 

<asp:ImageButton ID="ImageButton2"

runat="server" CausesValidation="False" CommandName="Cancel"

ImageUrl="~/graphics/cancel.JPG" />

</EditItemTemplate>

<ItemTemplate>

<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False" CommandName="Edit"

ImageUrl="~/graphics/pencil.JPG"

OnClientClick="return confirm('are you sure you want to edit this transaction?');"/>

  

<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" CommandName="Delete"

ImageUrl="~/graphics/delete.jpg"

OnClientClick="return confirm('are you sure you want to delete this transaction?');"/>

</ItemTemplate>

</asp:TemplateField>

and now the problems are even worse. The javascript confirm fires but neither the delete nor the update works. In fact, upon clicking the Update button the  OK and Cancel buttons do not appear. Can anyone give me a primer in how this works? I'm a ADO.NET noob and am rapidly getting more and more lost.


Thanks

bump
Christopher Baker replied to Christopher Baker on 13-May-09 09:20 AM
bump