Firstly you should be using a parameterized query - see
http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/[
^]
2 - this doesn't even compile, so I suspect you've made an error as you copied the code here (see
apply = apply...
3 - You are attempting to select the sentence text from the database instead of the column name: I think
...SELECT '" + dt1.Rows[i]["SentenceText1"].ToString() + "' FROM Sentences...
should be
...SELECT SentenceText FROM Sentences...
4 - The SQL doesn't look at all right - it appears to be inserting stuff back into the table that is already there. You need to review the way you are doing that.
5. Finally, as you are looping through the results anyway you can avoid blanks with something like
foreach (var value in sentences)
{
if(!string.IsNullOrWhiteSpace(value))
dt1.Rows.Add(value);
}
or the same thing using linq
foreach (var value in sentences.Where(value => !string.IsNullOrWhiteSpace(value)))
{
dt1.Rows.Add(value);
}
[edit - Points 3 and 4 were just wrong - sorry]
[edit 2]
After playing around with the sql (using parameterized query!!) I noticed that you will insert the value for as many times as you already have rows on the table. I amended the sql to just add a single row like this
INSERT INTO Sentences(SentenceText) SELECT TOP 1 @sentenceText
FROM Sentences WHERE NOT EXISTS(SELECT SentenceText FROM Sentences WHERE SentenceText=@sentenceText)