问题描述
我要建一个使用 ObjectDataSource控件的应用程序。我想显示在我所有的耐心细节的 GridView控件并一旦用户在它选择的记录,我想显示在细节特定记录的数据视图。
I'm building an application that uses an ObjectDataSource. I want all my patient details to be displayed in a GridView and once a user selects a record in it, I want to display the data for particular record in a details view.
不过我在 getPatientFullDetailsbyPPS
在参数化查询(@PPS为nvarchar(4000))SELECT * FROM患者
其中,PPS = @PPS'需要参数'@PPS',这是不
提供。
C#:
public static Patient GetPatientFullDetailsByPPS(string PPS)
{
Patient patient = new Patient();
//string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(GetConnectionString()))
{
SqlCommand cmd = new
SqlCommand("Select * from Patients where PPS = @PPS", con);
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@PPS";
parameter.Value = PPS;
cmd.Parameters.Add(parameter);
con.Open();
SqlDataReader dr = cmd.ExecuteReader(); --------> error here
while (dr.Read())
{
patient.PPS = dr["PPS"].ToString();
patient.Surname = dr["Surname"].ToString();
patient.Name = dr["Name"].ToString();
patient.DOB = dr["DOB"].ToString();
patient.Gender = dr["Gender"].ToString();
patient.BloodGroup = dr["BloodGroup"].ToString();
patient.MedicalCard = dr["MedicalCard"].ToString();
patient.AddressLine1 = dr["AddressLine1"].ToString();
patient.AddressLine2 = dr["AddressLine2"].ToString();
patient.City = dr["City"].ToString();
patient.County = dr["County"].ToString();
patient.Phone = dr["Phone"].ToString();
patient.Mobile = dr["Mobile"].ToString();
patient.Email = dr["Email"].ToString();
}
}
return patient;
}
HTML:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="AllPatientsObjectDataSource" Width="759px" Height="179px" style="margin-right: 15px" DataKeyNames="PPS">
<Columns>
<asp:BoundField DataField="PPS" HeaderText="PPS" SortExpression="PPS" />
<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="2">
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="PatientsDetailsObjectDataSource" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="PPS" HeaderText="PPS" SortExpression="PPS" />
<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
<asp:BoundField DataField="BloodGroup" HeaderText="BloodGroup" SortExpression="BloodGroup" />
<asp:BoundField DataField="MedicalCard" HeaderText="MedicalCard" SortExpression="MedicalCard" />
<asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" SortExpression="AddressLine1" />
<asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="County" HeaderText="County" SortExpression="County" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Mobile" HeaderText="Mobile" SortExpression="Mobile" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
</Fields>
</asp:DetailsView>
</td>
</tr>
</table>
<asp:ObjectDataSource ID="AllPatientsObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetPatients" TypeName="PatientDB" DataObjectTypeName="Patient" DeleteMethod="DeletePatient" OnDeleted="ObjectDataSource1_Deleted"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="PatientsDetailsObjectDataSource" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetPatientFullDetailsByPPS" TypeName="PatientDB">
<SelectParameters>
<asp:ControlParameter ControlID="GridView2" Name="PPS" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
的注:我已设置 dataNameKeys
到 PPS
在 GridView2
。的
推荐答案
请确保该参数的值不是空
。它必须是一个有效的对象或 DBNull.Value
。
Make sure that the value for the parameter is not null
. It must be a valid object or DBNull.Value
.
这是一个可怕的设计选择微软,但由于某些原因,他们对待一个空
值作为完全不含参数。 ADO.NET大多不使用仿制药,所以我想这是一个借口......一些。 :)
This was a terrible design choice by Microsoft, but for some reason they treat a null
value as excluding the parameter altogether. ADO.NET mostly doesn't utilize generics so I guess that's an excuse... somewhat. :)
这篇关于该参数化查询''需要参数'',但未提供该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!