Error using DateTime in C #

-1

I am working on visual c # and I need to pass the date as is to a database. Also when the user saves the data entered, he / she is asked if he / she wants to save more data By giving it if it is cleaned and the user can write again in the form

My code where I do everything I just explained is the following:

private void btnGuardar_Click(object sender, EventArgs e)
    {




        DialogResult result = new DialogResult();
        do {
            DateTime fecha = new DateTime();
            fecha = DateTime.Now;
            limpiarDatos();
            ObjDatos = new Datos(int.Parse(txtNumCuenta.Text), txtCarrera.Text, CbArea.SelectedItem.ToString(), int.Parse(txtEdad.Text), CbSexo.SelectedItem.ToString(), CBPadecimiento.SelectedItem.ToString(), TxtManejo.Text, CbAtendio.SelectedItem.ToString(), fecha);
            ObjDatos.guardar_datos();

            MessageBox.Show("Datos  guardados correctamente ¿guardar más datos?", "datos", MessageBoxButtons.YesNo);
        } while (result == DialogResult.Yes);
    }

}

and the error I get is this:

My class Data is this:

namespace Captura
{



class Datos
{


    private int NumCuenta;
    private String carrera;
    private String area;
    private int edad;
    private String sexo;
    private String padecimiento;
    private String manejo;
    private String atendio;
    private String fecha;


    SqlConnection conexion;
    SqlCommand comando;
    SqlDataAdapter adaptador;
    String consulta;

    public Datos(int NumCuenta, String carrera, String area, int edad, String sexo, String padecimiento, String manejo, String atendio, String fecha)
    {
        this.NumCuenta = NumCuenta;
        this.carrera = carrera;
        this.area = area;
        this.edad = edad;
        this.sexo = sexo;
        this.padecimiento = padecimiento;
        this.manejo = manejo;
        this.atendio = atendio;
        this.fecha = fecha;
    }
    public Datos()
    {

    }

    public void setNumCuenta(int NumCuenta) { this.NumCuenta = NumCuenta; }
    public int getNumCuenta() { return NumCuenta; }
    public void setCarrera(String carrera) { this.carrera = carrera; }
    public String getCarrera() { return carrera; }
    public void setArea(String area) { this.area = area; }
    public String getArea() { return area; }
    public void setEdad( int edad) { this.edad = edad; }
    public int getEdad() { return edad; }
    public void setSexo(String sexo) { this.sexo = sexo; }
    public String getSexo() { return sexo; }
    public void setPadecimiento(String padecimiento) { this.padecimiento = padecimiento; }
    public String getPadecimiento() { return padecimiento; }
    public void setManejo(String manejo) { this.manejo = manejo; }
    public String getManejo() { return manejo; }
    public void setAtendio(String atendio) { this.atendio = atendio; }
    public String getAtendio() { return atendio; }
    public void setfecha( String fecha) { this.fecha = fecha; }
    public String getfecha() { return fecha; }

    public void conectar()
    {
        conexion = new SqlConnection("Data Source = ALO\SQLEXPRESS; Initial Catalog = Medico; integrated security = true");
    }

    public void guardar_datos()
    {
        conectar();
        consulta = "insert into pacientes values(@num_cuenta,@carrera,@area,@edad,@sexo,@padecimiento,@manejo,@atendido,@fecha   )";
        comando = new SqlCommand(consulta, conexion);
        comando.CommandType = CommandType.Text;
        comando.Parameters.Clear();
        comando.Parameters.AddWithValue("@num_cuenta",getNumCuenta());
        comando.Parameters.AddWithValue("@carrera", getCarrera());
        comando.Parameters.AddWithValue("@area",getArea());
        comando.Parameters.AddWithValue("@edad",getEdad());
        comando.Parameters.AddWithValue("@sexo",getSexo());
        comando.Parameters.AddWithValue("@padecimiento",getPadecimiento());
        comando.Parameters.AddWithValue("@manejo",getManejo());
        comando.Parameters.AddWithValue("@atendido",getAtendio());
        comando.Parameters.AddWithValue("@fecha",getfecha());

        comando.Connection.Open();
        comando.ExecuteNonQuery();
        comando.Connection.Close();

    }

}




}
    
asked by Alonso Sánchez 11.12.2018 в 04:17
source

1 answer

1

1- DateTime.Now does not return a string, returns a DateTime object and your constructor of the Data class expects a String. A complete list with the formats for the toString method you can see it in this link link

String fecha = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");

2- For the user to save more data you must delete the do ... while and evaluate the response of the dialog, delete the form and let the user follow the normal flow, what you have in your code generates a loop that does not allow the user to save other data because if it answers 'Yes' the while takes you to the beginning of the loop, that is, there is a logic error.

3- In C # it is not necessary to write the getters and setters as in java, you can simplify it like this:

private String carrera {get; set;}

A very good explanation you can check it here on this community link (in English)

link

    
answered by 12.12.2018 в 23:12