从SqlDataReader填充GridView的“无效列”错误

时间:2013-12-26 18:34:40

标签: asp.net gridview webforms

我正在尝试从数据库中读取数据,然后在绑定到GridView之前填充数据表。

当我运行应用程序时,会产生以下错误:

Exception Details: System.Data.SqlClient.SqlException: Invalid column name  'PrescriptionNumber'.
Invalid column name 'PrescriptionNumber'.
Invalid column name 'DrugCode'.
Invalid column name 'PrescriptionDate'.
Invalid column name 'PaymentStatus'.
Invalid column name 'AmountPaidFrom'.

Source Error: 


Line 34:           cmd.Connection = con;
Line 35:           con.Open();
Line 36:           using (SqlDataReader reader = cmd.ExecuteReader())
Line 37:           {
Line 38:               if (reader.HasRows)    

这是标记:

<asp:GridView ID="grdpayment" runat="server" Width="876px" 
        AutoGenerateColumns="False" CellPadding="4" GridLines="Horizontal" 
        BackColor="White" BorderColor="#336666" BorderStyle="Double" 
        BorderWidth="3px" style="margin-right: 288px" 
        onselectedindexchanged="grdpayment_SelectedIndexChanged">
    <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>

有没有想知道我做错了什么? 这是我的表脚本:

CREATE TABLE [dbo].[Payment](
[PrescriptionNumber] [varchar](50) NOT NULL,
[TreatmentCode] [varchar](50) NOT NULL,
[DrugCode] [varchar](50) NOT NULL,
[PrescriptionDate] [date] NOT NULL,
[DrugQuantity] [int] NOT NULL,
[PaymentStatus] [varchar](50) NOT NULL,
[AmountPaid] [money] NOT NULL

)ON [PRIMARY]

C#从数据中读取数据的方法:

 public void makepayment(string prescriptionValue)
    {
      DataTable storedata = new DataTable();
      string _connection = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
      SqlConnection con = new SqlConnection(_connection);
      string sqlquery =
           "Select PrescriptionNumber,DrugCode,PrescriptionDate,PaymentStatus,AmountPaid"
         + "From Payment where PrescriptionNumber='" + prescriptionValue + "'";
      SqlCommand cmd = new SqlCommand(sqlquery,con);
      SqlDataAdapter adapt = new SqlDataAdapter(cmd);
      adapt.Fill(storedata);
      grdpayment.DataSource = storedata;
      grdpayment.DataBind();
    } 
我打电话给这样的方法:

protected void btnsearchprescripitionnum_Click(object sender, EventArgs e)
    {
        if (txtprescriptionnum.Text == "")
        {

            checkval.Text = "Enter PriscriptionNumber";
        }
        else
        {
            makepayment(txtprescriptionnum.Text.ToString());
        }


    }

1 个答案:

答案 0 :(得分:3)

错误意味着错误消息中列出的列('PrescriptionNumber','PrescriptionNumber','DrugCode','PrescriptionDate','PaymentStatus'和'AmountPaidFrom')实际上不存在于被查询的表中。

您需要验证是否在正确的表格中引导查询,查询中没有拼写错误,并且列实际存在于您尝试查询的表格中。