Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Problem in DateTime conversion!! Please help

Problem in DateTime conversion!! Please help

Scheduled Pinned Locked Moved ASP.NET
databasehelptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    skhan17
    wrote on last edited by
    #1

    Hello, I am having problem in running query for the following code: Actually My database in in MS Access and I need to compare the date column (which is in Date format in the database) with the value of the Date textbox (which is a string). So maybe I need to convert the string to date format and then compare......should I convert to date format? My date is stored in the database as dd-mmm-yy. For example it's stored as 31-Aug-09. Please assist:

    public void executePositiveListQuery_ANY(string selected_Date)
    {
    string sql;
    if(selected_Date=="empty")
    sql= "SELECT DISTINCT re_MLNO FROM ml_hiv_status WHERE (re_HIV1_Status=1 AND re_HIV2_Status=1)";
    else
    {
    string selectedDate = (Convert.ToDateTime(selected_Date)).ToShortDateStr ing();// I am having problem here
    //DateTime DD= Convert.ToDateTime(selected_Date);
    sql= "SELECT re_MLNO FROM ml_hiv_status WHERE ( re_SpecimenDate='" + selectedDate + "' AND re_HIV1_Status=1 AND re_HIV2_Status=1)";
    resultsLabel.Text ="You Have selected"+ selectedDate;
    }

    try
    {
    OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECT ION_STRING);
    OleDbDataAdapter adapter = new OleDbDataAdapter();
    OleDbCommand command = new OleDbCommand(sql, connection);

    DataSet ds = new DataSet();
    adapter.SelectCommand = command;

    int count = adapter.Fill(ds);

    if (count > 0 && count <= 200)
    {
    DataView view = ds.Tables[0].DefaultView;
    //view.Sort=e.SortExpression;
    resultsDatagrid.DataSource = view;
    resultsDatagrid.DataBind();
    resultsDatagrid.Visible = true;
    resultsLabel.Text = count.ToString() + " results found.";
    resultsLabel.Visible = true;
    exportLinkbutton.Visible = true;
    }
    else if (count> 200)
    {
    DataView view = ds.Tables[0].DefaultView;
    //view.Sort=e.SortExpression;
    resultsDatagrid.DataSource = view;
    resultsDatagrid.DataBind();
    resultsDatagrid.Visible = false;
    resultsLabel.Text = "More than 200 results found. Please Click the Export to Excel Link to Download the Results.";
    resultsLabel.Visible = true;
    exportLinkbutton.Visible = true;
    }
    else
    {
    resultsLabel.Text = "No results found.";
    resultsLabel.Visible = true;
    exportLinkbutton.Visible = false;
    resultsDatagrid.Visible = false;
    }
    connection.Close();
    }
    catch(Exception ex)
    {
    message.InnerHtml = "SQL: " + sql + "<p></p>" + ex.ToString(); //Now it throws exception here!!
    }

    D R 2 Replies Last reply
    0
    • S skhan17

      Hello, I am having problem in running query for the following code: Actually My database in in MS Access and I need to compare the date column (which is in Date format in the database) with the value of the Date textbox (which is a string). So maybe I need to convert the string to date format and then compare......should I convert to date format? My date is stored in the database as dd-mmm-yy. For example it's stored as 31-Aug-09. Please assist:

      public void executePositiveListQuery_ANY(string selected_Date)
      {
      string sql;
      if(selected_Date=="empty")
      sql= "SELECT DISTINCT re_MLNO FROM ml_hiv_status WHERE (re_HIV1_Status=1 AND re_HIV2_Status=1)";
      else
      {
      string selectedDate = (Convert.ToDateTime(selected_Date)).ToShortDateStr ing();// I am having problem here
      //DateTime DD= Convert.ToDateTime(selected_Date);
      sql= "SELECT re_MLNO FROM ml_hiv_status WHERE ( re_SpecimenDate='" + selectedDate + "' AND re_HIV1_Status=1 AND re_HIV2_Status=1)";
      resultsLabel.Text ="You Have selected"+ selectedDate;
      }

      try
      {
      OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECT ION_STRING);
      OleDbDataAdapter adapter = new OleDbDataAdapter();
      OleDbCommand command = new OleDbCommand(sql, connection);

      DataSet ds = new DataSet();
      adapter.SelectCommand = command;

      int count = adapter.Fill(ds);

      if (count > 0 && count <= 200)
      {
      DataView view = ds.Tables[0].DefaultView;
      //view.Sort=e.SortExpression;
      resultsDatagrid.DataSource = view;
      resultsDatagrid.DataBind();
      resultsDatagrid.Visible = true;
      resultsLabel.Text = count.ToString() + " results found.";
      resultsLabel.Visible = true;
      exportLinkbutton.Visible = true;
      }
      else if (count> 200)
      {
      DataView view = ds.Tables[0].DefaultView;
      //view.Sort=e.SortExpression;
      resultsDatagrid.DataSource = view;
      resultsDatagrid.DataBind();
      resultsDatagrid.Visible = false;
      resultsLabel.Text = "More than 200 results found. Please Click the Export to Excel Link to Download the Results.";
      resultsLabel.Visible = true;
      exportLinkbutton.Visible = true;
      }
      else
      {
      resultsLabel.Text = "No results found.";
      resultsLabel.Visible = true;
      exportLinkbutton.Visible = false;
      resultsDatagrid.Visible = false;
      }
      connection.Close();
      }
      catch(Exception ex)
      {
      message.InnerHtml = "SQL: " + sql + "<p></p>" + ex.ToString(); //Now it throws exception here!!
      }

      D Offline
      D Offline
      DoctorMick
      wrote on last edited by
      #2

      The best thing to do would be to parameterise the query so the conversion work is done for you. Failing that, converting the date format in your query string to either yyyy-mm-dd or dd mmm yyyy should do the trick.

      S 1 Reply Last reply
      0
      • D DoctorMick

        The best thing to do would be to parameterise the query so the conversion work is done for you. Failing that, converting the date format in your query string to either yyyy-mm-dd or dd mmm yyyy should do the trick.

        S Offline
        S Offline
        skhan17
        wrote on last edited by
        #3

        How would I do that? Can you please help?

        J 1 Reply Last reply
        0
        • S skhan17

          How would I do that? Can you please help?

          J Offline
          J Offline
          Jeremy Likness
          wrote on last edited by
          #4

          Use SQL Parameters to Overcome Ad Hoc Performance Issues[^]

          Jeremy Likness Latest Article: Whats in Your Collection? Part 1 of 3: Interfaces Blog: C#er : IMage

          1 Reply Last reply
          0
          • S skhan17

            Hello, I am having problem in running query for the following code: Actually My database in in MS Access and I need to compare the date column (which is in Date format in the database) with the value of the Date textbox (which is a string). So maybe I need to convert the string to date format and then compare......should I convert to date format? My date is stored in the database as dd-mmm-yy. For example it's stored as 31-Aug-09. Please assist:

            public void executePositiveListQuery_ANY(string selected_Date)
            {
            string sql;
            if(selected_Date=="empty")
            sql= "SELECT DISTINCT re_MLNO FROM ml_hiv_status WHERE (re_HIV1_Status=1 AND re_HIV2_Status=1)";
            else
            {
            string selectedDate = (Convert.ToDateTime(selected_Date)).ToShortDateStr ing();// I am having problem here
            //DateTime DD= Convert.ToDateTime(selected_Date);
            sql= "SELECT re_MLNO FROM ml_hiv_status WHERE ( re_SpecimenDate='" + selectedDate + "' AND re_HIV1_Status=1 AND re_HIV2_Status=1)";
            resultsLabel.Text ="You Have selected"+ selectedDate;
            }

            try
            {
            OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECT ION_STRING);
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            OleDbCommand command = new OleDbCommand(sql, connection);

            DataSet ds = new DataSet();
            adapter.SelectCommand = command;

            int count = adapter.Fill(ds);

            if (count > 0 && count <= 200)
            {
            DataView view = ds.Tables[0].DefaultView;
            //view.Sort=e.SortExpression;
            resultsDatagrid.DataSource = view;
            resultsDatagrid.DataBind();
            resultsDatagrid.Visible = true;
            resultsLabel.Text = count.ToString() + " results found.";
            resultsLabel.Visible = true;
            exportLinkbutton.Visible = true;
            }
            else if (count> 200)
            {
            DataView view = ds.Tables[0].DefaultView;
            //view.Sort=e.SortExpression;
            resultsDatagrid.DataSource = view;
            resultsDatagrid.DataBind();
            resultsDatagrid.Visible = false;
            resultsLabel.Text = "More than 200 results found. Please Click the Export to Excel Link to Download the Results.";
            resultsLabel.Visible = true;
            exportLinkbutton.Visible = true;
            }
            else
            {
            resultsLabel.Text = "No results found.";
            resultsLabel.Visible = true;
            exportLinkbutton.Visible = false;
            resultsDatagrid.Visible = false;
            }
            connection.Close();
            }
            catch(Exception ex)
            {
            message.InnerHtml = "SQL: " + sql + "<p></p>" + ex.ToString(); //Now it throws exception here!!
            }

            R Offline
            R Offline
            Robert_Pan
            wrote on last edited by
            #5

            OledbParamenter @yourtime

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups