I'm trying to access date from monthcalender for executing a SQL command in Windows Forms Application.

Below is the C# code

C# Code

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
     MyString = monthCalendar1.SelectionRange.Start.ToString();
     SqlCommand jk = new SqlCommand("select * from [Fee] where Date = '" + MyString + "' ", ob);
     SqlDataAdapter km = new SqlDataAdapter(jk);
     DataTable kl = new DataTable();
     dataGridView1.DataSource = kl;


CREATE TABLE [dbo].[Fee](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Admission] [varchar](50) NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [Type] [varchar](50) NOT NULL,
    [Amount] [int] NOT NULL,
    [Total] [int] NOT NULL,
    [Date] [date] NOT NULL,
    [Status] [varchar](50) NOT NULL,

It works perfectly for the dates between 1st and 12th days of every month. But when I select days other than that will raise an exception.


System.Data.SqlClient.SqlException: 'Conversion failed when converting date and/or time from character string.'

I have also checked the following Questions

  1. How to get the selected date of a MonthCalendar control in C#
  2. How to change date format from DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD?

But it didn't give me any solution. It may be because of my lack of knowledge. Can anyone help me to solve this issue.

0 Answers