none
how to insert date in mysql through c#.net?

    Question

  • Hi all,

    i am using mysql as database

    i want to insert date into mysql through c#.net

    but my problem is when iam inserting recod it shows the error at date

    i dont know how to handle this

    my code is:

    cmd = new MySqlCommand("insert into customers(CustomerName,EntryDate,TotalAmount)values('" + txtCusName.Text.Trim()+"',"+DateTime.Now+","
                       + Convert.ToInt32(txtTotal.Text.Trim()) +")", con);

    cmd.ExecuteNonQuery();

    how can i store date into mysql through the query?

    Thanks,


    programmer on .net

    Monday, July 30, 2012 5:38 PM

Answers

  • Put one breakpoint on that line and tell me what value of DateTimePicker1.Value show, cuz it could have words(months for example) and it could cause error. If's not the case, and his value show correctly.
                string date = "30/07:2012 15:54:22";
                DateTime time;
                DateTime.TryParse(date, out time);
                string s = string.Format("select * from customers where EntryDate='{0}'", date);
    It works as expected.

    Web Developer

    Monday, July 30, 2012 5:55 PM
  • hi Norkk

    thanks for ur reply.

    when i want to search based on date iam writing the following query

    string s="select * from customers where EntryDate="+DateTimePicker1.value;

    but it is showing error at date

    You have to provide the exact format as its saved inside database. Value property of dateTimePicker contol does not necessary provide it.

    So to do so, you can use ToString() method to specify the exact format:

    //if your date format in db is like MM/dd/yyyy do:
    string strDate = dateTimePicker1.Value.ToString("MM/dd/yyyy"); //change format accordinlgy to your format in DB!

    now, use strDate variable inside sql query.


    Mitja

    Monday, July 30, 2012 6:06 PM

All replies

  • Try this
    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
    
    //Or
    
    DateTime.Now.ToString("yyyyMMddHHmmss");
    
    


    Web Developer

    Monday, July 30, 2012 5:39 PM
  • hi Norkk

    thanks for ur reply.

    when i want to search based on date iam writing the following query

    string s="select * from customers where EntryDate="+DateTimePicker1.value;

    but it is showing error at date

    how can i slove this issue.?


    programmer on .net

    Monday, July 30, 2012 5:51 PM
  • Put one breakpoint on that line and tell me what value of DateTimePicker1.Value show, cuz it could have words(months for example) and it could cause error. If's not the case, and his value show correctly.
                string date = "30/07:2012 15:54:22";
                DateTime time;
                DateTime.TryParse(date, out time);
                string s = string.Format("select * from customers where EntryDate='{0}'", date);
    It works as expected.

    Web Developer

    Monday, July 30, 2012 5:55 PM
  • hi Norkk

    thanks for ur reply.

    when i want to search based on date iam writing the following query

    string s="select * from customers where EntryDate="+DateTimePicker1.value;

    but it is showing error at date

    You have to provide the exact format as its saved inside database. Value property of dateTimePicker contol does not necessary provide it.

    So to do so, you can use ToString() method to specify the exact format:

    //if your date format in db is like MM/dd/yyyy do:
    string strDate = dateTimePicker1.Value.ToString("MM/dd/yyyy"); //change format accordinlgy to your format in DB!

    now, use strDate variable inside sql query.


    Mitja

    Monday, July 30, 2012 6:06 PM