data source and SQL queries
Hey everyone,
I have been trying to retrieve data from my SQL Server database to allow the user to view his own info using data source, however when i add a where clause in the SQL select query I get an error msg.
here's my method and error message:
string u = Home.username; //this is a global string variable created in the Home.aspx file specifying the name of the user currently logged on
string myConnection = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn2 = new SqlConnection(myConnection);
string sql="SELECT* FROM LogIn where (username=u)"; //the where clause here isnt accepted
SqlDataAdapter myCommand = new SqlDataAdapter(sql, conn2);
DataSet ds = new DataSet();
myCommand.Fill(ds);
DataView source = new DataView(ds.Tables[0]);
MyDataGrid.DataSource = source ;
MyDataGrid.DataBind();
}
Error:
Server Error in '/WhatsYourWebsite' Application.
--------------------------------------------------------------------------------
Invalid column name 'u'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'u'.
Source Error:
Line 96: // Create and fill a DataSet.
Line 97: DataSet ds = new DataSet();
Line 98: myCommand.Fill(ds);
Line 99: // Bind MyDataGrid to the DataSet. MyDataGrid is the ID for the
Line 100: // DataGrid control in the HTML section.
Source File: c:\inetpub\wwwroot\whatsyourwebsite\main.aspx.cs Line: 98
Stack Trace:
[SqlException: Invalid column name 'u'.]
System.Data.SqlClient.SqlCommand.ExecuteRead er(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +723
System.Data.SqlClient.SqlCommand.ExecuteRead er(CommandBehavior behavior) +44
System.Data.SqlClient.SqlCommand.System.Data .IDbCommand.ExecuteReader(CommandBehavior behavior) +5
System.Data.Common.DbDataAdapter.FillFromCom mand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +304
System.Data.Common.DbDataAdapter.Fill(DataSe t dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77
System.Data.Common.DbDataAdapter.Fill(DataSe t dataSet) +38
WhatsYourWebsite.main.Button2_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\whatsyourwebsite\main.aspx.cs:9 8
System.Web.UI.WebControls.Button.OnClick(Eve ntArgs e) +108
System.Web.UI.WebControls.Button.System.Web. UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostB ackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameVa lueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
I would appreciate some help
Thanks
Susan
|