-1

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;
MethodMan
  • 18,137
  • 6
  • 33
  • 52
Midori_hige
  • 319
  • 3
  • 21

3 Answers3

1

As user Jeroen Heier pointed out in the comments.. I was missing a '

Here's the final query

String query = "INSERT INTO alumno (rut, nombre, apellido, fechaN) VALUES ('" + a.Rut + "','" + a.Nombre + "','" + a.Apellido + "','" + a.Fecha + "')";
         comando = new SqlCommand(query, x);

Using parameterized query:

String query = "INSERT INTO alumno (rut, nombre, apellido, fechaN) VALUES (@rut,@nombre,@apellido,@fechaN)";
         comando = new SqlCommand(query, x);

         comando.Parameters.AddWithValue("@rut", this.rut);
         comando.Parameters.AddWithValue("@nombre", this.nombre);
         comando.Parameters.AddWithValue("@apellido", this.apellido);
         comando.Parameters.AddWithValue("@fechaN", this.fecha);
Midori_hige
  • 319
  • 3
  • 21
  • 1
    Well that will save you today, but just for fun try to insert an alumno with a nombre that contains a single quote (O'Hara for example) – Steve May 31 '16 at 20:18
  • Yep, an error appears. Crappy method my teacher taught me. – Midori_hige May 31 '16 at 20:20
  • check this question. http://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements – Juan Carlos Oropeza May 31 '16 at 20:22
  • 1
    And this is not all. Sql Injection is a bigger problem. Letting your user type something that goes directly inside you query could be very costly for your customers. [This famous comic](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work) explain it well. Use parameterized queries. ALWAYS and show your teacher this question – Steve May 31 '16 at 20:22
0

You didnt mention rdbms, but looks like is case sensitive, so use double quotes for your field name same as your create table. Or change your field name to lower case in the table to "fechan".

"fechaN" (varchar 15)

"INSERT INTO alumno (rut, nombre, apellido, fechaN)
                                            ^^^^^^

And yes, use parameter instead, You are vulnerable to Sql Injection https://xkcd.com/327/

Juan Carlos Oropeza
  • 45,789
  • 11
  • 74
  • 113
-1
String query = "INSERT INTO alumno (rut, nombre, apellido, fechaN) VALUES     ('" + a.Rut + "','" + a.Nombre + "','" + a.Apellido + "'," + a.Fecha + ")";

Try without single quotes:

String query = "INSERT INTO alumno (rut, nombre, apellido, fechaN) 
                VALUES (" + a.Rut + "," + a.Nombre + "," + a.Apellido + ","   
                        + a.Fecha + ")";