asp.net – 插入和更新问题
我是ASP.NET / C#的菜鸟,但我会尽我所能尽力描述我的问题.
现在我有一个连接了sql源的gridview;它抓住了正确的信息. gridview的信息将根据在网格外部的下拉列表中选择的内容而发生变化.问题在于更新和插入. 一旦尝试更新. (当我在其中一列上单击编辑时)我收到以下错误消息:’DropDownList2’具有一个无效的SelectedValue,因为它在项目列表中不存在. 我将列拆分为模板,如下所示,因此我可以更轻松地管理它们. “下拉列表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> 程序: 这是下拉列表.这给出了名称和值. *注意:此过程适用于第一个下拉列表,我想第二个下拉列表. ALTER PROCEDURE dbo.uspDoctorList AS BEGIN SET NOCOUNT ON SELECT ID,LastName +',' + FirstName AS 'Doctor' FROM Doctor ORDER BY 'Doctor' END 这是更新程序.不确定这是否是错误的,或者它只是我的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 我会感激任何帮助.顺便说一句,这个东西比我们教的东西(使用ASP中的存储过程)稍微提前一点,我要加倍努力获得奖励标记..在此先感谢. 如果您需要更多信息,请询问 解决方法
问题1 – 更新gridview中的数据
1)正如Andriy所指出的,你的第一个问题是ID在< UpdateParameters>中被列出两次,删除其中一个. 2)从参数列表中删除时间戳,timestamps are updated automatically <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> 注意:如果需要保存日期时间戳,请在SQL中将Timestamp列的数据类型更改为datetime,并将Update存储过程更改为 – Patient.Timestamp = GETDATE() 3)从更新存储过程中删除@Timestamp(作为参数和WHERE子句) 4)在gridview设置DataKeyNames =“ID”的标记中这是非常重要的,使用DataKeyNames属性指定表示数据源主键的字段,必须设置它才能自动更新和删除GridView控件工作: <asp:GridView ID="GridView1" DataKeyNames="ID" 完成上述所有操作后,您的更新将会正常运行,我已经使用SQL Server Express 2008和ASP.NET 4.0为您创建了一个示例项目,您可以在Google驱动器上找到它here(只需点击文件 – >下载获取.zip项目) 问题2 – 在gridview中插入数据并刷新 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> 2)向页面添加插入控件,并在用户单击添加按钮时调用sql数据源的Insert()方法: <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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-4 – 使用asp.net MVC4,如何在默认情
- asp.net-core-mvc – 我是否需要在Visual Studio
- asp.net-mvc – 为什么ASP.Net MVC便携式区域不像
- asp.net-mvc – .NET MVC / Entity Framework应用
- asp.net-mvc – 为什么在MVC中传递实体不是一个好
- 如何为ASP.NET应用程序全局设置autocomplete = o
- 如何在ASP.net Identity 2.0上启用AccessFailedC
- ASP.Net vnext api在localhost上工作,在azure上返
- 基于ASP.NET使用C#实现简单人脸识别功能
- 优化 – 字典/客户端VS应用程序变量