bind gridview with datasource gives me invalid column
I want to bind my gridview with my SQL query that is my data source. I tried, but it gives me an error. I'm using my login ID as a where
clause in my select
query. Here is my code:
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 th开发者_如何学Pythone 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;
}
It gives me an Invalid Column exception at this line:
adapter.Fill(ds);
Could someone tell me where I'm going wrong?
problem is this statement and you missed single quote for string value and replace this statement with this one...
string queryString = "Select * from FILE_INFO WHERE ALLOCATED_TO = '" + user + "'";
Verify that your datagrid has "AutogenerateColumns" property set to "true"
精彩评论