I’m having problems with the following method…
It works for the ‘@user’ bit (i.e. it takes in the user variable and works within the sql command).
But the @column variable still is not working somehow.
It’s probably something to do with the way I’ve phrased the actual sql statement.
At the moment, the column value (‘aisles’) is hard coded into the sql statement, as you can see below.
This will return 7, which is correct… but if I try to use @column instead of ‘aisles’, and pass the string “aisles” into the column var, it returns 0.
I don’t know why it is accepting one variable and working, but not accepting the other.
Hope I’m being clear enough.
Thanks for any help with this!!
Here's the method:
Expand|Select|Wrap|Line Numbers
- static public int findColumnValue(string user, string column)
- {
- Int32 columnValue = 0;
- string connString = "Initial Catalog=Warehouse;Data Source=localhost;Integrated Security=no;user id=Nigel";
- string sql =
- "SELECT @column FROM models WHERE username LIKE @user";
- using (SqlConnection conn = new SqlConnection(connString))
- {
- SqlCommand cmd = new SqlCommand(sql, conn);
- SqlParameter userParam = new SqlParameter();
- userParam.ParameterName = "@user";
- userParam.Value = user;
- SqlParameter columnParam = new SqlParameter();
- columnParam.ParameterName = "@column";
- columnParam.Value = column;
- cmd.Parameters.Add(userParam);
- cmd.Parameters.Add(columnParam);
- try
- {
- conn.Open();
- columnValue = (Int32)cmd.ExecuteScalar();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- return (int)columnValue;
- }