Click here to Skip to main content
15,896,526 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written code in which large text is splitted into a sentence & stored in a table & if the sentence already exists in the table, then it's not stored. Here is my code:
C#
int index = Convert.ToInt32(e.CommandArgument.ToString());

                GridViewRow row = GridView1.Rows[index];

                var text = GridView1.Rows[index].Cells[0].Text;

                String[] sentences = Regex.Split(text, @"(?<=[.!?])\s+(?=\p{Lt})");

                System.Data.DataTable dt1 = new System.Data.DataTable();

                dt1.Columns.Add("SentenceText1");

                foreach (string value in sentences)
                {
                    dt1.Rows.Add(value);
                }
                SqlConnection con = new SqlConnection(conn);
                con.Open();
                string sql = "";
                string apply = "";
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    sql = sql + "INSERT INTO Sentences(SentenceText) SELECT '" + dt1.Rows[i]["SentenceText1"].ToString() + "' FROM Sentences WHERE NOT EXISTS(SELECT SentenceText FROM Sentences WHERE SentenceText='" + dt1.Rows[i]["SentenceText1"].ToString() + "')";
                    apply = apply + "insert into ContentSentences(ContentID)select ContentID from Contents"
                    SqlCommand cmd = new SqlCommand(sql, con,);
                    cmd.ExecuteNonQuery();
                }

It works but it stores the " " value in the table. So how can I resolve this problem?
Posted
Updated 27-Feb-15 0:45am
v3

1 solution

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
SQL
...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
C#
foreach (var value in sentences)
{
    if(!string.IsNullOrWhiteSpace(value))
        dt1.Rows.Add(value);
}
or the same thing using linq
C#
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
SQL
INSERT INTO Sentences(SentenceText) SELECT TOP 1 @sentenceText
FROM Sentences WHERE NOT EXISTS(SELECT SentenceText FROM Sentences WHERE SentenceText=@sentenceText)
 
Share this answer
 
v3
Comments
vatsaldesai 27-Feb-15 7:22am    
Thank You Chill60 i am will do what you say...
CHill60 27-Feb-15 7:26am    
Watch out - I got some bits wrong - I've just updated my solution. And I'll have another look at the sql too
vatsaldesai 27-Feb-15 7:32am    
yep i got same problem as per your said. i want to ask you but you did it before i ask.
CHill60 27-Feb-15 7:43am    
I must be psychic :) ... I've amended my solution again with sql that works when I tried it here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900