参数化查询''需要参数'',这是未提供的

Dod*_*odi 0 c# asp.net gridview objectdatasource detailsview

我正在构建一个使用ObjectDataSource的应用程序.我希望我的所有患者详细信息都显示在GridView中,一旦用户选择其中的记录,我想在详细信息视图中显示特定记录的数据.

但是我有一个错误 getPatientFullDetailsbyPPS

参数化查询'(@PPS nvarchar(4000))选择*来自患者,其中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;
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

注:我已dataNameKeysPPSGridView2.

Nat*_*nSr 6

确保参数的值不是null.它必须是有效的对象或DBNull.Value.

这是微软的一个糟糕的设计选择,但由于某些原因,他们将null值视为完全排除参数.ADO.NET主要不使用泛型,所以我猜这是一个借口......有点.:)