Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cmd.Parameters.Add("@eMail", SqlDbType.NVarChar(256)).Value = EmailID.Text;

Posted on 2014-01-24
8
Medium Priority
?
497 Views
Last Modified: 2014-01-27
cmd.Parameters.Add("@eMail", SqlDbType.NVarChar(256)).Value = EmailID.Text;


How do I do this so it is efficient and I could specify the type and the size of the Varchar

Email      nvarchar(256)  <--- this is how I have declared it in a the database
0
Comment
Question by:goodk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39808668
Try this:
cmd.Parameters.Add("@eMail", SqlDbType.NVarChar, 256).Value = EmailID.Text;

Open in new window

If you feel setting the value on the returned SqlParameter object is inefficient (I don't think it is) then you could set the value as well in 1 call, but you would need to create the SqlParameter yourself which means you need to provide all arguments (untested):
cmd.Parameters.Add(new SqlParameter("@eMail", SqlDbType.NVarChar, 256, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Current, EmailID.Text));

Open in new window

0
 

Author Comment

by:goodk
ID: 39809491
Thanks.
Is there also a way to not declare the size of the field?   like -1 or something like that?
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39809539
Well the size is optional in this construction so you can leave it out.
cmd.Parameters.Add("@eMail", SqlDbType.NVarChar).Value = EmailID.Text;

Open in new window

0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:goodk
ID: 39811018
cmd.Parameters.Add("@eMail", SqlDbType.NVarChar).Value = EmailID.Text;

This is what I did but do not understand why it keep truncating the stored value at 20 characters?

xxxxxxxxxxxxxxxxxxxx

12345678901234567890
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 39811625
I just tested it with a longer value and it was inserted in the database without a problem, can you share some more code?
            try {
                using (SqlConnection conn = new SqlConnection(@"Server=.\sqlexpress;Database=eeQ_28347912;uid=ee;pwd=ee;")) {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand()) {
                        cmd.Connection = conn;
                        cmd.CommandText = "INSERT INTO eMailTest VALUES (@eMail)";
                        cmd.Parameters.Add("@eMail", SqlDbType.NVarChar).Value = EmailID.Text;
                        cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }

Open in new window

0
 

Author Comment

by:goodk
ID: 39812665
CREATE PROCEDURE [dbo].[pAddUser](
      @UserName VARCHAR(20),
      @eMail VARCHAR(20),
      @Algorithm VARCHAR(16),
      @Pass VARCHAR(16)
)

The problem was the stored procedure.  Is there a way not to constraint it?

@eMail NVARCHAR,  ?
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 39812797
should be possible with nvarchar(max) if your version is new enough...
0
 

Author Closing Comment

by:goodk
ID: 39812917
Extremely good help.  Thanks a lot.
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

670 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