I’m writing to StackOverflow about a recurring problem I have with ASP Core and C sharp WinForm. Each table exists with columns in my database, Vertica. The company where I work uses the Vertica database. I use System.Data.Odbc. Every time I execute a save request, I
have this message :
System.Data.Odbc.OdbcException: ‘ERROR [42703] ERROR 2624: Column “Username” does not exist
If I have, for example, 3 columns like Username, Password and Site, it will be Username does not exist.
If I remove Username from the INSERT, it will be Password does not exist, etc.
I use System.Data.Odbc. Every time I execute a save request, I have this message.
The command read() is working, I can retrieve data from the database. If I execute a SELECT query, that works.
Here is the code :
Anyone not works
Code Simplify for testing:
public void Save(SwissportStateModel model)
{
OdbcConnection conn = _db.GetConnection();
string query = "Insert into flexautomatisation.tbl_swissport_test (Username) VALUES
(@Username)";
conn.Open();
OdbcCommand cmd = new OdbcCommand(query, conn);
cmd.Parameters.AddWithValue("@Username", "John");
cmd.ExecuteNonQuery();
conn.Close();
}
Normal code :
DateTime localDate = DateTime.Now;
string cmdstring = "Insert into flexautomatisation.tbl_swissport_state (Environment,
Schedule, IsLaunch, Created_at) VALUES (@Environment, @Schedule, @IsLaunch,
@Created_at)";
using (OdbcConnection conn = _db.GetConnection())
{
conn.Open();
using (OdbcCommand cmd = new OdbcCommand(cmdstring, conn))
{
cmd.Parameters.AddWithValue("@Environment", model.Environment);
cmd.Parameters.AddWithValue("@Schedule", model.Schedule);
cmd.Parameters.AddWithValue("@IsLaunch", model.IsLaunch);
cmd.Parameters.AddWithValue("@Created_at", localDate);
cmd.ExecuteNonQuery();
}
conn.Close();
}
Thank you for your help.
Update:
Here are the table definitions.
For table 1:
CREATE TABLE flexautomatisation.tbl_swissport_test ( Id IDENTITY ( 1 ) , Username varchar(255) NULL );
For table 2:
CREATE TABLE flexautomatisation.tbl_swissport_state ( Id IDENTITY ( 1 ) , Environment int NOT NULL, Schedule int DEFAULT 5, IsLaunch boolean DEFAULT false, Created_at timestamp DEFAULT NULL::timestamp );
5
With the help of Yong Shun, I found the solution to this problem.
Effectively, We need to use a VALUES like this: VALUES(?)
The solution can look like this.
OdbcConnection conn = _db.GetConnection();
string query = "Insert into flexautomate.tbl_employee_test
(Username,Name,Age) VALUES (?,?,?)";
conn.Open();
OdbcCommand cmd = new OdbcCommand(query, conn);
cmd.Parameters.AddWithValue("@Username", "John");
cmd.Parameters.AddWithValue("@Name", "Doe");
cmd.Parameters.AddWithValue("@Age", 45);
cmd.ExecuteNonQuery();
conn.Close();
It works.
Thank you for being so helpful.