将错误视为无效的列名C#.NET

时间:2018-02-23 10:46:03

标签: c# sql asp.net ado.net

我是C#.net的新手。我正在开发一个小应用程序,其中一个下拉列表中的数据将根据在另一个下拉列表中选择的值进行填充。在查看输出后,我收到错误Invalid column name 'CategoryBasedList'。你能指出我哪里出错了。我附上下面的代码。提前谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class UserLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
        {
          populate();
        }
    }
    protected void ddlCategoryBasedList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    protected void ddlListOfBooks_SelectedIndexChanged(object sender, EventArgs e)
        {

        string constr = ConfigurationManager.ConnectionStrings["CONN_STR"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        SqlCommand com = new SqlCommand("Select distinct(CategoryBasedList) from Category where CategoryList = '" +  (ddlListOfBooks.SelectedValue).ToString() + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlCategoryBasedList.DataTextField = ds.Tables[0].Columns["CategoryBasedList"].ToString();
        ddlCategoryBasedList.DataSource = ds.Tables[0];
        ddlCategoryBasedList.DataBind();

        }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {

        }
    public void populate()
    {
    string constr = ConfigurationManager.ConnectionStrings["CONN_STR"].ToString();
    SqlConnection con = new SqlConnection(constr);
    con.Open();
    SqlCommand com = new SqlCommand(" Select distinct(CategoryList) from Category", con);
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataSet ds = new DataSet();
    da.Fill(ds);
    ddlListOfBooks.DataTextField = ds.Tables[0].Columns["CategoryList"].ToString();
    ddlListOfBooks.DataSource = ds.Tables[0];
    ddlListOfBooks.DataBind();
    }
}

上述表格的数据库结构 - CATEGORY如下。

CREATE TABLE [dbo].[Category](
    [[CategoryBasedList]]] [varchar](100) NOT NULL,
    [CategoryList] [varchar](100) NOT NULL,
    [Authors] [varchar](100) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF 
GO

2 个答案:

答案 0 :(得分:3)

您的slq尝试访问不存在的列:

dragula([document.querySelector('.container1'), document.querySelector('.container2')], {

  copy: function (el, source) {
    return source === document.querySelector('.container1')
  },
  accepts: function (el, target) {
    return target !== document.querySelector('.container1')
  },
   removeOnSpill: true
  });

var findId = $('.container2').find(id^=n);

$('.showMeId').append(findId);

检查表new SqlCommand(@" Select distinct(CategoryBasedList) from Category where CategoryList = '" + (ddlListOfBooks.SelectedValue).ToString() + "'", con); 中的列名:根据您的DDL,您的数据库列名为Category

修复后,google Sql Injection和/或阅读Why do we always prefer using parameters in SQL statements?使用[CategoryBasedList]为sql语句提供参数。

此外,您应该开始使用SqlParameters来正确处理您的IDIsposable-Implementationing DB-Objects,以防它们抛出异常。

对于指针,您可以在此处查看:in a "using" block is a SqlConnection closed on return or exception?

答案 1 :(得分:2)

您需要在CategoryBasedList周围排除括号。正如您现在所示,字段名称实际上是[CategoryBasedList]],而不是您所期望的CategoryBasedList。 只需重命名该列名称即可删除额外的括号,您应该好好去。

另外,如前所述,您需要了解SQL Injections。不是通过字符串连接构造查询,而是需要使用SQL Parameters