Datagridview Won't stay updated

L

Landry, Eric

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"
AutoGenerateColumns="False"
style="margin-left: 0px; margin-right: 0px; margin-bottom: 0px"
Width="1020px" BorderColor="Black" BorderStyle="Solid"
CellPadding="4"
BackColor="White" BorderWidth="1px"
ForeColor="Black"
GridLines="Vertical" HorizontalAlign="Center"
BackImageUrl="~/POTracking/images/buttontemplate.jpg"
Font-Names="Calibri" DataSourceID="ObjectDataSource1">
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#E1E3EC" BorderColor="Black"
BorderStyle="Solid"
BorderWidth="1px" Font-Names="Calibri"
HorizontalAlign="Center" />
<Columns>

<asp:CommandField ButtonType="Image"
DeleteImageUrl="~/POTracking/images/Deletesmall.png"
ShowCancelButton="False" ShowDeleteButton="True"
ShowEditButton="True"
EditImageUrl="~/POTracking/images/Document.png"
ShowInsertButton="True"
UpdateImageUrl="~/POTracking/images/Sync.png" />
<asp:TemplateField HeaderText="QTY"
SortExpression="Quantity">
<headerstyle width="36px" />
<EditItemTemplate>
<asp:TextBox ID="qtybox" runat="server" width="32px"
Text='<%# Bind("Quantity", "{0}")
%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="qtylbl" runat="server" Text='<%#
Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="40px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Bill To"
SortExpression="Billto" >
<headerstyle width="85px" />
<EditItemTemplate>
<asp:DropDownList ID="billtobox" runat="server"
DataSourceID="billto"
DataTextField="name" datavaluefield="id"
Width="79px"
SelectedValue='<%# Bind("Billto", "{0}") %>'
appenddatabounditems="true">
<asp:ListItem Value="0">Select</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="billto" runat="server"
ConnectionString="<%$
ConnectionStrings:pOTracking %>" SelectCommand="billto"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="billtolbl"
runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Description"
SortExpression="Description">
<headerstyle width="454px" />
<EditItemTemplate>
<asp:TextBox ID="descbox" width="450px"
runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="desclbl" runat="server" Text='<%#
Bind("Description") %>'></asp:Label>
</ItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="Type" SortExpression="Type">
<headerstyle width="44px" />
<EditItemTemplate>
<asp:DropDownList ID="typebox" runat="server"
width="40px" appenddatabounditems="True"
DataSourceID="seltype" DataTextField='typeabbr'
DataValueField="id" SelectedValue='<%#
Bind("Type", "{0}") %>'>
<asp:ListItem Value="0">Select</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="seltype" runat="server"
ConnectionString="<%$
ConnectionStrings:pOTracking %>" SelectCommand="type"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="typelbl" runat="server" Text='<%#
Bind("Type") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="46px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Project"
SortExpression="project">
<headerstyle width="110px" />
<EditItemTemplate>
<asp:DropDownList ID="projbox" runat="server"
DataSourceID="Projectrd"
DataTextField="name" DataValueField="Projectid"
width="106px"
SelectedValue='<%# Bind("project", "{0}") %>'
appenddatabounditems="true">
<asp:ListItem Value="0">Select</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="Projectrd" runat="server"
ConnectionString="<%$
ConnectionStrings:pOTracking %>" SelectCommand="proj"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="projlbl" runat="server" Text='<%#
Bind("project") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price"
SortExpression="UnitPrice">
<headerstyle width="79px" />
<EditItemTemplate>
<asp:TextBox ID="unitpricebox" width="75px"
runat="server"
Text='<%# Bind("UnitPrice", "{0:N}") %>'
Height="22px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="unitpricelbl" runat="server"
Text='<%# Bind("UnitPrice") %>'></asp:Label>
</ItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="Total"
SortExpression="Total">
<headerstyle width="79px" />
<EditItemTemplate>
<asp:TextBox ID="Totalbox" runat="server"
BackColor="Silver"
BorderColor="Black" Text='<%# Bind("Total",
"{0:N}") %>' ReadOnly="True"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="totallbl" runat="server" Text='<%#
Bind("Total") %>'></asp:Label>
</ItemTemplate>

</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#F7F7DE" ForeColor="Black"
HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True"
ForeColor="White" />
<HeaderStyle Wrap="False" BorderColor="Black"
BorderStyle="Solid"
BorderWidth="1px" />
<AlternatingRowStyle BackColor="#C7C7C7" />
</asp:GridView>



VB.Net Code


<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="AddPOLines" TypeName="POTracker.Purchase_Order"
InsertMethod="AddPOLines"
EnableCaching="True" UpdateMethod="AddPOLines">
<UpdateParameters>
<asp:parameter Name="Quantity" Type="Int32" />
<asp:parameter Name="billto" Type="Int32" />
<asp:parameter Name="description" Type="String" />
<asp:parameter Name="type" Type="String" />
<asp:parameter Name="project" Type="Int32" />
<asp:parameter Name="UnitPrice" Type="Double" />
<asp:parameter Name="total" Type="Double" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="Quantity" Type="Int32" />
<asp:parameter Name="billto" Type="Int32" />
<asp:parameter Name="description" Type="String" />
<asp:parameter Name="type" Type="String" />
<asp:parameter Name="project" Type="Int32" />
<asp:parameter Name="UnitPrice" Type="Double" />
<asp:parameter Name="total" Type="Double" />
</InsertParameters>
</asp:ObjectDataSource>



<asp:Button ID="add" runat="server" Text="New Row" />

VB.Net
Imports System.Security
Imports System.Security.Principal.WindowsIdentity
Imports Microsoft.ApplicationBlocks
Partial Public Class create

Inherits System.Web.UI.Page



Private Sub pogrid_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs) Handles pogrid.RowEditing
pogrid.EditIndex = e.NewEditIndex
End Sub

Private Sub pogrid_RowUpdated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles
pogrid.RowUpdated
pogrid.EditIndex = -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
ObjectDataSource1.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(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 total As Double) As POLine
Dim newline As POLine = Me.AddPOLines

newline.Quantity = Quantity
newline.Billto = billto
newline.Description = description
newline.Type = type
newline.project = project
newline.UnitPrice = 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.Quantity
' billto = updateln.Billto
' description = updateln.Description
' type = updateln.Type
' project = updateln.project
' UnitPrice = updateln.UnitPrice
' unittotal = updateln.Total
' Return updateln
'End Function



Public Function AddPOLines() As POLine
Dim newline As POLine = New POLine
_polines.Add(newline)
Return newline
End Function


'Public Function upPOLines() As POLine
' Dim updateln As POLine = New POLine
' _polines.Add(updateln)
' 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
 
B

Bob Barrows [MVP]

I am posting this for a coworker.

There was no way for you to know it (except maybe by browsing through some
of the previous questions in this newsgroup before posting yours - always a
recommended practice) , but this is a classic (COM-based) asp newsgroup.
ASP.Net bears very little resemblance to classic ASP so, while you may be
lucky enough to find a dotnet-knowledgeable person here who can answer your
question, you can eliminate the luck factor by posting your question to a
group where those dotnet-knowledgeable people hang out. I suggest
microsoft.public.dotnet.framework.aspnet or the forums at www.asp.net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top