Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
I' m getting an error at runtime when i select a date in monthcalender control. It throws an error saying
Quote:
Conversion failed when converting datetime from character string.
. I have assigned Date as
Quote:
nvarchar(255)
in SQL Server 2005.

Below is the code of Win Form in C# ADO.NET:

C#
public partial class Search : Form
    {
        SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=Externetworks Interviews");
        DataSet ds = new DataSet();
        public Search()
        {
            InitializeComponent();
            
        }

        private void Search_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'externetworks_InterviewsDataSet1.Interviews' table. You can move, or remove it, as needed.
            this.interviewsTableAdapter1.Fill(this.externetworks_InterviewsDataSet1.Interviews);
            // TODO: This line of code loads data into the 'externetworks_InterviewsDataSet.Interviews' table. You can move, or remove it, as needed.
            //this.interviewsTableAdapter.Fill(this.externetworks_InterviewsDataSet.Interviews);
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Please Enter Order no", "ERROR");
                textBox1.Focus();
            }
            else
            {
                SqlCommand cmd = new SqlCommand("Select * from Interviews where orderno= " + int.Parse(textBox1.Text), con);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds,"Interviews" );
                dataGridView1.Show();
                dataGridView1.DataSource = ds.Tables["Interviews"];
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("Select * from Interview", con);
            dataGridView1.Show();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Interviews obj = new Interviews();
            obj.Show();
            this.Hide();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            
                DataTable dt = new DataTable();
                DateTime mdate = monthCalendar1.SelectionRange.Start.Date;
                DateTime start = monthCalendar1.SelectionStart;
                DateTime end = monthCalendar1.SelectionEnd;
                SqlCommand cmd = new SqlCommand("select * from Interviews where intvwdate=@intvwdate", con);

                cmd.Parameters.AddWithValue("@intvwdate", Convert.ToDateTime(monthCalendar1.SelectionStart));
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds, "Interviews");
                dataGridView1.Show();
                dataGridView1.DataSource = ds.Tables["Interviews"];
            

        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Login obj = new Login();
            obj.Show();
            this.Hide();
        }
        
    }
}
Posted
Comments
[no name] 20-Sep-13 14:10pm    
So you are taking a DateTime, converting it to a DateTime and then trying to save it as a varchar? Why?
Richard C Bishop 20-Sep-13 14:13pm    
Where does it fail?
Member 10165997 20-Sep-13 14:18pm    
it fails in this part of the code.


private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{

DataTable dt = new DataTable();
DateTime mdate = monthCalendar1.SelectionRange.Start.Date;
DateTime start = monthCalendar1.SelectionStart;
DateTime end = monthCalendar1.SelectionEnd;
SqlCommand cmd = new SqlCommand("select * from Interviews where intvwdate=@intvwdate", con);

cmd.Parameters.AddWithValue("@intvwdate", Convert.ToDateTime(monthCalendar1.SelectionStart));
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "Interviews");
dataGridView1.Show();
dataGridView1.DataSource = ds.Tables["Interviews"];


}
Richard C Bishop 20-Sep-13 14:20pm    
Generally when someone asks where your code fails, they mean which line. My guess is the Convert.ToDate(monthCalendar1.SelectionStart) is where it is.
Member 10165997 20-Sep-13 14:33pm    
Yes, it is the same one which you have guessed.

1 solution

It seems like you have some kind of data format missmatch. If you declare your column of type nvarchar(255), which is somehow equivalent to the string type, the value used in the SELECT's WHERE clause must also be of that type. It could probably be better redefining the column type as DateTime to be able to use this construct:
cmd.Parameters.AddWithValue("@intvwdate", Convert.ToDateTime(monthCalendar1.SelectionStart));
 
Share this answer
 
Comments
Member 10165997 20-Sep-13 15:50pm    
I tried to redefine the column type to DateTime but it pops up with a Warning Message: Warning: Data might be lost converting column 'IntvwDate' from 'nvarchar(255)'.

When i click on OK button, it shows:'Interviews' table
- Unable to modify table.
Conversion failed when converting datetime from character string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900