I am posting this for a coworker.
--------------------------------------------------------
The issue i'm having is after i enter all my data into my gridview's
textboxes and dropdownlists the data wont stick. My object data source dumps
to a class but after the update the grid should read from the class as well.
After the update button is clicked all the data passes through the class
like it should and doesn't throw any errors. After all the data moves
through the class and the page reloads, it looks like it's adding a new
line. I am new to using "OOP" so my class's may be a bit off. I was told by
a co-worker that i should have an update method and not use the same insert
method? As this may be the issue, if someone could please take look at my
class and show me what an actual update method is. The commented portion of
my class below was my attempt at building an update method.
All help is greatly appreciated. Thanks!
ASPX code
<asp:GridView ID="pogrid" runat="server"
AutoGenerateCol umns="False"
style="margin-left: 0px; margin-right: 0px; margin-bottom: 0px"
Width="1020px" BorderColor="Bl ack" BorderStyle="So lid"
CellPadding="4"
BackColor="Whit e" BorderWidth="1p x"
ForeColor="Blac k"
GridLines="Vert ical" HorizontalAlign ="Center"
BackImageUrl="~/POTracking/images/buttontemplate. jpg"
Font-Names="Calibri" DataSourceID="O bjectDataSource 1">
<FooterStyle BackColor="#CCC C99" />
<RowStyle BackColor="#E1E 3EC" BorderColor="Bl ack"
BorderStyle="So lid"
BorderWidth="1p x" Font-Names="Calibri"
HorizontalAlign ="Center" />
<Columns>
<asp:CommandFie ld ButtonType="Ima ge"
DeleteImageUrl= "~/POTracking/images/Deletesmall.png "
ShowCancelButto n="False" ShowDeleteButto n="True"
ShowEditButton= "True"
EditImageUrl="~/POTracking/images/Document.png"
ShowInsertButto n="True"
UpdateImageUrl= "~/POTracking/images/Sync.png" />
<asp:TemplateFi eld HeaderText="QTY "
SortExpression= "Quantity">
<headerstyle width="36px" />
<EditItemTempla te>
<asp:TextBox ID="qtybox" runat="server" width="32px"
Text='<%# Bind("Quantity" , "{0}")
%>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="qtylbl" runat="server" Text='<%#
Bind("Quantity" ) %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="40px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Bil l To"
SortExpression= "Billto" >
<headerstyle width="85px" />
<EditItemTempla te>
<asp:DropDownLi st ID="billtobox" runat="server"
DataSourceID="b illto"
DataTextField=" name" datavaluefield= "id"
Width="79px"
SelectedValue=' <%# Bind("Billto", "{0}") %>'
appenddatabound items="true">
<asp:ListItem Value="0">Selec t</asp:ListItem>
</asp:DropDownLis t>
<asp:SqlDataSou rce ID="billto" runat="server"
ConnectionStrin g="<%$
ConnectionStrin gs:POTracking %>" SelectCommand=" billto"
SelectCommandTy pe="StoredProce dure"></asp:SqlDataSour ce>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="billtolbl"
runat="server"> </asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Des cription"
SortExpression= "Descriptio n">
<headerstyle width="454px" />
<EditItemTempla te>
<asp:TextBox ID="descbox" width="450px"
runat="server" Text='<%# Bind("Descripti on") %>'></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="desclbl" runat="server" Text='<%#
Bind("Descripti on") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Typ e" SortExpression= "Type">
<headerstyle width="44px" />
<EditItemTempla te>
<asp:DropDownLi st ID="typebox" runat="server"
width="40px" appenddatabound items="True"
DataSourceID="s eltype" DataTextField=' typeabbr'
DataValueField= "id" SelectedValue=' <%#
Bind("Type", "{0}") %>'>
<asp:ListItem Value="0">Selec t</asp:ListItem>
</asp:DropDownLis t>
<asp:SqlDataSou rce ID="seltype" runat="server"
ConnectionStrin g="<%$
ConnectionStrin gs:POTracking %>" SelectCommand=" type"
SelectCommandTy pe="StoredProce dure"></asp:SqlDataSour ce>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="typelbl" runat="server" Text='<%#
Bind("Type") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="46px" />
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Pro ject"
SortExpression= "project">
<headerstyle width="110px" />
<EditItemTempla te>
<asp:DropDownLi st ID="projbox" runat="server"
DataSourceID="P rojectrd"
DataTextField=" name" DataValueField= "Projectid"
width="106px"
SelectedValue=' <%# Bind("project", "{0}") %>'
appenddatabound items="true">
<asp:ListItem Value="0">Selec t</asp:ListItem>
</asp:DropDownLis t>
<asp:SqlDataSou rce ID="Projectrd" runat="server"
ConnectionStrin g="<%$
ConnectionStrin gs:POTracking %>" SelectCommand=" proj"
SelectCommandTy pe="StoredProce dure"></asp:SqlDataSour ce>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="projlbl" runat="server" Text='<%#
Bind("project") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Uni t Price"
SortExpression= "UnitPrice" >
<headerstyle width="79px" />
<EditItemTempla te>
<asp:TextBox ID="unitpricebo x" width="75px"
runat="server"
Text='<%# Bind("UnitPrice ", "{0:N}") %>'
Height="22px"></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="unitpricelb l" runat="server"
Text='<%# Bind("UnitPrice ") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
<asp:TemplateFi eld HeaderText="Tot al"
SortExpression= "Total">
<headerstyle width="79px" />
<EditItemTempla te>
<asp:TextBox ID="Totalbox" runat="server"
BackColor="Silv er"
BorderColor="Bl ack" Text='<%# Bind("Total",
"{0:N}") %>' ReadOnly="True" ></asp:TextBox>
</EditItemTemplat e>
<ItemTemplate >
<asp:Label ID="totallbl" runat="server" Text='<%#
Bind("Total") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
<PagerStyle BackColor="#F7F 7DE" ForeColor="Blac k"
HorizontalAlign ="Right" />
<SelectedRowSty le BackColor="#CE5 D5A" Font-Bold="True"
ForeColor="Whit e" />
<HeaderStyle Wrap="False" BorderColor="Bl ack"
BorderStyle="So lid"
BorderWidth="1p x" />
<AlternatingRow Style BackColor="#C7C 7C7" />
</asp:GridView>
VB.Net Code
<asp:ObjectData Source ID="ObjectDataS ource1" runat="server"
SelectMethod="A ddPOLines" TypeName="POTra cker.Purchase_O rder"
InsertMethod="A ddPOLines"
EnableCaching=" True" UpdateMethod="A ddPOLines">
<UpdateParamete rs>
<asp:Paramete r Name="Quantity" Type="Int32" />
<asp:Paramete r Name="billto" Type="Int32" />
<asp:Paramete r Name="descripti on" Type="String" />
<asp:Paramete r Name="type" Type="String" />
<asp:Paramete r Name="project" Type="Int32" />
<asp:Paramete r Name="UnitPrice " Type="Double" />
<asp:Paramete r Name="total" Type="Double" />
</UpdateParameter s>
<InsertParamete rs>
<asp:Paramete r Name="Quantity" Type="Int32" />
<asp:Paramete r Name="billto" Type="Int32" />
<asp:Paramete r Name="descripti on" Type="String" />
<asp:Paramete r Name="type" Type="String" />
<asp:Paramete r Name="project" Type="Int32" />
<asp:Paramete r Name="UnitPrice " Type="Double" />
<asp:Paramete r Name="total" Type="Double" />
</InsertParameter s>
</asp:ObjectDataS ource>
<asp:Button ID="add" runat="server" Text="New Row" />
VB.Net
Imports System.Security
Imports System.Security .Principal.Wind owsIdentity
Imports Microsoft.Appli cationBlocks
Partial Public Class create
Inherits System.Web.UI.P age
Private Sub pogrid_RowEditi ng(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewEditEventAr gs) Handles pogrid.RowEditi ng
pogrid.EditInde x = e.NewEditIndex
End Sub
Private Sub pogrid_RowUpdat ed(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewUpdatedEven tArgs) Handles
pogrid.RowUpdat ed
pogrid.EditInde x = -1
If e.Exception IsNot Nothing Then
'Do something
End If
End Sub
Protected Sub add_Click(ByVal sender As Object, ByVal e As EventArgs)
Handles add.Click
ObjectDataSourc e1.Insert()
End Sub
End Class
VB.Net Class
Public Class Purchase_Order
Dim _polines As List(Of POLine) = New List(Of POLine)
Dim _poNumber As String = String.Empty
Public Property Number() As String
Get
Return _poNumber
End Get
Set(ByVal value As String)
_poNumber = value
End Set
End Property
Public Function AddPOLines(ByVa l Quantity As Integer, ByVal billto As
Integer, ByVal description As String, ByVal type As String, ByVal project As
Integer, ByVal UnitPrice As Double, ByVal total As Double) As POLine
Dim newline As POLine = Me.AddPOLines
newline.Quantit y = Quantity
newline.Billto = billto
newline.Descrip tion = description
newline.Type = type
newline.project = project
newline.UnitPri ce = UnitPrice
newline.Total = total
Return newline
End Function
'Public Function upPOLines(ByVal Quantity As Integer, ByVal billto As
Integer, ByVal description As String, ByVal type As String, ByVal project As
Integer, ByVal UnitPrice As Double, ByVal unittotal As Double) As POLine
' Dim updateln As POLine = Me.upPOLines
' Quantity = updateln.Quanti ty
' billto = updateln.Billto
' description = updateln.Descri ption
' type = updateln.Type
' project = updateln.projec t
' UnitPrice = updateln.UnitPr ice
' unittotal = updateln.Total
' Return updateln
'End Function
Public Function AddPOLines() As POLine
Dim newline As POLine = New POLine
_polines.Add(ne wline)
Return newline
End Function
'Public Function upPOLines() As POLine
' Dim updateln As POLine = New POLine
' _polines.Add(up dateln)
' Return updateln
'End Function
Public Property POLines() As List(Of POLine)
Get
Return _polines
End Get
Set(ByVal value As List(Of POLine))
_polines = value
End Set
End Property
End Class
Public Class POLine
Dim _quantity As Integer
Dim _billto As Integer
Dim _description As String
Dim _type As Integer
Dim _projectitem As Integer
Dim _unitprice As Decimal
Dim _total As Decimal
Public Property Quantity() As Integer
Get
Return _quantity
End Get
Set(ByVal value As Integer)
_quantity = value
End Set
End Property
Public Property Billto() As Integer
Get
Return _billto
End Get
Set(ByVal value As Integer)
_billto = value
End Set
End Property
Public Property Description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
Public Property Type() As Integer
Get
Return _type
End Get
Set(ByVal value As Integer)
_type = value
End Set
End Property
Public Property project() As Integer
Get
Return _projectitem
End Get
Set(ByVal value As Integer)
_projectitem = value
End Set
End Property
Public Property UnitPrice() As Decimal
Get
Return _unitprice
End Get
Set(ByVal value As Decimal)
_unitprice = value
End Set
End Property
Public Property Total() As Decimal
Get
Return _total
End Get
Set(ByVal value As Decimal)
_total = value
End Set
End Property
End Class