Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Quote:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated


The Code behind Compose message is

C#
protected void btnSubmit_Click(object sender, EventArgs e)
   {
       try
       {
           string date, messagefrom, messageto, message;
           int employeeid;
           date = DateTime.Now.ToString();
           messagefrom = from;
           messageto = dropTo.SelectedItem.Text;
           message = txtMessage.Text;
           DataTable dt = BusinessAccessLayer.Query("select employeeid from employees where name='" + messageto + "'");
           if (dt.Rows.Count > 0)
           {
               employeeid = Convert.ToInt32(dt.Rows[0]["employeeid"]);
               string sqlstr = "insert into Messages values('" + date + "','" + messagefrom + "','" + messageto + "','" + message + "'," + employeeid + ",1)";
               BusinessAccessLayer.NonQuery(sqlstr);
           }
           Response.Redirect("managermessageoutbox.aspx", false);
       }
       catch (Exception ex)
       {
           lblError.Text = "Error: " + ex.Message;
       }
   }
Posted
Comments
Sergey Alexandrovich Kryukov 12-Jan-16 15:12pm    
First of all, don't store time data as varchar. You faced the case when the string is not a valid date/time, or in unexpected format...
—SA
ZurdoDev 12-Jan-16 15:50pm    
Debug the code and see what is happening. The error says you are trying to put something into a datetime field that is out of the valid range.
deepankarbhatnagar 12-Jan-16 23:32pm    
It may be possible when your sequence to insertion in table Messages in not correct order. Please specify the Messages table column names sequentially.
Member 12245428 13-Jan-16 7:48am    
How i can upload the table pic here ??
Member 12245428 13-Jan-16 17:19pm    

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Your problem i_s data dependent, but didn't provide the data.
The debugger will allow you to see what is the value you try to convert to DateTime. This is the first step to solution.
Once you tell us this value, we will be able to figure out what is wrong.
 
Share this answer
 
Comments
Member 12245428 12-Jan-16 23:11pm    
you said this <quote>Your problem i_s data dependent, but didn't provide the data.
you means the database table file for this code ??
Patrice T 12-Jan-16 23:31pm    
You can consider that dates already in the sql database are ok.
My guess is that the date variable contain a date in a form different than what sql is expecting.
It is very common to have date format depending on country.
Member 12245428 13-Jan-16 7:49am    
Can u show me in some example ??
Member 12245428 13-Jan-16 17:13pm    
Sir i really can't figure out how to get rid of this error
Member 12245428 13-Jan-16 17:21pm    
The problem is here
C#
date = DateTime.Now.ToString();


for debugging purpose change this line to this

date = '01-Jan-2016';

and re-execute the code, you will see that it gets successfully executed and a row has been added in the message table.

Now its your task to understand what is the difference between the previous and new changes :)
 
Share this answer
 
Comments
Member 12245428 13-Jan-16 7:49am    
I tried that but it did not work for me
Member 12245428 13-Jan-16 17:21pm    

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