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."
我是Visual Studio 2010和MySQL的新手。我正在创建一个可以添加员工的表单。然而,当我点击Add按钮时,我得到了一个错误,表明“输入字符串的格式不正确”。
This is the Screen shots:
这是屏幕截图:
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();
}
}
}
3 个解决方案
#1
3
The error is here:
这里的错误是:
command.Parameters["@birthday"].Value = addbday.Text;
addbday.Text is not in not a datetime field.
addbday。文本不属于datetime字段。
Even though you did declare it to be a datetime:
即使您声明它是一个datetime:
command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.DateTime));
Rewrite the offending line to:
将违规行重写为:
command.Parameters["@birthday"].Value = Convert.ToDateTime(addbday.Text);
#2
0
An example, you declared your "birthday" as datetime command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.DateTime));
例如,您将您的“生日”声明为datetime命令。添加(新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.
但是,您的值正在被插入为command.Parameters["@birthday"]。值= addbday.Text;//我不确定你的格式是否正确。
That's why you have format error.
这就是为什么会出现格式错误。
#3
0
I'd take a look at this line, since the parameter is declared as a datetime. Make sure it is in an expected format.
我将查看这一行,因为参数被声明为datetime。确保它是预期的格式。
command.Parameters["@birthday"].Value = addbday.Text;
#1
3
The error is here:
这里的错误是:
command.Parameters["@birthday"].Value = addbday.Text;
addbday.Text is not in not a datetime field.
addbday。文本不属于datetime字段。
Even though you did declare it to be a datetime:
即使您声明它是一个datetime:
command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.DateTime));
Rewrite the offending line to:
将违规行重写为:
command.Parameters["@birthday"].Value = Convert.ToDateTime(addbday.Text);
#2
0
An example, you declared your "birthday" as datetime command.Parameters.Add(new MySqlParameter("@birthday", SqlDbType.DateTime));
例如,您将您的“生日”声明为datetime命令。添加(新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.
但是,您的值正在被插入为command.Parameters["@birthday"]。值= addbday.Text;//我不确定你的格式是否正确。
That's why you have format error.
这就是为什么会出现格式错误。
#3
0
I'd take a look at this line, since the parameter is declared as a datetime. Make sure it is in an expected format.
我将查看这一行,因为参数被声明为datetime。确保它是预期的格式。
command.Parameters["@birthday"].Value = addbday.Text;