0

I have an error I don't understand how to get rid of. I want to compare a string consisting of: ID + " " + date + " " + time + ":00" to a string in the database (data type = text).

Here's an example of what I fetch from my gridview:
1 + " " + 2010-07-07 + " " + 10:00 + ":00" (i.e. 1 2010-07-07 10:00:00)
and this is what I want to compare it to:
1 2010-07-07 10:00:00
The error message says "Incorrect syntax near '2010'." What did I miss? The values I get when I try to debug are the exakt same values that I want to compare.

Here's the code (some of it in swedish *sorry*, some c# as well as sql:

int möteID;   //=ID
        string datum; //=date
        string tid;   
        string starttid;  //=the time I'm using
        string sluttid;
        string tillfälleID; //=the string from the gridview

        StringBuilder str = new StringBuilder();
        int index = int.Parse(e.CommandArgument.ToString());
        möteID = Convert.ToInt32(GridViewFöreslagnaMöten.Rows[index].Cells[0].Text);
        datum = Convert.ToString(GridViewFöreslagnaMöten.Rows[index].Cells[2].Text); 
        tid = Convert.ToString(GridViewFöreslagnaMöten.Rows[index].Cells[3].Text);
        starttid = tid.Substring(1, 5);
        sluttid = tid.Substring(9, 5);
        tillfälleID = Convert.ToString(möteID + " " + datum + " " + starttid + ":00");

        SqlConnection conn = new SqlConnection(config);
        conn.Open();

        string sql = "SELECT medlemID FROM MedlemTillfalle WHERE MedlemTillfalle.tillfalleID = " + tillfälleID;

        SqlCommand comm = new SqlCommand(sql, conn);
        SqlDataReader dr = comm.ExecuteReader(); //Incorrect syntax near '2010'

...and I'm aware I'm not using parameters... I'll be getting there soon, still a newbie.

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by BonaDrag
0

Here's a possible explanation I found in another forum:
"You can't use text columns in an equal operation of a WHERE clause. If you are using SQL Express or SQL Server 2005, change your column type from text to either varchar(max) or nvarchar(max)".
To be continued...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.