Solved

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

Posted on 2014-01-24
8
487 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
Technology Partners: 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: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 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

751 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