|
|
 |
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 tens of thousands of computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other programmers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |

October 30th, 2009, 03:51 PM
|
|
Authorized User
|
|
Join Date: Oct 2009
Posts: 78
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
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>
<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 />
<br />
<br />
</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
|

October 30th, 2009, 04:33 PM
|
 |
Wrox Author
Points: 35,654, Level: 82 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,575
Thanks: 12
Thanked 284 Times in 280 Posts
|
|
Maybe Product.Id doesn't default to -1?
Imar
|

October 30th, 2009, 05:14 PM
|
|
Authorized User
|
|
Join Date: Oct 2009
Posts: 78
Thanks: 11
Thanked 0 Times in 0 Posts
|
|
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.......
|

October 30th, 2009, 06:03 PM
|
 |
Wrox Author
Points: 35,654, Level: 82 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,575
Thanks: 12
Thanked 284 Times in 280 Posts
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |