101*_*110 5 sql sql-server asp.net stored-procedures
我是ASP.NET/C#的菜鸟,但我会尽我所能尽力描述我的问题.
现在我有一个连接了sql源的gridview; 它抓住了正确的信息.gridview的信息将根据在网格外部的下拉列表中选择的内容而发生变化.问题在于更新和插入.
一旦尝试更新.(当我在其中一列上单击编辑时)我收到以下错误消息:'DropDownList2'具有一个无效的SelectedValue,因为它在项目列表中不存在.参数名称:value
我将列拆分为模板,如下所示,因此我可以更轻松地管理它们."下拉列表2"位于我的编辑模板中.它连接到数据源; 这与我的第一个dropdownmenu完全一样,完美无缺.所以我不相信这将是该程序背后的SQL.但我确实将此下拉列表2绑定到Doctor. 现在有人告诉我绑定它,所以我不知道绑定是如何工作的
如果我取消绑定,我至少可以在单击编辑后看到网格,但是在我更新后我收到错误消息:过程或函数uspPatientUpdate指定了太多参数.
现在我已经在网上寻找解决方案,但我无法绕过绑定.我会根据需要提供以下代码.
ASP:
<asp:SqlDataSource ID="sdPatient" runat="server" ConnectionString="<%$ ConnectionStrings:MedicalOfficeConnectionString %>" DeleteCommand="usp_PatientDelete" InsertCommand="uspPatientInsert" SelectCommand="uspPatientSelectByIDOrSelectAll" UpdateCommand="uspPatientUpdate" SelectCommandType="StoredProcedure" DeleteCommandType="StoredProcedure" InsertCommandType="StoredProcedure" UpdateCommandType="StoredProcedure">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="OHIP" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter DbType="Date" Name="DOB" />
<asp:Parameter Name="VisitsPerYear" Type="Int32" />
<asp:Parameter Name="DoctorID" Type="Int32" />
<asp:Parameter Name="Timestamp" Type="Byte"></asp:Parameter>
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="DoctorID" PropertyName="SelectedValue" Type="Int32" DefaultValue="0" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="OHIP" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter DbType="Date" Name="DOB" />
<asp:Parameter Name="VisitsPerYear" Type="Int32" />
<asp:Parameter Name="DoctorID" Type="Int32" />
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="Timestamp" Type="Byte"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<p>Select Patient By Doctor:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="sdDoctorList" DataTextField="Doctor" DataValueField="ID" AppendDataBoundItems="True">
<asp:ListItem Value="0">All Doctors</asp:ListItem>
</asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" DataSourceID="sdPatient" GridLines="Horizontal">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:TemplateField HeaderText="OHIP" SortExpression="OHIP">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("OHIP") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("OHIP") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DOB" SortExpression="DOB">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("DOB") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("DOB") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Visits Per Year" SortExpression="VisitsPerYear">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("VisitsPerYear") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("VisitsPerYear") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Doctor" SortExpression="Doctor">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="sdDoctorList" DataTextField="Doctor" DataValueField="ID" SelectedValue='<%# Bind("Doctor") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Doctor") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
程序:
这是下拉列表.这给出了名称和值.*注意:此过程适用于第一个下拉列表,我想第二个下拉列表.
ALTER PROCEDURE dbo.uspDoctorList
AS
BEGIN
SET NOCOUNT ON
SELECT ID, LastName +', ' + FirstName AS 'Doctor'
FROM Doctor
ORDER BY 'Doctor'
END
Run Code Online (Sandbox Code Playgroud)
这是更新程序.不确定这是否是错误的,或者它只是我的ASP
ALTER PROCEDURE dbo.uspPatientUpdate
@ID int,
@OHIP char(10),
@FirstName nvarchar(20),
@LastName nvarchar(40),
@DOB date,
@VisitsPerYear int,
@DoctorID int,
@Timestamp Timestamp
AS
BEGIN
SET NOCOUNT OFF
UPDATE Patient
SET OHIP = @OHIP,
FirstName = @FirstName,
LastName = @LastName,
DOB = @DOB,
VisitsPerYear = @VisitsPerYear,
DoctorID = @DoctorID
WHERE ID = @ID AND Timestamp = @Timestamp
END
Run Code Online (Sandbox Code Playgroud)
我会感激任何帮助.顺便说一句,这个东西比我们教的东西(使用ASP中的存储过程)稍微提前一点,我要加倍努力获得奖励标记..在此先感谢.
如果您需要更多信息,请询问
1)正如Andriy指出的,你的第一个问题是ID在 中列出了两次 <UpdateParameters>
,删除其中一个。
2)从参数列表中删除Timestamp,时间戳会自动更新
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="OHIP" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="DOB" DbType="DateTime" />
<asp:Parameter Name="VisitsPerYear" Type="Int32" />
<asp:Parameter Name="DoctorID" Type="Int32" />
</UpdateParameters>
Run Code Online (Sandbox Code Playgroud)
注意:如果需要保存日期时间戳,请在 SQL 中将时间戳列的数据类型更改为日期时间,并将更新存储过程更改为 -Patient.Timestamp = GETDATE()
3)从更新存储过程中删除@Timestamp(既作为参数又从WHERE子句中)
4)在gridview的标记中设置DataKeyNames="ID"
这一点非常重要,使用DataKeyNames属性来指定代表数据源主键的字段,必须设置它才能使GridView控件的自动更新和删除功能发挥作用:
<asp:GridView
ID="GridView1"
DataKeyNames="ID"
Run Code Online (Sandbox Code Playgroud)
完成上述所有操作后,您的更新将起作用,我使用 SQL Server Express 2008 和 ASP.NET 4.0 为您创建了一个示例项目,您可以在 Google 驱动器上找到它(只需单击“文件”->“下载”)获取 .zip 项目)
1)更改<InsertParameters>
为从控件获取数据(我显示的示例只有四个参数,您可以根据需要更改它)
<InsertParameters>
<asp:ControlParameter ControlID="txtOhip" Name="OHIP" />
<asp:ControlParameter ControlID="txtFirstName" Name="FirstName" />
<asp:ControlParameter ControlID="txtLastName" Name="LastName" />
<asp:ControlParameter ControlID="ddlDoctorId" Name="DoctorID" PropertyName="SelectedValue" />
</InsertParameters>
Run Code Online (Sandbox Code Playgroud)
2)向页面添加插入控件,并Insert()
在用户单击添加按钮时调用您的sql数据源的方法:
<table>
<tr>
<td>
OHIP
</td>
<td>
<asp:TextBox ID="txtOhip" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
First name
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last name
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Doctor
</td>
<td>
:<asp:DropDownList ID="ddlDoctorId" runat="server" AutoPostBack="True" DataSourceID="sdDoctorList"
DataTextField="Doctor" DataValueField="DoctorID" AppendDataBoundItems="True">
<asp:ListItem Value="0">All Doctors</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="text-align: right" colspan="2">
<asp:Button ID="btnAdd" OnClick="Add" runat="server" Text="Add" />
</td>
</tr>
</table>
<script runat="server">
protected void Add(object sender,EventArgs e)
{
sdPatient.Insert();
}
</script>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1478 次 |
最近记录: |