Får felmedelande när jag försöker lägga in en rad i en MySQL databas via .net, C#. Hade varit oerhörd taksam om någon hade något svar på problemet. När man använder MySQL-connectorn ska man använda ? istället för @, kanske är så här också. Testa att byta till ? jag bytte ut @ mot ?, men det hjälpte tyvärr inte. Fick ett nytt felmedelande. Problemet är nu löst, följande ska man skriva för MySQL med parametrar:Connection mot MySQL
Har följande kod:
protected void CreateUser_Click(object sender, EventArgs e)
{
string mySQL = "insert into Admin (Fornamn, Efternamn) values (@firstname, @lastname)";
OdbcConnection myConnection = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};SERVER=xxx;DATABASE=xxx;UID=xxx;PWD=xxx;");
OdbcCommand myCommand = new OdbcCommand(mySQL, myConnection);
myCommand.Parameters.Add(new OdbcParameter("@firstname", OdbcType.VarChar, 15));
myCommand.Parameters["@firstname"].Value = Firstname.Text;
myCommand.Parameters.Add(new OdbcParameter("@lastname", OdbcType.VarChar, 15));
myCommand.Parameters["@lastname"].Value = Lastname.Text;
myCommand.Connection.Open();
if( myCommand.Connection.State == ConnectionState.Open )
{
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
Resultat.Text = "Uppgifterna har nu lagrats i databasen.";
}
else
{
Resultat.Text = "Misslyckats";
}
Får följande felmedelande:
ERROR [HYT00] [MySQL][ODBC 3.51 Driver][mysqld-5.0.18-standard-log]Column 'Fornamn' cannot be null
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Odbc.OdbcException: ERROR [HYT00] [MySQL][ODBC 3.51 Driver][mysqld-5.0.18-standard-log]Column 'Fornamn' cannot be null
Source Error:
Line 35: if( myCommand.Connection.State == ConnectionState.Open )
Line 36: {
Line 37: myCommand.ExecuteNonQuery();
Line 38: myCommand.Connection.Close();
Line 39:
Source File: d:\users\www\g19895\u19895\www\AdminAspNet\AdminNewLogin.aspx.cs Line: 37
Stack Trace:
[OdbcException (0x80131937): ERROR [HYT00] [MySQL][ODBC 3.51 Driver][mysqld-5.0.18-standard-log]Column 'Fornamn' cannot be null]
System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) +35
System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) +1735
System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) +60
System.Data.Odbc.OdbcCommand.ExecuteNonQuery() +87
_Default.CreateUser_Click(Object sender, EventArgs e) in d:\users\www\g19895\u19895\www\AdminAspNet\AdminNewLogin.aspx.cs:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102Sv: Connection mot MySQL
/JohanSv:Connection mot MySQL
Något annat förslag?
ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.18-standard-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'firstname, 'ddddddddddd'lastname)' at line 1
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Odbc.OdbcException: ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.18-standard-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'firstname, 'ddddddddddd'lastname)' at line 1
Source Error:
Line 35: if( myCommand.Connection.State == ConnectionState.Open )
Line 36: {
Line 37: myCommand.ExecuteNonQuery();
Line 38: myCommand.Connection.Close();
Line 39:
Source File: d:\users\www\g19895\u19895\www\AdminAspNet\AdminNewLogin.aspx.cs Line: 37
Stack Trace:
[OdbcException (0x80131937): ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.0.18-standard-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'firstname, 'ddddddddddd'lastname)' at line 1]
System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode) +35
System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod) +1735
System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader) +60
System.Data.Odbc.OdbcCommand.ExecuteNonQuery() +87
_Default.CreateUser_Click(Object sender, EventArgs e) in d:\users\www\g19895\u19895\www\AdminAspNet\AdminNewLogin.aspx.cs:37
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102Sv: Connection mot MySQL
string mySQL = "insert into Admin (Fornamn, Efternamn) values (?, ?)";
OdbcConnection myConnection = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};SERVER=XXX;DATABASE=XXX;UID=XXX;PWD=XXX;");
OdbcCommand myCommand = new OdbcCommand(mySQL, myConnection);
myCommand.Parameters.Add(new OdbcParameter("?firstname", OdbcType.VarChar, 15));
myCommand.Parameters["?firstname"].Value = Firstname.Text;
myCommand.Parameters.Add(new OdbcParameter("?lastname", OdbcType.VarChar, 15));
myCommand.Parameters["?lastname"].Value = Lastname.Text;