Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 30th, 2009, 02:51 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Updating items in form view

Thank you sir for last post. That really helped me out.
But as problems never end, I have another problem. Well not used in book but I wanna add functionality of updating products in AddProduct.aspx file.
I have used Edit Item Template with Form view and added events like
OnItemInserting = "FormView1_ItemInserting" OnItemUpdating= "FormView1_ItemUpdating"
OnItemInserted = "FormView1_ItemInserted" OnItemUpdated = "FormView1_ItemUpdated"

AddProduct.aspx page looks like

Code:
<asp:FormView ID="FormView1" runat="server" DataSourceID="odsProducts" DefaultMode="Insert" DataKeyNames = "Id" 
 OnItemInserting = "FormView1_ItemInserting" OnItemUpdating= "FormView1_ItemUpdating" 
 OnItemInserted = "FormView1_ItemInserted" OnItemUpdated = "FormView1_ItemUpdated" >
    <EditItemTemplate >
    <h1>Update The Product</h1>
      <table border="0" cellpadding="3" cellspacing="0" style="width: 100%; height: 100%">
      <tr>
        <td class="Label" valign="top">
          Title:
        </td>
        <td>
          <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' Width="300px" />
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TitleTextBox" ErrorMessage="Please Enter A Title" Display="Dynamic"></asp:RequiredFieldValidator>
        </td>
      </tr>
      <tr>
        <td class="Label" valign="top">
          Description:</td>
        <td>
          <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' TextMode="MultiLine" Width="300px" Height="106px" />
          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DescriptionTextBox" ErrorMessage="Please Enter A Description" Display="Dynamic"></asp:RequiredFieldValidator>
        </td>
      </tr>
      <tr>
        <td class="Label" valign="top">
          Category:
        </td>
        <td>
          <asp:DropDownList ID="lstCategoryId" runat="server" DataSourceID="odsProductCategories" DataTextField="Description" DataValueField="Id">
          </asp:DropDownList>
        </td>
      </tr>
      <tr>
        <td class="Label" valign="top">
          Price:
        </td>
        <td>
          <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' Width="300px" />
          <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="PriceTextBox" ErrorMessage="Please Enter The Price" Display="Dynamic"></asp:RequiredFieldValidator><asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="PriceTextBox" ErrorMessage="Please Enter A Number Between 0 And 10000" MaximumValue="10000" MinimumValue="0" Type="Currency" Display="Dynamic"></asp:RangeValidator>
        </td>
      </tr>
      <tr>
        <td class="Label" valign="top">
          Image:</td>
        <td>
          <asp:FileUpload ID="FileUpload1" runat="server" Width="300px" />
          <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="FileUpload1" Display="Dynamic" ErrorMessage="Please Select An Image First"></asp:RequiredFieldValidator></td>
      </tr>
      <tr>
        <td>
        </td>
        <td>
          &nbsp;<asp:Button ID="btnInsert" runat="server" CommandName="Insert" Text="Insert" />
          <asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="False" OnClick="btnCancel_Click" />
        </td>
      </tr>
      </table>
      <br />
      <br />
      <br />
      &nbsp;<br />
      <br />
      &nbsp;
    </EditItemTemplate>
      
    <InsertItemTemplate>
Same implementation as in book.....
 </InsertItemTemplate>
</asp:FormView>
I have changed the corresponding object data source as
Code:
<asp:ObjectDataSource ID="odsProducts" runat="server" DataObjectTypeName="Product" InsertMethod="InsertProduct" SelectMethod="GetProduct" UpdateMethod="UpdateProduct"  TypeName="ShopManager">
Also added update method in the ShopManagerDB.vb and called it from ShopManager.vb
Code:
 Public Shared Sub UpdateProduct(ByVal theProduct As Product)
        Try
            Using myConnection As New SqlConnection(AppConfiguration.ConnectionString)

                Dim myCommand As SqlCommand = New SqlCommand("sprocProductInsertUpdateSingleItem", myConnection)
                myCommand.CommandType = CommandType.StoredProcedure

                myCommand.Parameters.Clear()
                myCommand.Parameters.AddWithValue("@title", theProduct.Title)
                myCommand.Parameters.AddWithValue("@description", theProduct.Description)
                myCommand.Parameters.AddWithValue("@price", theProduct.Price)
                myCommand.Parameters.AddWithValue("@categoryId", theProduct.CategoryId)
                myCommand.Parameters.AddWithValue("@pictureUrlSmall", theProduct.PictureUrlSmall)
                myCommand.Parameters.AddWithValue("@pictureUrlMedium", theProduct.PictureUrlMedium)
                myCommand.Parameters.AddWithValue("@pictureUrlLarge", theProduct.PictureUrlLarge)

                myConnection.Open()

                myCommand.ExecuteNonQuery()
                myConnection.Close()
            End Using
        Catch ex As Exception
            ' Pass up the error; it will be caught by the code in the Global.asax and the generic error page set up in web.config.
            Throw
        End Try
    End Sub
But rather than updating item it inserts item in the database. For this purpose I had removed
Code:
If theProduct.Id = -1 Then
          myCommand.Parameters.AddWithValue("@id", DBNull.Value)
        Else
          myCommand.Parameters.AddWithValue("@id", theProduct.Id)
        End If
And used myCommand.Parameters.Clear() to get rid off previous value but still it is not working.....
please help me in this regard.......
And yeah once again thank you for last post
 
Old October 30th, 2009, 03:33 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Maybe Product.Id doesn't default to -1?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old October 30th, 2009, 04:14 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Well I have tried the update method with and without theProduct.Id = -1 as well
Code:
If theProduct.Id = -1 Then
                    myCommand.Parameters.AddWithValue("@id", DBNull.Value)
                Else
                    myCommand.Parameters.AddWithValue("@id", theProduct.Id)
                End If
But it always inserts the product instead of updating it.
Well according you what should be the procedure of updating items using form view. Maybe my approach is wrong at all.
So is there any other way of implementing update using form view.
Thank you sir.......
 
Old October 30th, 2009, 05:03 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

What I meant was the initial value of the Id. Take a look at this:

If theProduct.Id = -1 Then

What if the default value of Id = 0, the default for an Integer? Then it always assumes an update....

Likewise, if the Id is never set when you load an item from the database using something like GetProduct, its value may always be -1 and an Insert always occurs.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to view my data in my database in a certain items? jhanny007 PHP Databases 1 March 6th, 2009 12:43 AM
how to view my data in my database in a certain items? jhanny007 MySQL 3 January 6th, 2009 08:16 AM
How to view picture by using form view? mysecondlove ASP.NET 2.0 Basics 1 April 8th, 2008 07:17 AM
updating the table from the list view columns Yasho VB Databases Basics 2 July 31st, 2007 02:06 AM
how to view the checked items after navigating madhusrp C# 1 January 24th, 2006 03:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.