You must Sign In to post a response.
  • Category: ASP.NET

    Datetime in to sql format

    hi friends,

    i'am writting datetime method is
    public string OnCreated()
    DateTime Date1 = DateTime.Now;
    string sqlFormattedDate = Date1.ToString("yyyy-MM-dd HH:mm:ss.fff");
    return sqlFormattedDate;
    sql format is:2015-12-04 17:09:52.963
    but it show the error

    The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

    i'am searching this error in google but i'am not get it.

    i want like sql format.example 2015-12-04 17:09:52.963

    Thank you
  • #763433
    Hello Sadiq,

    Try this code :

    string dateValue = System.Date.Now.ToString();
    string formatForMySql = Convert.ToDateTime(dateValue).ToString("yyyy-MM-dd HH:mm:ss");

    Hope this will be helpful for you.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #763437
    Hi Sadiq.

    The code works perfectly. I called it in page load and it gave the attached out put.

    public void Page_Load(object sender, EventArgs e)

    public void OnCreated()
    DateTime myDate= DateTime.Now;
    string sqlFormattedDate = myDate.ToString("yyyy-MM-dd HH:mm:ss.fff");
    //return sqlFormattedDate;
    Response.Write("" + sqlFormattedDate);

    Sridhar Thota.
    Editor: DNS Forum.

    Delete Attachment

  • #763441
    hi Sridhar sir,
    Actually i 'am calling that method in stringcollection class can you please check the below code.
    protected void Save_Click(object sender, EventArgs e)
    int rowIndex = 0;
    StringCollection sc = new StringCollection();
    if (ViewState["CurrentTable"] != null)
    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
    if (dtCurrentTable.Rows.Count > 0)
    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)

    TextBox box1 = (TextBox)Repeater1.Items[rowIndex].FindControl("TxtArea");
    TextBox box2 = (TextBox)Repeater1.Items[rowIndex].FindControl("TextBox1");
    TextBox box3 = (TextBox)Repeater1.Items[rowIndex].FindControl("TextBox2");
    TextBox box4 = (TextBox)Repeater1.Items[rowIndex].FindControl("TextBox3");
    TextBox box5 = (TextBox)Repeater1.Items[rowIndex].FindControl("TextBox4");
    Label l2=(Label)Repeater1.Items[rowIndex].FindControl("Label2");
    Label l3 = (Label)Repeater1.Items[rowIndex].FindControl("Label3");
    Label l4 = (Label)Repeater1.Items[rowIndex].FindControl("Label4");
    Label l5 = (Label)Repeater1.Items[rowIndex].FindControl("Label5");

    sc.Add(DropDownList1.SelectedValue.ToString() + "," + int.Parse(DropDownList2.SelectedValue.ToString()) + "," + box1.Text + "," + box2.Text + "," + box3.Text + "," + box4.Text + "," + box5.Text + "," + l2.Text + "," + l3.Text + "," + l4.Text + "," + l5.Text+","+OnCreated() );


    Thank you

  • #763485
    use following snippet to convert datetime to string

    string text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffffff", CultureInfo.InvariantCulture)

    here, if you really don't know about the program current culture you can use 'InvariantCulture'

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763493

    it seems the problem with conversion from one datatype to another datatype, I suggested you to please check the datatypes clearly and then let us know.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #765565
    Thanks for the discussion. Sorry to say that I could not help you, but after reading this discussion, I know that conversion of data type resulted an out-of-range value. I hosted my website on,which provides hosting services. Continue discussion so that those who have no idea can get something.

  • #765575

    Try this,

    public string OnCreated()
    string sqlFormattedDate = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss.fff");
    return sqlFormattedDate;

  • #766391
    you can try this Query

    select CONVERT(varchar(20),date_time,101) from sample2


    select CONVERT(varchar(20),date_time,102) from sample2

    select CONVERT(varchar(20),date_time,103) from sample2

    select CONVERT(varchar(20),date_time,111) from sample2

    select CONVERT(varchar(20),date_time,110) from sample2


    select * from sample2 where CONVERT(varchar(20),date_time,101) between CONVERT(varchar(20),date_time,101) and CONVERT(varchar(20),date_time,101)

    select * from sample2 where CONVERT(varchar(20),date_time,102) between CONVERT(varchar(20),date_time,102) and CONVERT(varchar(20),date_time,102)

    Name : Dotnet Developer-2015
    Email Id

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

Sign In to post your comments