tags:

views:

51

answers:

2

I have a C# webservice on a Windows Server that I am interfacing with on a linux server with PHP. The PHP grabs information from the database and then the page offers a "more information" button which then calls the webservice and passes in the name field of the record as a parameter. So i am using a WHERE statement in my query so I only pull the extra fields for that record. I am getting the error:

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

Where 42 is the value from the name field from the database.

my query is

string selectStr = "SELECT name, castNotes, triviaNotes FROM tableName WHERE name =\"" + show + "\"";

I do not know if it is a problem with my query or something is wrong with the database, but here is the rest of my code for reference.

NOTE: this all works perfectly when I grab all of the records, but I only want to grab the record that I ask my webservice for.

public class ktvService  : System.Web.Services.WebService {

[WebMethod]
public string moreInfo(string show) {

    string connectionStr = "MyConnectionString";
    string selectStr = "SELECT name, castNotes, triviaNotes FROM tableName WHERE name =\"" + show + "\"";

    SqlConnection conn = new SqlConnection(connectionStr);
    SqlDataAdapter da = new SqlDataAdapter(selectStr, conn);
    DataSet ds = new DataSet();
    da.Fill(ds, "tableName");
    DataTable dt = ds.Tables["tableName"];

    DataRow theShow = dt.Rows[0];
    string response = "Name: " + theShow["name"].ToString() + "Cast: " + theShow["castNotes"].ToString() + " Trivia: " + theShow["triviaNotes"].ToString();

    return response;

} 

}

+1  A: 

Shouldn't the WHERE clause be WHERE name = '" + show + "'"; Strings should be enclosed in single quotes and not double quotes for SQL statements.

Also the System.Data.SqlClient namespace is for SQL Server and not MySQL. See MySQL official docs for connecting to MySQL via C#.

Mr Roys
+1 - can't believe I didn't see the MySQL part of the question... he tricked me!
John Rasch
+4  A: 

Quick solution:

I believe you need single quotes in your selectStr:

string selectStr = 
"SELECT name, castNotes, triviaNotes FROM tableName WHERE name = '" + show + "'";

More information:

In .NET, you'll want to be sure you close out any connections explicitly when you no longer need them. The easiest way to do this is to wrap using statements around any types that implement IDisposable, such as SqlConnection in this case:

using(SqlConnection conn = new SqlConnection(connectionStr))
{
    SqlDataAdapter da = new SqlDataAdapter(selectStr, conn);
    DataSet ds = new DataSet();
    da.Fill(ds, "tableName");
    DataTable dt = ds.Tables["tableName"];

    DataRow theShow = dt.Rows[0];
    string response = "Name: " + theShow["name"].ToString() + "Cast: " + theShow["castNotes"].ToString() + " Trivia: " + theShow["triviaNotes"].ToString();

    return response;
}

Additionally, it looks like your code could be easily subject to SQL injection. What if someone submits a form with the value: fake name' OR 1=1;DROP DATABASE someDbName;--?

You'll want to take advantage of SQL parameters, something like:

SqlCommand cmd = new SqlCommand(
  "SELECT name, castNotes, triviaNotes FROM tableName WHERE name = @show", conn);

cmd.Parameters.AddWithValue("@show", show);
John Rasch
Alrighty... replace all `SqlWhatever` with `MySqlWhatever` if you're using MySQL ;)
John Rasch
Thank you, but now I am getting the error: System.Data.SqlClient.SqlException: The data types text and varchar are incompatible in the equal to operator. After I switched it to the single quotes. That makes me think its a data type conversion issue but that makes no sense to me.
Ryan Sullivan
@Ryan - is your `name` column of type `text`? I suggest you change it to `varchar(max)` and it will work with the equals sign. Alternatively you can do `WHERE name LIKE @show` instead, but I don't recommend that approach.
John Rasch
Actually I just got it to work, Had to use LIKE instead of = in the query because the data in the table is stored as TEXT not varchar.Thank you for all the help, including the SQL injection solution, that was next on my list to figure out.
Ryan Sullivan
Why don't you recommend the approach? I am just learning about SQL, I do not know much.
Ryan Sullivan
@Ryan - you should generally reserve `text` columns for when you have large amounts of text you are storing... even then `varchar(max)` is probably still the best option if you're not searching it. Based on your data, it looks like `show` isn't going to grow that large so you could change the column type to `varchar(255)` and be fine. You may want to create a primary key out of that column if you expect it to be unique since you don't appear to be using a surrogate key.
John Rasch
I totally agree with you, but unfortunately I do not own the database, nor do I have permissions to edit it. I am strictly querying the database. Thank you for all your help!
Ryan Sullivan