Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

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
0
goodk
Asked:
goodk
  • 4
  • 4
1 Solution
 
Robert SchuttSoftware EngineerCommented:
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
 
goodkAuthor Commented:
Thanks.
Is there also a way to not declare the size of the field?   like -1 or something like that?
0
 
Robert SchuttSoftware EngineerCommented:
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
goodkAuthor Commented:
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
 
Robert SchuttSoftware EngineerCommented:
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
 
goodkAuthor Commented:
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
 
Robert SchuttSoftware EngineerCommented:
should be possible with nvarchar(max) if your version is new enough...
0
 
goodkAuthor Commented:
Extremely good help.  Thanks a lot.
0

Featured Post

Independent Software Vendors: 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!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now