Hello everyone. I'm beginner and i trying to make time register system. But i can't insert my datetime to my database.Here's the code that i wrote

private void button1_Click(object sender, EventArgs e)
        {
            string dt;   
            string dt2;
            DateTime date = DateTime.Now;    
            DateTime date2 = DateTime.Now;    
            dt = date.ToLongTimeString();        // display format:  11:45:44 AM
            dt2 = date2.ToShortDateString();         // display format:  5/22/2010

            SqlConnection conn = new SqlConnection("data source=dulguun-pc\\sqlexpress; database=tsag_burtgel; Integrated security=true");
            conn.Open();

            SqlCommand cmd = new SqlCommand();

            if (comboBox1.SelectedItem ==null)
            {
                MessageBox.Show("Та дансны кодоо оруулна уу");
            }
            else
            {
                int code = Convert.ToInt32(comboBox1.SelectedItem);
                cmd.Parameters.Add("@dugaar", SqlDbType.Int).Value = code;  
            }            

            if (radioButton1.Checked == true)
            {
                cmd.Parameters.Add("@time_in", SqlDbType.NVarChar,50).Value = dt;
                cmd.Parameters.Add("@date_in", SqlDbType.NVarChar, 50).Value = dt2;
            }
            else
            {
                cmd.Parameters.Add("@time_in", SqlDbType.NVarChar, 50).Value = dt;
                cmd.Parameters.Add("@date_in", SqlDbType.NVarChar, 50).Value = dt2;
            }
            if (radioButton2.Checked == true)
            {
                cmd.Parameters.Add("@time_out", SqlDbType.NVarChar, 50).Value = dt;
                cmd.Parameters.Add("@date_out", SqlDbType.NVarChar, 50).Value = dt2;
            }
            else
            {
                cmd.Parameters.Add("@time_out", SqlDbType.NVarChar,50).Value = dt;
                cmd.Parameters.Add("@date_out", SqlDbType.NVarChar, 50).Value = dt2;
            }

            cmd.CommandText = "Insert into ajilchin values(@dugaar,@date_in,@date_out,@time_in,@time_out)";
            cmd.Connection = conn;
            cmd.ExecuteNonQuery();

            load();

            conn.Close();
            comboBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";

please tell me what should i do and what i did wrong

Edited 6 Years Ago by __avd: Added [code] tags.

try this

DateTime.Now.ToString("yyyyMMdd hh:mm:ss:fff");

Edited 6 Years Ago by finito: n/a

This article has been dead for over six months. Start a new discussion instead.