The data types ntext and nvarchar are incompatible in the equal to operator

22.5k Views Asked by At

I have a problem and i dont know how to fix it.

I have a simple table in database

CREATE TABLE [dbo].[home] (
    [Id]   INT   NOT NULL,
    [text] NTEXT NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

I`m using FormView in visual studio 2012, The FormView is connected with the Home table of the database and there is edit/update/delete options on.

The problem is that when I`m trying to Update the text in te database there is an error

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

A little help please..

This is the code I use to write in the DB :

    <%@ Page Title="" Language="C#" MasterPageFile="~/admin/adminmaster.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSource1">
        <EditItemTemplate>
            text:
            <asp:TextBox Text='<%# Bind("text") %>' runat="server" ID="textTextBox" /><br />
            Id:
            <asp:Label Text='<%# Eval("Id") %>' runat="server" ID="IdLabel1" /><br />
            <asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateButton" CausesValidation="True" />&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="UpdateCancelButton" CausesValidation="False" />
        </EditItemTemplate>
        <InsertItemTemplate>
            text:
            <asp:TextBox Text='<%# Bind("text") %>' runat="server" ID="textTextBox" /><br />
            Id:
            <asp:TextBox Text='<%# Bind("Id") %>' runat="server" ID="IdTextBox" /><br />
            <asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" />&nbsp;<asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" />
        </InsertItemTemplate>
        <ItemTemplate>
            text:
            <asp:Label Text='<%# Bind("text") %>' runat="server" ID="textLabel" /><br />
            Id:
            <asp:Label Text='<%# Eval("Id") %>' runat="server" ID="IdLabel" /><br />
            <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditButton" CausesValidation="False" />&nbsp;<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" ID="DeleteButton" CausesValidation="False" />&nbsp;<asp:LinkButton runat="server" Text="New" CommandName="New" ID="NewButton" CausesValidation="False" />
        </ItemTemplate>
    </asp:FormView>
    <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConflictDetection="CompareAllValues" ConnectionString='<%$ ConnectionStrings:ConnectionString_SQLServer %>' DeleteCommand="DELETE FROM [home] WHERE [Id] = @original_Id AND [text] = @original_text" InsertCommand="INSERT INTO [home] ([Id], [text]) VALUES (@Id, @text)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [Id], [text] FROM [home]" UpdateCommand="UPDATE [home] SET [text] = @text WHERE [Id] = @original_Id AND [text] = @original_text">
        <DeleteParameters>
            <asp:Parameter Name="original_Id" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="original_text" Type="String"></asp:Parameter>
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Id" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="text" Type="String"></asp:Parameter>
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="text" Type="String"></asp:Parameter>
            <asp:Parameter Name="original_Id" Type="Int32"></asp:Parameter>
            <asp:Parameter Name="original_text" Type="String"></asp:Parameter>
        </UpdateParameters>
    </asp:SqlDataSource>
</asp:Content>

And this is where I want to get the text form DB and write it in to Label :

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


            string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString_SQLServer"].ConnectionString;

            string text = "SELECT text FROM home WHERE id=1";

            using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(text, conn);
                box1_text.Text = (string)cmd.ExecuteScalar();

            }
        }


}
5

There are 5 best solutions below

2
On
drrr.Close();

//SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["rashmi"].ToString());

SqlCommand cmd1 = new SqlCommand("insert into registration(fullname,address,contact,village,city,district,state,email,password) values(@fullname,@address,@contact,@village,@city,@district,@state,@email,@password)");
con.Open();
cmd1.Connection = con;
cmd1.CommandType = CommandType.Text;

cmd1.Parameters.AddWithValue("@fullname", fullname.Text.Trim().ToString());
cmd1.Parameters.AddWithValue("@address", address.Text.Trim().ToString());
cmd1.Parameters.AddWithValue("@contact", contact.Text.Trim().ToString());
cmd1.Parameters.AddWithValue("@village", village.Text.Trim().ToString());
cmd1.Parameters.AddWithValue("@city", city.Text.Trim().ToString());
cmd1.Parameters.AddWithValue("@district", district.Text.Trim().ToString());
cmd1.Parameters.AddWithValue("@state", state.Text.Trim().ToString());
//cmd1.Parameters.AddWithValue("@crop", crop.Text.Trim().ToString());
cmd1.Parameters.AddWithValue("@email", email.Text.Trim().ToString());
cmd1.Parameters.AddWithValue("@password", password.Text.Trim().ToString());

int row;
try
{
    row = cmd1.ExecuteNonQuery();
    if (row > 0)
    {
        // lbltxt.Text = "<b>data sucess fully enter</b>";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Registration Successfully');window.location ='Registration.aspx';", true);

        //Response.Write("data sucess fully enter");
        // Response.Redirect("login.aspx");
    }
    else
    {
        lbltxt.Text = "<b>data sucess fully enter</b>";
        lbltxt.ForeColor = System.Drawing.Color.DarkSlateBlue;
    }

    //else
    //{
    //    Response.Write("data not insert");
    //}
}
catch (Exception)
{
    throw;
}
finally
{
    if (con.State == ConnectionState.Open)
        con.Close();
}
0
On

If this is happening to you with DBeaver, it appears if you press 'Cancel' after a failed 'Save', the row will get saved regardless. I guess this is a bug in the software.

2
On

In your query put a convert around any ntext fields to convert them to nvarchar(max)

for example: convert(nvarchar(max), nTextField)

0
On

To avoid getting above exception execute following query in sql server-

Alter table home 
Alter column text nvarchar(max)
GO

I hope my solution is useful for you.

0
On

Can Try with LIKE

For Example

SELECT * FROM cor.Computer WHERE description LIKE 'HP NOTEBOOK'