Insert time and time into database

Asked

Viewed 482 times

0

I want to enter the time and date separately in the database, which have the fields timestamp and data, respectively.

I have the following code on the insert button:

SqlCommand sqlInsertCabecalho = 
new SqlCommand("Insert into cabecalho (nRequesicao,nomeEmpresa,colaborador,data,hora,nota) VALUES(@nRequesicao,@nomeEmpresa,@colaborador,@data,@hora,2nota)", sqlConn);

sqlInsertCabecalho.Parameters.AddWithValue("@nRequesicao", nRequesicao.ToString());
sqlInsertCabecalho.Parameters.AddWithValue("@nomeEmpresa", DropDownListEmpresa.Text);
sqlInsertCabecalho.Parameters.AddWithValue("@colaborador", Session["New"].ToString());
sqlInsertCabecalho.Parameters.AddWithValue("@data", DateTime.Now.ToString("yyyy-MM-dd"));
sqlInsertCabecalho.Parameters.AddWithValue("@hora", DateTime.Now.ToString("HH:mm:ss"));
sqlInsertCabecalho.Parameters.AddWithValue("@nota", TextBoxObservacoes.Text);

sqlConn.Open();
sqlTran = sqlConn.BeginTransaction();
sqlInsertCabecalho.Transaction = sqlTran;
sqlInsertCabecalho.ExecuteNonQuery();
sqlTran.Commit();
sqlConn.Close();

Response.Redirect("Consulta.aspx");

But however I get an exception and does not insert. What I am doing wrong?

  • Tell me the exception. What is the value you’re trying to use? I have an idea, but I don’t want to guess.

  • @bigown I am trying to use the automatic date/time value, the exection says that there is an incorrect syntax.

  • @I don’t know if I don’t understand the question or explained it wrong, but I want to enter the current time and date value of when the insertion is made. Hence the 'Datetime.Now'

  • Yeah, okay, so that’s not the problem. I need more detail, the line where the error occurs, the exact exception. I think the mistake isn’t even in those lines, they’re correct. Unless you don’t have the @data or @hora inside the command you’re mounting.

  • Can you [Edit] your question and add the exact exception? And also the content of sqlInsertCabecalho.

  • @jbueno made the changes

  • @moustache I edited and added the detail

Show 2 more comments

2 answers

2

It’s a typo, there’s a 2 in place of @ (2nota)

VALUES(@nRequesicao,@nomeEmpresa,@colaborador,@data,@hora,@nota)

2


There is a typo, change to:

VALUES (@nRequesicao, @nomeEmpresa, @colaborador, @data, @hora, @nota)
  • @jbueno is undoubtedly an error of a stupid distraction of mine, but now I came across this exception: 'Additional information: Cannot Insert an Explicit value into a timestamp column. Use INSERT with a column list to exclude the timestamp column, or Insert a DEFAULT into the timestamp column.'

  • What database are you using? If you want to write to a column Timestamp, need to send to this format, can not send anything.

  • @Kawaii is already subject to another question. There is no problem you open more than one question in a short interval. If this problem has already been solved, you mark one of the answers as correct in the below the number of votes. Anyway, this error probably has to do with database column data type. What kind of columns data and hora?

  • Understood, and thank you very much!

  • @Kawaii, if you’re going to ask a new question, be sure to inform the data bank and column type. I voted to close the question, because it’s just a typo, there’s no problem with it, but it’s not going to help anyone else.

Browser other questions tagged

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