Error clicking button to save data to database

Asked

Viewed 59 times

-1

Good morning guys, I’m trying to save some data in the database, for a button of windows form C# and this error appears. I have tried everything but the error persists. I did the print and pasted the code of Forms and sql, please someone could help me?

inserir a descrição da imagem aqui

strSql ="insert into Paciente 
         values(@IDPACIENTE,@NOMEPACIENTE,@SOBRENOME,@DATANASCIMENTO,
                @GENERO,@CPF,@UFRG,@EMAIL,@CELULAR,@TELEFONEFIXO,
                @PRONTUARIO,@CONVENIO,@CARTEIRINHACONVENIO,@VALIDADECARTEIRINHA)";

sqlCon = new SqlConnection(strCon);
SqlCommand comando = new SqlCommand(strSql, sqlCon);

comando.Parameters.Add("@IDPACIENTE", SqlDbType.Int).Value = textBox_ID.Text;
comando.Parameters.Add("@NOMEPACIENTE", SqlDbType.VarChar).Value = textBox_Nome.Text;
comando.Parameters.Add("@SOBRENOME", SqlDbType.VarChar).Value = textBox_SOBRENOME.Text;
comando.Parameters.Add("@DATANASCIMENTO", SqlDbType.DateTime).Value = maskedTextBox_DataNascimento.Text;
comando.Parameters.Add("@GENERO", SqlDbType.Char).Value =comboBox_Genero.Text;
comando.Parameters.Add("@CPF", SqlDbType.Char).Value =maskedTextBox_CPF.Text;
comando.Parameters.Add("@UFRG", SqlDbType.VarChar).Value = textBox_UFRG.Text;
comando.Parameters.Add("@EMAIL", SqlDbType.VarChar).Value =textBox_EMAIL.Text;
comando.Parameters.Add("@CELULAR", SqlDbType.VarChar).Value =maskedTextBox_CELULAR.Text;
comando.Parameters.Add("@TELEFONEFIXO", SqlDbType.VarChar).Value =maskedTextBox_TELEFONE.Text;
comando.Parameters.Add("@PRONTUARIO", SqlDbType.VarChar).Value =textBox_Prontuario.Text;
comando.Parameters.Add("@CONVENIO", SqlDbType.VarChar).Value =textBox_CONVENIO.Text;
comando.Parameters.Add("@CARTEIRINHACONVENIO", SqlDbType.VarChar).Value =textBox_CarteirinhaConvenio.Text;
comando.Parameters.Add("@VALIDADECARTEIRINHA", SqlDbType.DateTime).Value =maskedTextBox_ValidadeCarteirinha.Text;

try
{
    sqlCon.Open();
    comando.ExecuteNonQuery();
    MessageBox.Show("Cadastro Efetuado Com Sucesso");
}
catch (Exception ex)
{
    
}
finally
{
    sqlCon.Close();
    

CREATE TABLE Paciente(
    IDPACIENTE INT IDENTITY PRIMARY KEY NOT NULL,
    NOMEPACIENTE VARCHAR (50),
    SOBRENOME  VARCHAR (50),
    DATANASCIMENTO DATETIME,
    GENERO CHAR(1),
    CPF CHAR(12) NOT NULL,
    UFRG  VARCHAR (50),
    EMAIL VARCHAR (100),
    CELULAR VARCHAR (100),
    TELEFONEFIXO VARCHAR(10),
    PRONTUARIO VARCHAR(100),
    CONVENIO VARCHAR(100),
    CARTEIRINHACONVENIO VARCHAR(100),
    VALIDADECARTERINHA  DATETIME
)
  • The error says that you cannot implicitly convert a string to date: in the form, it is string, in the database, it is date. You need to convert to date before saving, or change to bank (undeclared).

2 answers

0

You can use it as the comment you made, or in this way I will show you, because it may happen that the contents of the string are not a valid date, so avoiding errors is a good practice:

string str = "2021-05-10";
DateTime result;
if(DateTime.TryParse(str, out result)) //retorna true se conseguir converter para DateTime                 
{                     
//usar o conteudo da variável result                 
}

0

You need to convert the text of your maskedTextBox into Datetime, because the column VALIDADECARTERINHA from your database is type Datetime and Voce is trying to pass a string to this field.

Try to change this line:

comando.Parameters.Add("@VALIDADECARTEIRINHA", SqlDbType.DateTime).Value =maskedTextBox_ValidadeCarteirinha.Text;

For this:

comando.Parameters.Add("@VALIDADECARTEIRINHA", SqlDbType.DateTime).Value = Convert.ToDateTime(maskedTextBox_ValidadeCarteirinha.Text);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.