2

how to Insert Datetimepicker text in SQL Database as datetime datatype

1
  • What is the problem - getting DateTime form DateTimePicker or inserting DateTime into the database? Commented Jun 30, 2010 at 7:52

3 Answers 3

2

In order to parse the datetime from datetime picker you can do the following:

string dt = dateTimePicker.Value.ToString("yyyy-MM-dd hh:mm:ss");

And then in order to insert it in your database you can do:

Insert into table (id, mytime) 
values("5",  CONVERT(datetime, CONVERT( varchar(11), dt, 101))

If you need information on how to access your database you can read the following article in CodeProject

Sign up to request clarification or add additional context in comments.

Comments

0

Parse the text in C#, and insert the parsed DateTime. Example:

var date = new DateTime(dateTimePickerText);
InsertIntoSqlDatabase(date); // Code for inserting here.

Comments

0

You can insert date as DateTime directly (without converting into string) by using DataRow. For example:

  using (SqlConnection sqlconn = new SqlConnection())
        {
            sqlconn.ConnectionString = @"Data Source = #your_serwer\instance#; Initial Catalog = #db_name#; User Id = #user#; Password = #pwd#;";
            sqlconn.Open();

            // select your date and a key index field on a table (e.g. "id")
            string sql = "select id, some_date from table_1 where id = 1 ";

            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlconn))
            {
                using (DataTable table = new DataTable())
                {
                    adapter.Fill(table);                       

                    DataRow r = null;
                    if (table.Rows.Count == 1)
                        r = table.Rows[0];   // case, when there is an row for update
                    else
                        r = table.NewRow();  // case, when there is no row, and you would like to insert one

                    r.BeginEdit();
                    // here you assign your value
                    r["some_date"] = dateTimePicker.Value;
                    r.EndEdit();

                    if (table.Rows.Count == 0)
                    {
                        // in case of insert, provide here some other values for other non null columns like
                        r["id"] = 2;
                        // also you have to add this row into DataTable
                        table.Rows.Add(r);
                    }

                    // this will update the database
                    SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(adapter);
                    adapter.Update(table);
                }
            }
        }

Hope, this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.