Hello,
I'm new to asp.net. I'm learning from "Beginning ASP.Net 4: in C# &
VB". I've tried the examples in the chapter successfully ("Using the GridView and SqlDataSource Controls" & "Inserting Data with the DetailsView Control"). I've also managed to adapt it to my own project.
"Using the GridView and SqlDataSource Controls"
It work okay when i replace w/ stored procedures.
"Inserting Data with the DetailsView Control"
It does not work w/ stored procedures.
Pls. advise.
Pls. see code, below.
ASPLDLMarketDetail.aspx
Code:
<%@ Page Title="ASPL DL: Create/Edit New Market" Language="VB" MasterPageFile="~/MasterPages/Management.master" AutoEventWireup="false" CodeFile="ASPLDLMarketDetail.aspx.vb" Inherits="Management_ASPLDLMarketDetail" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="lblMarket" runat="server" Text="Create/Edit Market"></asp:Label>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="ID" DataSourceID="sdsWebGetMarketByID" Height="50px"
Width="125px" DefaultMode="Insert">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:TemplateField HeaderText="CategoryID" SortExpression="CategoryID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="sdsWebGetCategory" DataTextField="Name" DataValueField="ID"
SelectedValue='<%# Bind("CategoryID") %>' AutoPostBack="True">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="sdsWebGetCategory" DataTextField="Name" DataValueField="ID"
SelectedValue='<%# Bind("CategoryID") %>' AutoPostBack="True">
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EnterOn" HeaderText="EnterOn"
SortExpression="EnterOn" />
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="sdsWebGetMarketByID" runat="server"
ConnectionString="<%$ ConnectionStrings:DLDBConnectionString1 %>"
InsertCommand="prWebInsertMarket" InsertCommandType="StoredProcedure"
SelectCommand="prWebGetMarketByID" SelectCommandType="StoredProcedure"
UpdateCommand="prWebUpdateMarket" UpdateCommandType="StoredProcedure">
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="CategoryID" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="ID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="CategoryID" Type="Int32" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsWebGetCategory" runat="server"
ConnectionString="<%$ ConnectionStrings:DLDBConnectionString1 %>"
SelectCommand="SELECT [ID], [Name] FROM [tbCategory]"></asp:SqlDataSource>
</asp:Content>
ASPLDLMarketDetail.aspx.
vb
Code:
Partial Class Management_ASPLDLMarketDetail
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString.Get("ID") IsNot Nothing Then
DetailsView1.DefaultMode = DetailsViewMode.Edit
End If
End Sub
Protected Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
EndEditing()
End Sub
Protected Sub DetailsView1_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView1.ItemUpdated
EndEditing()
End Sub
Private Sub EndEditing()
Response.Redirect("ASPLDLMarket.aspx")
End Sub
End Class
Stored Procedures: prWebGetMarketByID
Code:
USE [DLDB]
GO
/****** Object: StoredProcedure [dbo].[prWebGetMarketByID] Script Date: 01/22/2015 18:25:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Name
-- Create date: 2 Jan 2015
-- Description: Get Market By ID
-- =============================================
ALTER PROCEDURE [dbo].[prWebGetMarketByID]
-- Add the parameters for the stored procedure here
@ID int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT [ID], [Name], [Description], [CategoryID], [EnterOn] FROM [tbMarket]
WHERE [ID] = @ID ORDER BY [Name]
END
Stored Procedures: prWebInsertMarket
Code:
USE [DLDB]
GO
/****** Object: StoredProcedure [dbo].[prWebInsertMarket] Script Date: 01/22/2015 18:27:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: htkwan
-- Create date: 2 Jan 2015
-- Description: Insert Market
-- =============================================
ALTER PROCEDURE [dbo].[prWebInsertMarket]
-- Add the parameters for the stored procedure here
@Name nvarchar(50),
@Description nvarchar(50),
@CategoryID int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT [tbMarket] ([Name], [Description], [CategoryID])
VALUES (@Name, @Description, @CategoryID)
END
Stored Procedures: prWebUpdateMarket
Code:
USE [DLDB]
GO
/****** Object: StoredProcedure [dbo].[prWebUpdateMarket] Script Date: 01/22/2015 18:28:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: htkwan
-- Create date: 2 Jan 2015
-- Description: Update Market
-- =============================================
ALTER PROCEDURE [dbo].[prWebUpdateMarket]
-- Add the parameters for the stored procedure here
@Name nvarchar(50),
@Description nvarchar(50),
@CategoryID int,
@ID int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE [tbMarket] SET [Name] = @Name,
[Description] = @Description,
[CategoryID] = @CategoryID,
[EnterOn] = GetDate()
WHERE [ID] = @ID
END