I have created a search form to locate a record based on a search value called license ID. I am able to pull the all the fields for that value. My question is I am trying to update any field for that record, example if I want to update the address field or the zip code field which all these field values are in text boxes. I get an syntax error at or near ""Lname"". Lname is the database field name for last name. This project is just a test project to get familiar with Postgres. Here is my source code. I am not sure what the problems is, the field name is correctly specified.
private void UpdateRecord()
{
try
{
NpgsqlConnection conn = Connection.getConnection();
conn.Open();
NpgsqlCommand cmd = new NpgsqlCommand("update info set \"Fname\" = :FirstName, set \"Lname\" = :LastName, set \"Address\" = :Address," +
"set \"City\" = :City, set \"State\" = State, set \"Zip\" = :Zip," +
"set \"PhoneNumber\" = :PhoneNumber where \"LicenceNumber\" = '" + LicenseID + "' ;", conn);
cmd.Parameters.Add(new NpgsqlParameter("FirstName", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters.Add(new NpgsqlParameter("LastName", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters.Add(new NpgsqlParameter("Address", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters.Add(new NpgsqlParameter("City", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters.Add(new NpgsqlParameter("State", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters.Add(new NpgsqlParameter("Zip", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters.Add(new NpgsqlParameter("PhoneNumber", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters[0].Value = txtFirstName.Text;
cmd.Parameters[1].Value = txtLastName.Text;
cmd.Parameters[2].Value = txtAddress.Text;
cmd.Parameters[3].Value = txtCity.Text;
cmd.Parameters[4].Value = cboState.Text;
cmd.Parameters[5].Value = txtZip.Text;
cmd.Parameters[6].Value = mtxtPhoneNumber.Text;
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
You only need "set" once per command.
NpgsqlCommand cmd = new NpgsqlCommand("update info set \"Fname\" = :FirstName, \"Lname\" = :LastName, \"Address\" = :Address," +
"\"City\" = :City, \"State\" = State, \"Zip\" = :Zip," +
"\"PhoneNumber\" = :PhoneNumber where \"LicenceNumber\" = '" + LicenseID + "' ;", conn);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With