Groups | Blog | Home
all groups > asp.net webcontrols > may 2007 >

asp.net webcontrols : updating values in gridview


Larisa Fak
5/23/2007 12:21:18 PM
Hi!
I am building website scheduling application in Visual Studio. I have a
gridview that displays hour wages and depending of dropdown selection of
type (holiday, regular, etc... ) should be changed actual hourly wages.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:SchedulingConnectionString %>"
DeleteCommand="DELETE FROM [MockInputSchedule] WHERE [ID] =
@ID" InsertCommand="INSERT INTO [MockInputSchedule] ([empid], [code_nc],
[salary], [HourType], [ActHourlyRate]) VALUES (@empid, @code_nc,
@salary, @HourType, @ActHourlyRate)"
SelectCommand="SELECT [ID], [empid], [code_nc], [salary],
[HourType], [ActHourlyRate] FROM [MockInputSchedule] WHERE ([canteen_no]
= @canteen_no)"
UpdateCommand="UPDATE [MockInputSchedule] SET [empid] =
@empid, [code_nc] = @code_nc, [salary] = @salary, [HourType] =
@HourType, [ActHourlyRate] = @ActHourlyRate WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="empid" Type="String" />
<asp:Parameter Name="code_nc" Type="String" />
<asp:Parameter Name="salary" Type="Decimal" />
<asp:Parameter Name="HourType" Type="String" />
<asp:Parameter Name="ActHourlyRate" Type="Decimal" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:SessionParameter Name="canteen_no"
SessionField="canteen" Type="String" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="empid" Type="String" />
<asp:Parameter Name="code_nc" Type="String" />
<asp:Parameter Name="salary" Type="Decimal" />
<asp:Parameter Name="HourType" Type="String" />
<asp:Parameter Name="ActHourlyRate" Type="Decimal" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1" Style="position: relative">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="code_nc" HeaderText="code_nc"
ReadOnly="True" SortExpression="code_nc" />
<asp:BoundField DataField="salary" HeaderText="salary"
ReadOnly="True" SortExpression="salary" />
<asp:TemplateField HeaderText="HourType"
SortExpression="HourType">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1"
runat="server" AutoPostBack="True" Style="position: relative">
<asp:ListItem>R</asp:ListItem>
<asp:ListItem>U</asp:ListItem>
<asp:ListItem>S</asp:ListItem>
<asp:ListItem>OT</asp:ListItem>
<asp:ListItem>H</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("HourType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ActHourlyRate"
HeaderText="ActHourlyRate" SortExpression="ActHourlyRate" />
</Columns>
</asp:GridView>

and in code behind

Public Function ActualHoursRate(ByVal HourType, ByVal salary)

' Calculates actual rate

Dim grv As GridViewRow = Nothing

Dim ActHourRate As Decimal
Dim label1 As Label = Nothing

For Each gvr As GridViewRow In GridView1.Rows

HourType = (CType(grv.FindControl("HourType"),
DropDownList)).SelectedItem.Text
salary = (CType(grv.FindControl("salary"), Label))
Select Case (HourType.Text)
Case ("R")
ActHourRate = salary
Case ("U")
ActHourRate = salary
Case ("S")

ActHourRate = 1.25 * salary
Case ("N")

ActHourRate = 1.08 * salary
Case ("OT")
ActHourRate = 1.5 * salary
Case ("H")
ActHourRate = 2 * salary
End Select
Label1.Text = ActHourRate.ToString
Next gvr

End Function



Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
GridView1.RowUpdating
Dim HourType As String =
CType(GridView1.Rows(e.RowIndex).FindControl("HourType"),
DropDownList).SelectedValue
Dim salary As Double =
(CType(GridView1.Rows(e.RowIndex).FindControl("salary"),
Label)).ToString


SqlDataSource1.UpdateParameters("HourType").DefaultValue = "R"
ActualHoursRate(HourType, salary)
SqlDataSource1.Update()
End Sub

Something is wrong with the code behind, it gives me error:
Object reference not set to an instance of an object.
for line Dim HourType As String =
CType(GridView1.Rows(e.RowIndex).FindControl("HourType"),
DropDownList).SelectedValue
Can you help me?
Thanks!

burlo
5/23/2007 11:50:01 PM
I think it is because your FindControl() function pasts the wrong id. You
have the input id parameter as "HourType" but looking at your code in your
aspx page the id of the dropdownList is "DropDownList1" so this should be
passed in instead.
i.e
Dim HourType As String =
CType(GridView1.Rows(e.RowIndex).FindControl("DropDownList1"),
DropDownList).SelectedValue

[quoted text, click to view]
AddThis Social Bookmark Button