CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Posts
    46

    date only to be selected

    Code:
    sqlCommand = new SqlCommand("SELECT date FROM Transactions", sqlConnection);
                        sqlDataReader = sqlCommand.ExecuteReader();
                        while (sqlDataReader.Read())
                        {
                            comboBoxResult.Items.Add(sqlDataReader["date"].ToString());
                        }
    Please kind coders, this line of code is adding both date and time to the comboBoxResult which i do not want, i only added
    date to the database but when retriving it it brings both date and time. I want only the date to be added. Thanks.

  2. #2
    Join Date
    Jul 2005
    Location
    Louisville, KY
    Posts
    201

    Resolved Re: date only to be selected

    I'd suggest trying something similar to the following:

    Code:
    DateTime dtFromSQL;
    sqlCommand = new SqlCommand("SELECT date FROM Transactions", sqlConnection);
                        sqlDataReader = sqlCommand.ExecuteReader();
                        while (sqlDataReader.Read())
                        {
                            dtFromSQL = DateTime.Parse(sqlDataReader["date"].ToString());
                            comboBoxResult.Items.Add(dtFromSQL.ToShortDateString());
                        }
    Depending on how you want to display your date, once you have it in the DateTime variable, you can format it however you want, short date being the "mm/dd/yyyy" if using the Engish (US) culture.

    If this resolves your question, please mark as resolved and rate up. Thanks!
    -Quinn

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: date only to be selected

    Wouldn't you need to use "SELECT [date] FROM Transactions" since date is a reserved word?
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured