Solved

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

Posted on 2014-01-24
8
461 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
  • 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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

707 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

18 Experts available now in Live!

Get 1:1 Help Now