使用datasource绑定gridview给我无效的列

时间:2011-03-15 07:28:32

标签: asp.net gridview sqldatasource

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

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;
    }

它在此行给出了一个无效列异常:

adapter.Fill(ds);

有人能告诉我哪里出错了吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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