Click here to Skip to main content
15,909,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a program that use ACCESS as database.

In my database i have a table ho have same row set to: Date/Time with no format in the database.

I want to save a date and time in my database from a datatimepicker.

When a retrieve from my datatimepicker the value, it save in my database only the date, but i need and time.

If i use

C#
DateTime saveDate = DateTime.Now;



This is the cod to save in my database.
C#
private void btnAdaugaContract_Click(object sender, EventArgs e)
        {
            VerificareDateIntroduse();
            if (DateIntroduse == true)
            {
                try
                {
                    Program.Connection.CommandText = "INSERT INTO Contracts (ContractId, ClientId, CreateDate, StartDate, EndDate, Procent) values (@ContractId, @ClientId, @CreateDate, @StartDate, @EndDate, @Procent)";
                    Program.Connection.AddParameter("@ContractId", txtNumarContract.Text);
                    Program.Connection.AddParameter("@ClientId", ClientActiv.ClientID);
                    Program.Connection.AddParameter("@CreateDate", Convert.ToDateTime(dInceput.Value));
                    Program.Connection.AddParameter("@StartDate", Convert.ToDateTime(dInceput.Value));
                    Program.Connection.AddParameter("@EndDate", Convert.ToDateTime(dFinalizare.Value));
                    Program.Connection.AddParameter("@Procent", Convert.ToDouble(txtComision.Text.Replace('.', ',')));
                    Program.Connection.ExecuteNonQuery();

                    for (int i = 0; i < dataProduseAmanet.Rows.Count; i++)
                    {
                        Program.Connection.CommandText = "INSERT INTO ContractItems (ContractId, Name, Description, Payment, Quantity, QuantityUnit) values (@ContractId, @Name, @Description, @Payment, @Quantity, @QuantityUnit)";
                        Program.Connection.AddParameter("@ContractId", txtNumarContract.Text);
                        Program.Connection.AddParameter("@Name", dataProduseAmanet.Rows[i].Cells[0].Value.ToString());
                        Program.Connection.AddParameter("@Description", dataProduseAmanet.Rows[i].Cells[3].Value.ToString());
                        Program.Connection.AddParameter("@Payment", dataProduseAmanet.Rows[i].Cells[1].Value.ToString());
                        Program.Connection.AddParameter("@Quantity", dataProduseAmanet.Rows[i].Cells[2].Value.ToString());
                        Program.Connection.AddParameter("@QuantityUnit", dataProduseAmanet.Rows[i].Cells[4].Value.ToString());
                        Program.Connection.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                this.Close();
            }
        }



if i replace the
C#
Program.Connection.AddParameter("@CreateDate", Convert.ToDateTime(dInceput.Value));
with

C#
Program.Connection.AddParameter("@CreateDate", saveDate);


the
C#
dInceput
is the datatimepicker with no formating.


The saveDate have the date and time, but it gives me the error: "data type mismatch".

Some suggestions.
Tanks.
Posted
Updated 22-Sep-12 2:43am
v4
Comments
[no name] 22-Sep-12 8:33am    
"DateTime saveDate = DateTime.New;"... no you are not using this at all anywhere in your program.
"but it gives me the error", what is "it" that give you the error?
Where is the real code that demonstrates this problem?

Try DateTime.Now instead of DateTime.New
 
Share this answer
 
Comments
Aciobanita Costel 22-Sep-12 8:47am    
was a mistake of typing, i use DateTime.Now.
C#
Convert.ToDateTime(saveData.ToString("f"))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900