【发布时间】: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