?
Solved

nvarchar and text.

Posted on 2006-05-18
7
Medium Priority
?
2,009 Views
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
            sqlConnection.Open();

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

            //while (Results.Read())
            //{
            //    Session["User"] = Results["login"];
            //}
        }
0
Comment
Question by:developernetwork
  • 4
  • 3
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16713524
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.
0
 

Author Comment

by:developernetwork
ID: 16713640
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.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16713730
>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,

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:developernetwork
ID: 16713762
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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)">
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="login" PropertyName="Text" />
                <asp:ControlParameter ControlID="TextBox2" Name="password" PropertyName="Text" />
            </SelectParameters>
        </asp:SqlDataSource>
   
    </div>
        <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"
            DataSourceID="SqlDataSource1">
            <Columns>
                <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" />
            </Columns>
        </asp:GridView>
    </form>
</body>
</html>
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16713775
>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.
0
 

Author Comment

by:developernetwork
ID: 16713793
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.
0
 

Author Comment

by:developernetwork
ID: 16714317
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?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

809 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