The parameterized query '' expects the parameter '', which was not supplied

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 19:16:32

问题


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.

However I have an error in getPatientFullDetailsbyPPS

The parameterized query '(@PPS nvarchar(4000))Select * from Patients where PPS = @PPS' expects the parameter '@PPS', which was not supplied.

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>

Note: I have set dataNameKeys to PPS in GridView2.


回答1:


Make sure that the value for the parameter is not null. It must be a valid object or DBNull.Value.

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. :)



来源:https://stackoverflow.com/questions/22306818/the-parameterized-query-expects-the-parameter-which-was-not-supplied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!