【问题标题】:bind gridview with datasource gives me invalid column将gridview与数据源绑定给了我无效的列
【发布时间】:2011-03-15 07:28:32
【问题描述】:

我想将我的 gridview 与作为我的数据源的 SQL 查询绑定。我试过了,但它给了我一个错误。我在select 查询中使用我的登录ID 作为where 子句。这是我的代码:

string user;
protected void Page_Load(object sender, EventArgs e)
{
   Label1.Text = Session["unm"].ToString();
   user = Label1.Text;

   Response.Write(user);
   string queryString = "Select * from FILE_INFO WHERE ALLOCATED_TO = " + user + "";
   DataSet ds = GetData(queryString);
   if (ds.Tables.Count > 0)
   {
      GridView1.DataSource = ds;
      GridView1.DataBind();
   }
   else
   {
      Response.Write("Unable to connect to the database");
   }
}
DataSet GetData(String queryString)
{

string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";

         DataSet ds = new DataSet();

         SqlConnection con = new SqlConnection(connectionString);
         SqlDataAdapter adapter = new SqlDataAdapter(queryString, con);

         adapter.Fill(ds);
         return ds;
    }

它在这一行给了我一个 Invalid Column 异常:

adapter.Fill(ds);

谁能告诉我哪里出错了?

【问题讨论】:

  • 检查allocated_to是否在您的表中,如果是,则检查列的类型是否与您提供比较的数据相似。
  • 给出了完整的异常细节,比如确切的声明、错误代码等。另外我假设你在做 asp.**net** 对吗?

标签: asp.net gridview sqldatasource


【解决方案1】:

问题是这个语句,你错过了字符串值的单引号并将这个语句替换为这个......

string queryString = "Select * from FILE_INFO WHERE ALLOCATED_TO = '" + user + "'";

【讨论】:

    【解决方案2】:

    验证您的数据网格的“AutogenerateColumns”属性是否设置为“true”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2013-03-27
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多