I have problems inserting a textboxvalue into a mysql database - there's no errormessage and no inserting. What am I doing wrong
private void RegisterCustomer()
{
string firstname = txtfirstname.ToString();
OdbcConnection conn;
conn = new OdbcConnection(ConfigurationManager.ConnectionStrings["jConnString"].ConnectionString);
conn.Open();
string sql = "insert into klant (firstname) values (@firstname)";
OdbcCommand cmd = new OdbcCommand(sql, conn);
cmd.Parameters.Add("@firstname", OdbcType.VarChar).Value = firstname;
开发者_JAVA百科 try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Check.Text += ex.ToString() + sql;
}
finally
{
conn.Close();
conn.Dispose();
Check.Text += "OK";
}
}
According to MSDN.
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbccommand.parameters.aspx
When CommandType is set to Text, the .NET Framework Data Provider for ODBC does not support passing named parameters to an SQL statement or to a stored procedure called by an OdbcCommand. In either of these cases, use the question mark (?) placeholder.
So your query should be:
string sql = "insert into klant (firstname) values (?)"
If you have multiple parameters, they are set in the order you add them.
In addition, I think the line
string firstname = txtfirstname.ToString();
should read
string firstname = txtfirstname.Text();
But that is not what is causing your immediate problem.
"insert into klant values (firstname) values (@firstname)"
I think the right query would be:
"insert into klant values (@firstname)";
Your query:
string sql = "insert into klant values (firstname) values (@firstname)";
Is specifying values
twice. It should be in the form:
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
{VALUES | VALUE} ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]
So remove the extra values
and you should be good.
You have values twice. I've never seen it that way. You INSERT INTO table (columm_names...) VALUES (value1, 'value2',...)
edit: maybe you should try straight text and eliminate the box to see if it enters. At least you'll know where to look.
edit: I'd also echo my firstname variable to see what it has.
More accurately, it should be:
insert into klant (firstname) values (@firstname)
精彩评论