nvarchar and text.

Posted on 2006-05-18
Last Modified: 2012-05-05
I have the following code for a login script (I know it's not encrypted yet. One step at a time... ) and I get the error
" The data types text and nvarchar are incompatible in the equal to operator ". I have searched high and low for information and found none. ANY help would be GREATLY appreciated...

        /// <summary>
        /// Raises the System.Login event.
        /// </summary>
        /// <param name="sender">Object that raised the event.</param>
        /// <param name="e">EventArgs to handle the event.</param>
        protected void OnLogin(object sender, EventArgs e)
            // construct a new SQL Database Connection for the Lookup
            SqlConnection sqlConnection =
                new SqlConnection(ConfigurationManager.ConnectionStrings["SQL2005"].ToString());

            // populate the query from the employeeData object
            SqlCommand Query =
                new SqlCommand( employeeData.SelectCommand , sqlConnection);

            // Add the Login Name to the Query
            Query.Parameters.AddWithValue("@name", txtName.Text.ToCharArray() );

            // Encrypt The Password so that it is compared to the one
            // in the database properly, never sending plaintext values
            Query.Parameters.AddWithValue("@password", txtPassword.Text.ToCharArray()  );

            // Open the SqlConnection Object for Reading

            // Execute the Query and Retrieve the Results
            SqlDataReader Results =
                Query.ExecuteReader( CommandBehavior.SingleResult );

            //while (Results.Read())
            //    Session["User"] = Results["login"];
Question by:developernetwork
    LVL 142

    Accepted Solution

    the data type TEXT is not really good to work with, and should be avoided. as you seem to be using SQL Server 2005, the relevant fields should be transformed into VARCHAR (NVARCHAR) columns.
    Note that basically, VARCHAR(8000) resp NVARCHAR(4000) are the limits, with SQL Server 2005 you can use VARCHAR(MAX) resp NVARCHAR(MAX) instead which would give up to 2 GB of space for that column.

    Author Comment

    But when I request data using an SqlDataSource object, it works fine. It's never given me problems with TEXT before. Furthermore, why is text so bad? I had too many problems with nvarchar making fields too big when we didn't need them that big, and text seemed to solve all of those.
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    >But when I request data using an SqlDataSource object, it works fine. It's never given me problems with TEXT before.
    Then you never used the TEXT field for comparison yet.
    you just run into one of the limitations of the TEXT data type

    > Furthermore, why is text so bad?
    the handling of these fields is not the same (more complex) than VARCHAR/NVARCHAR fields.
    until the size of 8000/4000, they will work the same, but as from that size, updating is no longer trivial.

    >I had too many problems with nvarchar making fields too big when we didn't need them that big
    I think you misunderstood something possibly? can you explain what you mean with that,


    Author Comment

    With nvarchar, I had the problem of trailing spaces in all text boxes and input fields. My clients got so annoyed that they literally told me they hated my program because of it. Trying to size the fields properly just became too time consuming and impossible to do, and all of my fields ended up with trailing spaces.

    But an SqlDataSource WORKS. That is what makes no sense. The following code (quick made) works absolutely fine, but it is insufficient for what I need (since I need to actually work in code with the results).

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

    <html xmlns="" >
    <head runat="server">
        <title>Untitled Page</title>
        <form id="form1" runat="server">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SQL2005 %>"
                SelectCommand="SELECT id, login, password, birthday, zipcode, state, city, address, phone, role, lastname, firstname FROM dbo.Employees WHERE (login LIKE @login) AND (password LIKE @password)">
                    <asp:ControlParameter ControlID="TextBox1" Name="login" PropertyName="Text" />
                    <asp:ControlParameter ControlID="TextBox2" Name="password" PropertyName="Text" />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" /><br />
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                        SortExpression="id" />
                    <asp:BoundField DataField="login" HeaderText="login" SortExpression="login" />
                    <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
                    <asp:BoundField DataField="birthday" HeaderText="birthday" SortExpression="birthday" />
                    <asp:BoundField DataField="zipcode" HeaderText="zipcode" SortExpression="zipcode" />
                    <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
                    <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
                    <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
                    <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
                    <asp:BoundField DataField="role" HeaderText="role" SortExpression="role" />
                    <asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />
                    <asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    >With nvarchar, I had the problem of trailing spaces in all text boxes
    only if you chosed NCHAR with indeed keeps trailing spaces.
    NVARCHAR trims the trailing spaces automatically.

    As I said, when you use NVARCHAR instead of NCHAR (and NTEXT/TEXT), you should not have trailing spaces AND no code problems.

    Author Comment

    Oh Geez, I'm such a moron. You're right. it's a conversion issue.

    In my query, I was using "login = @login" instead of "login LIKE @login". So it was trying to compare the two together. Thank you very much for the help. I'll look into nvarchar a bit more, I was unaware nvarchar and nchar were different.

    Author Comment

    Just to ask, is nvarchar(MAX) going to slow my system down? is it bad to use it? Is the (#) the number of characters, or number of bits?

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now