Link to home
Start Free TrialLog in
Avatar of goodk
goodkFlag for United States of America

asked on

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

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
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

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

Avatar of goodk

ASKER

Thanks.
Is there also a way to not declare the size of the field?   like -1 or something like that?
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

Avatar of goodk

ASKER

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

Avatar of goodk

ASKER

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,  ?
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of goodk

ASKER

Extremely good help.  Thanks a lot.