Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:42:26+00:00 2026-06-02T16:42:26+00:00

i have database from which i get data to my C# app using SQL

  • 0

i have database from which i get data to my C# app using SQL query.

I have option in my app which get data for current day selected from MonthCalendar:

"between '" + monthCalendar1.SelectionStart.Month.ToString() + "' and '" + monthCalendar1.SelectionStart.AddDays(1).ToString()+

so when i select 2012-03-06 from my calendar form i get something like this for my sql query:

between '2012-03-06' and '2012-03-12'

And it’s working.

Now i’d like to get data for entire month – how to modify part of sql query so when i select day in April i get this:

between '2012-04-01' and '2012-04-30'

?

EDIT:
I make some progress – I have first day of month, but how to get last?

monthCalendar1.SelectionStart.Year.ToString()+"-"+monthCalendar1.SelectionStart.Month.ToString().PadLeft(2,'0')+"-"+"01"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:42:27+00:00Added an answer on June 2, 2026 at 4:42 pm

    Never put your SQL statements together as strings! Remember Bobby tables…

    Apart from that:

    DateTime someDay = ...;
    DateTime firstOfMonth = new DateTime(someDay.Year, someDay.Month, 1);
    DateTime lastOfMonth = firstOfMonth.AddMonths(1).AddDays(-1);
    
    string sqlString = "SELECT ... FROM ... WHERE ... AND ... BETWEEN @d1 AND @d2";
    using (SqlCommand cmd = new SqlCommand(sqlString, connection))
    {
        cmd.Parameters.AddWithValue("@d1", firstOfMonth);
        cmd.Parameters.AddWithValue("@d2", lastOfMonth);
        ...
    }
    

    EDIT
    Come to think of it: You’ll actually notice that what you want will not return any entries that are between lastOfMonth 00:00:01 and lastOfMonth 23:59:59. Actually, you should query from the first of the selected month to the first of the following month to get the desired information (subtracting one Millisecond instead of one Day won’t help, as SQL Server can not store milliseconds extactly).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with like 20 fields which get populated from SQL database
I have a variable which I get from database I want to output this
I have a DataTable which I select from database (Well, these data cross several
I have a web-app-database 3 tier server setup. Web requests data from app, and
i have a mysql database from which i require an rss feed of its
I have a database connection which takes an input from the querystring to access
I have a function which calculates distance from database records. This function is called
i have a combobox which is displaying date from database...in my database the date
Here i have gridview which contain some values retrieved from database.We put radiobutton list
I have a list of e-mails from one database which I want to check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.