【问题标题】:"Invalid column" error populating GridView from SqlDataReader从 SqlDataReader 填充 GridView 时出现“无效列”错误
【发布时间】:2013-12-26 18:34:39
【问题描述】:

我正在尝试从数据库中读取数据,然后在绑定到 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

) 开启 [主要]

从数据中读取数据的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());
        }


    }

【问题讨论】:

  • 看起来您传递给 SqlCommand 的查询指定了表中不存在的字段。

标签: asp.net gridview webforms


【解决方案1】:

该错误表示错误消息中列出的列(“PrescriptionNumber”、“PrescriptionNumber”、“DrugCode”、“PrescriptionDate”、“PaymentStatus”和“AmountPaidFrom”)实际上不存在于正在查询的表中。

您需要验证您将查询定向到正确的表,查询中没有拼写错误,并且列确实存在于您尝试查询的表中。

【讨论】:

  • @jadrnel27,该表肯定存在正确的列
  • @jadrnel27,我已经编辑了我的问题。添加表格和方法的全部细节。
  • @kombo 哇,真奇怪!此时我唯一能想到的是您的连接字符串(“ApplicationServices”)没有将您指向正确的数据库。您是否拥有该数据库/表的多个实例/版本?比如,生产、质量保证和测试。还是某个地方的 Payment 表的旧的、不完整的版本?
  • 但是,@kombo,我没有看到您的错误消息中的代码(using (SqlDataReader reader = cmd.ExecuteReader()) 等等)。在哪里调用?
  • 我想相信这是问题所在:SqlCommand cmd = new SqlCommand(sqlquery,con);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多