Lesley Peters
Lesley Peters

Reputation: 409

asp.net weird error creating a DateTime

I want to create an account with the next code I've written in asp.net

But everytime when I create the datetime it's throwing me into the catch block, now I've used simple values like:

DateTime birthdate = new DateTime(1, 2, 2003);

but when I check the debugger, it's throwing me out of the try block at this line and I really don't understand why because Visual studio doesn't give me any errors so what can go wrong here???

try
{
    // male/female check
    string sex = "";
    if (rbMale.Checked)
    {
        sex = "Male";
    }
    else if (rbFemale.Checked)
    {
        sex = "Female";
    }
    // birthday
    int bdday = Convert.ToInt32(tbBdayDay.Value.ToString());
    int bdmonth = Convert.ToInt32(cbBdayMonth.Value.ToString());
    int bdyear = Convert.ToInt32(tbBdayYear.Value.ToString());

    //this line is pushing me into the catch block!! :(
    DateTime birthdate = new DateTime(1, 2, 2003);
    // username
    string name = tbFirstName.Value.ToString() + " " + tbLastName.Value.ToString();
    // fileshare link: www.filesharelink.com/+'username'+'random number'
    Random random = new Random();
    int randomNumber = random.Next(0, 10001);
    string filesharelink = "www.filesharelink.com/" + tbFirstName.Value.ToString() + randomNumber;
    // email
    string email = tbEmail.Value.ToString();
    // password
    string password = tbPassword1.Value.ToString();
    //returns true if account is succesfully registered
    if (administration.query.RegisterAccount(sex, birthdate, name, filesharelink, email, password))
    {
        ClientScript.RegisterStartupScript(this.GetType(), "titel", "alert('Account created succesfully, you can now log in!');", true);
    }
}
catch
{
    ClientScript.RegisterStartupScript(this.GetType(), "titel", "alert('Error creating your account, please try again.');", true);
}

Upvotes: 0

Views: 68

Answers (2)

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

Arguments of DateTime constructor you're trying to use is year, month, day, but you're providing them in incorrect order.

So literally you're trying to create 2003 February of 0001 year in your code.

Upvotes: 5

DGibbs
DGibbs

Reputation: 14608

new DateTime(1, 2, 2003);

Expects parameters in the format year, month day.

Your parameters are out of range.

Try using:

new DateTime(2003, 2, 1);

Assuming the month/day are the correct way around.

Upvotes: 3

Related Questions