p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 30th, 2009, 03:51 PM
Authorized User
Points: 143, Level: 2
Points: 143, Level: 2 Points: 143, Level: 2 Points: 143, Level: 2
Activity: 57%
Activity: 57% Activity: 57% Activity: 57%
 
Join Date: Oct 2009
Posts: 36
Thanks: 4
Thanked 0 Times in 0 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old October 30th, 2009, 04:33 PM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
Default

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

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old October 30th, 2009, 05:14 PM
Authorized User
Points: 143, Level: 2
Points: 143, Level: 2 Points: 143, Level: 2 Points: 143, Level: 2
Activity: 57%
Activity: 57% Activity: 57% Activity: 57%
 
Join Date: Oct 2009
Posts: 36
Thanks: 4
Thanked 0 Times in 0 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.......
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old October 30th, 2009, 06:03 PM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 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
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004

Did this post help you? Click the button to show your appreciation!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 08:17 AM
updating the table from the list view columns Yasho VB Databases Basics 2 July 31st, 2007 03:06 AM
how to view the checked items after navigating madhusrp C# 1 January 24th, 2006 03:28 PM



All times are GMT -4. The time now is 01:28 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc