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