Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
i have two textbox in wich i insert date value and one textbox for name.when i click on search button, record bettween this two date display.it is working.But when i not insert value in date textbox and insert only in name textbox and click on search button,then it gives me error.Error is:String was not recognized as a valid DateTime.
what i have to do to take textbox of date empty.
here is my code...
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            objdal2.Name = txt_Searchname.Text;
            objdal2.DateFrom =Convert.ToDateTime(txt_SearchDateFrom.Text);
            objdal2.DateTo =Convert.ToDateTime(txt_SearchDateTo.Text);
            objdal2.LeaveType = ddlLeaveType.SelectedItem.Text;
            objdal2.Approve =Convert.ToBoolean(ddlApprove.SelectedItem.Text);
            
           dv= objdal2.Search_Data();
           GridView1.DataSource = dv;
           GridView1.DataBind();
           lblmsg2.Text = "Total Records: " + count;
        }
Posted 1-Feb-13 23:09pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

The simple way is to check at the top of your method:
if (string.IsNullOrWhitespace(txt_SearchDateFrom.Text) || string.IsNullOrWhitespace(txt_SearchDateTo.Text))
   {
   MessageBox.Show("Please enter a form and a to date");
   return;
   }
But that doesn't stop them entering rubbish. I would use that, plus I would use TryParse:
if (DateTime.TryParse(txt_SearchDateFrom.Text, out objdal2.DateFrom) &&
    DateTime.TryParse(txt_SearchDateTo.Text, out objdal2.DateTo))
   {
   ...
   }
But my preference would be to prevent both problems altogether by not using TextBoxes at all! Have you considered a DateTimePicker instead? That way, the user can't enter an invalid date, and you don't have to do any conversion.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi,
Please Validate the Date Textbox should not be empty in (javscript).
or If your passing Empty text box value then just pass the Current date as default date for that textbox in coding wise just check on clicking on search button
if(textbox.text=="") 
{
 
textbox.text=Todaysdate;
}
else
{
 textbox.text="what ever your passing through text box value only"
}
It will work just try and let me know
....
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

if you are using stored procedure then pass date value as null.....
else manage it from front end using c#...................................
 
for more clarificatins send your searchdata code
  Permalink  
Comments
Member 9511889 at 2-Feb-13 5:14am
   
ALTER PROCEDURE dbo.LeaveForm_Search
@DateFrom datetime,
@DateTo datetime,
@Name varchar(50),
@LeaveType varchar(50),
@Approve bit
AS
SELECT Id, Userid, Groupid, [Name],convert(varchar,LeaveFrom,103) as LeaveFrom, convert(varchar,LeaveTo,103) as LeaveTo, LeaveType, Comment, Approve,CreatedOn, CreatedBy, ModifyOn, ModifyBy,ApproveBy
FROM LeaveForm
where (LeaveFrom between @DateFrom and @DateTo) and (LeaveTo between @DateFrom and @DateTo) and [Name] =@Name and LeaveType=@LeaveType and Approve=@Approve





RETURN
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This is because you are trying to convert and non date format into a dateTime at these lines
objdal2.DateFrom =Convert.ToDateTime(txt_SearchDateFrom.Text);
objdal2.DateTo =Convert.ToDateTime(txt_SearchDateTo.Text);
instead of Convert use DateTime.tryParse[^] and use the proper date validation in your search query.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100