sean
sean

Reputation: 9258

Input string was not in a correct format

I'm new to Visual Studio 2010 and MySQL. I'm creating a form where I can Add the Employees. However, when I click the Add Button I got an error stating "Input string was not in a correct format."

This is the Screen shots:

enter image description here

enter image description here

Code:

private void button_adduser_Click(object sender, EventArgs e)
    {
        string MyConString = "SERVER=localhost;" + "DATABASE=timekeeping;" + "UID=root;" + "PASSWORD=admin;";
        MySqlConnection connection = new MySqlConnection(MyConString);
        MySqlCommand command = connection.CreateCommand();
        command.Connection = connection;
        using (MySqlConnection conn = new MySqlConnection(MyConString))
        {
            connection.Open();
            using (MySqlCommand com = connection.CreateCommand())
            {
                command.CommandText = "insert into users(fname, mname, lname, position, contactnumber, emailadd, birthday, username, password) values(@fname, @mname, @lname, @position, @contactnumber, @emailadd, @birthday, @username, @password)";
                command.Parameters.Add(new MySqlParameter("@fname", SqlDbType.VarChar));
                command.Parameters.Add(new MySqlParameter("@mname", SqlDbType.VarChar));
                command.Parameters.Add(new MySqlParameter("@lname", SqlDbType.VarChar));
                command.Parameters.Add(new MySqlParameter("@position", SqlDbType.VarChar));
                command.Parameters.Add(new MySqlParameter("@contactnumber", SqlDbType.VarChar));
                command.Parameters.Add(new MySqlParameter("@emailadd", SqlDbType.VarChar));
                command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.DateTime));
                command.Parameters.Add(new MySqlParameter("@username", SqlDbType.VarChar));
                command.Parameters.Add(new MySqlParameter("@password", SqlDbType.VarChar));
                command.Parameters["@fname"].Value = addfname.Text;
                command.Parameters["@mname"].Value = addmname.Text;
                command.Parameters["@lname"].Value = addlname.Text;
                command.Parameters["@position"].Value = addposition.Text;
                command.Parameters["@contactnumber"].Value = addcontact.Text;
                command.Parameters["@emailadd"].Value = addemail.Text;
                command.Parameters["@birthday"].Value = addbday.Text;
                command.Parameters["@username"].Value = addusername.Text;
                command.Parameters["@password"].Value = addpassword.Text;
                command.ExecuteNonQuery();
            }
        }

    }

Upvotes: 3

Views: 3564

Answers (3)

Johan
Johan

Reputation: 76537

The error is here:

command.Parameters["@birthday"].Value = addbday.Text;

addbday.Text is not in not a datetime field.

Even though you did declare it to be a datetime:

command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.DateTime));

Rewrite the offending line to:

 command.Parameters["@birthday"].Value = Convert.ToDateTime(addbday.Text);

Upvotes: 3

agent-j
agent-j

Reputation: 27913

I'd take a look at this line, since the parameter is declared as a datetime. Make sure it is in an expected format.

command.Parameters["@birthday"].Value = addbday.Text; 

Upvotes: 0

Gary Tsui
Gary Tsui

Reputation: 1755

An example, you declared your "birthday" as datetime command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.DateTime));

but, your value is being inserted as command.Parameters["@birthday"].Value = addbday.Text; // which im not sure if you are in the right format or not.

That's why you have format error.

Upvotes: 0

Related Questions