0

I am trying to insert data into a database, but I keep getting this error on this line of code:

command.ExecuteNonQuery();

This is the error I get:

The data types text and text are incompatible in the equal to operator.

Here is my method:

public List<QuestionsClass> updateQuestions(List<QuestionsClass> items)
{
    connection = new SqlConnection(connectionString);
    command = new SqlCommand(@"IF EXISTS(SELECT * FROM QuestionnaireAnswer WHERE questionID = @questionID AND community = @community AND lot = @lot)
                               BEGIN
                                    UPDATE QuestionnaireAnswer SET answer = @answer, textBox = @textBox WHERE questionID = @questionID AND community = @community AND lot = @lot
                               END
                               ELSE
                               BEGIN
                                    INSERT INTO QuestionnaireAnswer (questionID, answer, community, lot, textBox) VALUES (@questionID, @answer, @community, @lot, @textBox)
                               END");

    command.Parameters.Add("@questionID", System.Data.SqlDbType.Int);
    command.Parameters.Add("@answer", System.Data.SqlDbType.Text);
    command.Parameters.Add("@community", System.Data.SqlDbType.Text);
    command.Parameters.Add("@lot", System.Data.SqlDbType.Int);
    command.Parameters.Add("@textBox", System.Data.SqlDbType.Text);

    command.Connection = connection;
    connection.Open();

    for (int i = 0; i < items.Count; i++)
    {
        command.Parameters["@questionID"].Value = items[i].questionID;
        command.Parameters["@answer"].Value = items[i].answer;
        command.Parameters["@community"].Value = items[i].community;
        command.Parameters["@lot"].Value = items[i].lot;
        command.Parameters["@textBox"].Value = items[i].textBox;
        command.ExecuteNonQuery();
    }

    connection.Close();

    return items;
}

Here is my QuestionsClass

public class QuestionsClass
{
    public int questionID { get; set; }
    public string answer { get; set; }
    public string community { get; set; }
    public int lot { get; set; }
    public string textBox { get; set; }
}

answer, community and textBox are being populated with strings.

In my Database answer, community and textBox are Data types Text. What Am I doing wrong?

Please Help.

Johnie Karr
  • 2,744
  • 2
  • 35
  • 44
user979331
  • 11,039
  • 73
  • 223
  • 418
  • Where are you getting the error? Could you indicate the line? – Hanlet Escaño Dec 18 '15 at 18:26
  • 2
    `ntext`, `text`, and `image` data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use `nvarchar(max)`, `varchar(max)`, and `varbinary(max)` instead. [See details here](http://msdn.microsoft.com/en-us/library/ms187993.aspx). Once you've changed your column from `text` to `varchar(max)`, you should be able to compare it – marc_s Dec 18 '15 at 18:28
  • Why not use a `stored procedure`? – Suprabhat Biswal Dec 18 '15 at 18:29

0 Answers0