I know the issue and I searched online. I know that this exception is thrown when my value length is greater than the length defined in the SQL Server table.
This is my C# code:
using (SqlCommand cmdInsertUser = new SqlCommand("INSERT INTO [Accounts] VALUES (@username, @password, @email, @fullname, @money, @active, @activationCode, @registrationDate);", con))
{
Random random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
activationCode = (Enumerable.Repeat(chars, 6).Select(s => s[random.Next(s.Length)]).ToArray()).ToString();
con.Open();
cmdInsertUser.Parameters.AddWithValue("@username", txtSignUpUsername.Text);
cmdInsertUser.Parameters.AddWithValue("@password", txtSignUpPassword.Text);
cmdInsertUser.Parameters.AddWithValue("@email", txtSignUpEmail.Text);
cmdInsertUser.Parameters.AddWithValue("@fullname", txtSignUpFullName.Text);
cmdInsertUser.Parameters.AddWithValue("@money", Convert.ToInt32(txtSignUpMoney.Text));
cmdInsertUser.Parameters.AddWithValue("@active", 0);
cmdInsertUser.Parameters.AddWithValue("@activationCode", activationCode);
cmdInsertUser.Parameters.AddWithValue("@registrationDate", DateTime.Now.ToShortDateString());
cmdInsertUser.ExecuteNonQuery();
con.Close();
}
This is my SQL Server table structure:
CREATE TABLE [dbo].[Accounts]
(
[Id] INT IDENTITY (1, 1) NOT NULL,
[Username] VARCHAR(12) NOT NULL,
[Password] VARCHAR(15) NOT NULL,
[Email] VARCHAR(50) NOT NULL,
[FullName] VARCHAR(50) NOT NULL,
[Money] INT NOT NULL,
[Active] BIT NOT NULL,
[ActivationCode] VARCHAR(6) NOT NULL,
[RegistrationDate] VARCHAR(50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
The values that I enter:
- Username = daniel7
- Password = 123456
- Email = da************16@email.com (my real mail is given instead of *)
- FullName = Daniel Ohayon
- Money = 0
- Active = 0
- ActivationCode = something random with 6 chars
- RegistrationDate = DateTime.Now.ToShortDateString()
PLEASE HELP ME!