Monday, March 12, 2012

Invalid column name from c# sql query

Hi

I have the following problem. I am trying to get some data from a database which matches the name in a session from a previous page:

e.g.


SqlCommand menubar = new SqlCommand("Select pernme from Person where pernme = " + (string)Session["tbname"], sqlConn);
SqlDataAdapter dataAdapter5 = new SqlDataAdapter();
dataAdapter5.SelectCommand = menubar;
DataSet dataSet5 = new DataSet();
dataAdapter5.Fill(dataSet5);
DataTable selcartest4 = dataSet5.Tables["table"];
if (selcartest4.Rows.Count != 0)

The session is called tbname and in that session is a users name

however insetad of doing the nornal thing and retrieving the data in the sql database table matching that name it comes up with the following error message:

System.Data.SqlClient.SqlException: Invalid column name 'jamie'

this is weird as the 'jamie' is the name in the session from the previous page and in fact not a column name at all the column name is pernme

I am totally stuck any help would eb great thanks

J

Change


SqlCommand menubar = new SqlCommand("Select pernme from Person where pernme = " + (string)Session["tbname"], sqlConn);

To


SqlCommand menubar = new SqlCommand("Select pernme from Person where pernme = '" + (string)Session["tbname"] + "'", sqlConn);

You need to put single quote around string.

|||

Thankyou that work

appreciated!

cheers

No comments:

Post a Comment