Click here to Skip to main content
15,895,772 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am reteriving the below query from database.
C#
select PFloanApplications.*,convert(varchar(20),PFloanApplications.applicationdate,111)as NewLoanApplicationDate,HREmployeeInfo.EmployeeId,HREmployeeInfo.Title,HREmployeeInfo.FirstName,HREmployeeInfo.MiddleName,HREmployeeInfo.Lastname,HREmployeeInfo.EPFNumber, HREmployeeInfo.FirstName+' '+HREmployeeInfo.Middlename+' '+HREmployeeInfo.lastname As EmployeeName,PFLoanPurpose.LoanPurposeName from PFLoanApplications left outer join HREmployeeInfo on PFLoanApplications.EmployeeId=HREmployeeInfo.EmployeeId  left outer join PFLoanPurpose on PFLoanApplications.LoanPurposeId=PFLoanPurpose.LoanPurposeId where (PFLoanApplications.ApplicationDate between 'date.text' and '" + dojto.Text + "')and(PFLoanApplications.IsDeleted IS NULL)and(PFLoanApplications.ApprovedStatus is null)


The problem is that when it finds the between the selected rows it shows the data and whenever it doesn't find the row it shows the following error.
SQL
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

please tell how to correct this error.
Posted
v4

C#
 System.Globalization.CultureInfo cul = new System.Globalization.CultureInfo("en-GB");
DateTime current_date;
                
DateTime current_date1;
current_date=DateTime.Parse(date.Text.Trim(),cul,System.Globalization.DateTimeStyles.NoCurrentDateDefault);

current_date1=DateTime.Parse(date.Text.Trim(),cul,System.Globalization.DateTimeStyles.NoCurrentDateDefault);


in you query
SQL
where colum_name between '"+current_date.ToShortDateString()+"' and '"+current_date1.ToShortDateString()+"'
 
Share this answer
 
Comments
ajays3356 30-Jan-14 2:16am    
i am trying your code but now when it finds the rows then it shows only one row out of three.
King Fisher 30-Jan-14 5:40am    
post what do you tried. im using the same code only
Quote:
PFLoanApplications.ApplicationDate between 'date.text' and '" + dojto.Text + "'
Don't do like this, instead go for parameterized query something like below...
C#
Select PFloanApplications.*,convert(varchar(20),PFloanApplications.applicationdate,111)as NewLoanApplicationDate,HREmployeeInfo.EmployeeId,HREmployeeInfo.Title,HREmployeeInfo.FirstName,HREmployeeInfo.MiddleName,HREmployeeInfo.Lastname,HREmployeeInfo.EPFNumber, HREmployeeInfo.FirstName+' '+HREmployeeInfo.Middlename+' '+HREmployeeInfo.lastname As EmployeeName,PFLoanPurpose.LoanPurposeName from PFLoanApplications left outer join HREmployeeInfo on PFLoanApplications.EmployeeId=HREmployeeInfo.EmployeeId  left outer join PFLoanPurpose on PFLoanApplications.LoanPurposeId=PFLoanPurpose.LoanPurposeId where (PFLoanApplications.ApplicationDate BETWEEN @StartDate AND @EndDate)and(PFLoanApplications.IsDeleted IS NULL)and(PFLoanApplications.ApprovedStatus is null)

C#
DateTime startDate = DateTime.Parse(date.Text);
DateTime endDate = DateTime.Parse(dojto.Text);

yourCommand.Parameters.AddWithValue("@StartDate", startDate);
yourCommand.Parameters.AddWithValue("@EndDate", endDate);
 
Share this answer
 
v2
Comments
ajays3356 30-Jan-14 2:38am    
No improvement same error is coming.
What is the DataType of ApplicationDate in Database Table?
ajays3356 30-Jan-14 4:09am    
datetime
Can you try with the following code like...

SqlParameter paramStartDate = yourCommand.Parameters.Add("@StartDate", SqlDbType.DateTime);
paramStartDate.Value = startDate;

SqlParameter paramEndDate = yourCommand.Parameters.Add("@EndDate", SqlDbType.DateTime);
paramEndDate.Value = endDate;

And if again same Exception comes, let me know on which line exactly the Exception comes.

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