I have a Database named: "alumno" with the following columns:
"rut" (varchar 30) PK
"nombre" (varchar 30)
"apellido" (varchar 30)
"fechaN" (varchar 15)
all of them with not null function.
Whenever I type letter I get a SQLException saying the column "fechaN" has invalid name, but if I type numbers it runs just fine. Here is my code:
public Boolean insertar(SqlConnection x, alumno a) {
String query = "INSERT INTO alumno (rut, nombre, apellido, fechaN) VALUES ('" + a.Rut + "','" + a.Nombre + "','" + a.Apellido + "'," + a.Fecha + ")";
comando = new SqlCommand(query, x);
int saber = comando.ExecuteNonQuery();
if (saber > 0) {
return true;
}
return false;
}
con = new conexion();
SqlConnection x = con.conectar();
String rut = txtRut.Text.Trim();
String nombre = txtNombre.Text.Trim();
String apellido = txtApellido.Text.Trim();
String fecha = txtfecha.Text.Trim();
alumno a = new alumno(rut, nombre, apellido, fecha);
try {
if (a.insertar(x, a)) {
MessageBox.Show("Ok");
} else {
MessageBox.Show("Error...");
}
} catch (SqlException ex) {
MessageBox.Show("..." + ex.Message);
}
}
con.desconectar();
x = null;