Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

The simple way is to check at the top of your method:
C#
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:
C#
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.
 
Share this answer
 
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
C#
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
....
 
Share this answer
 
v2
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
 
Share this answer
 
Comments
Member 9511889 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
This is because you are trying to convert and non date format into a dateTime at these lines
C#
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.
 
Share this answer
 

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