i have an ajax Calender extender along with textbox in gridview, it works fine, when i select some date from calendar then it tranfers date to textbox and after clicking Update button in gridview , it sends date to database , Fine

but when i don't select any thing from calender i.e. when textbox remains empty , and when i click update button , then it sends 1900-01-01,

why ? please someone help ......

here is my code , even i treid if else conditions but else never gets executed ,

code:

protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Update")
        {
            //Label lblMsgEmail = new Label();
            GridView gvr = GridView1;
            //GridViewRow r = GridView1.Rows;
            int index = Convert.ToInt32(e.CommandArgument);
            int pk = Convert.ToInt32(GridView1.DataKeys[index].Value);
            TextBox txtb = (TextBox) (gvr.Rows[index].FindControl("txtCal"));
           // TextBox txtb1 = (TextBox)(gvr.Rows[index].FindControl("txtCal2"));
            AjaxControlToolkit.CalendarExtender txtBox = (AjaxControlToolkit.CalendarExtender)(gvr.Rows[index].FindControl("CalendarExtender1"));
            Response.Write(txtb.Text);
            txtb.Text = txtBox.SelectedDate.ToString();
            String date = Request.Form[txtb.UniqueID];
            Response.Write(date);
            if(date != null || date!="")
           {
            SqlDataSource1.UpdateCommand = "UPDATE [tblScruitnyTeam] SET [interview] = '"+ date +"', [name] = @name, [designation] = @designation, [commiteerole] = @commiteerole, [signature] = @signature, [userid] = @userid WHERE [teamid] ='"+pk+"'";
           }
            else
            {
             SqlDataSource1.UpdateCommand = "UPDATE [tblScruitnyTeam] SET [interview] = '" + "Null" + "', [name] = @name, [designation] = @designation, [commiteerole] = @commiteerole, [signature] = @signature, [userid] = @userid WHERE [teamid] ='" + pk + "'";
             Response.Write("Else");
            }

Parse the string and wrap update code in,

Datetime dt;
if(DateTime.TryParse(date,out dt))
 {
   //code to update record
 }

EDIT:

@HunainHafeez : Done, Thanks alot man ! but just for learning purpose tel me that how did it work ?

I think return value of Calendar extender will be DateTime.MinValue. if you didn't select the calendar then this statement String date = Request.Form[txtb.UniqueID]; assign invalidate date value. Using DateTime.Parse, DateTime.TryParse and DateTime.ExactTryParse - static methods of DateTime struct, you can parse the string data to DateTime object and also validate date/time value.

commented: wowwwwwwwwwww ! Done, Thanks alot man ! but just for learning purpose tel me that how did it work ? +2

Your conditional statement is always true probably because even If you don't select a date, that is the default value, not null or "" as you would expect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.