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

时间:2014-03-10 17:14:44

标签: 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;
}

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

1 个答案:

答案 0 :(得分:6)

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

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