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

i write code given below in that code i got an error String was not recognized as a valid DateTime. can anybody tell me where i wrong or it issues of anything else

C#
protected void imgbtnSave_Click(object sender, ImageClickEventArgs e)
   {
       try
       {
           ///''''''''''' data save '''''''''''''''
           Label ctrl = new Label();
           ctrl = (Label)this.UploadCtrl1.Controls[0];
           objAdmin.Product  = txtpname.Text;
           objAdmin.ClubId  = GlobleVariable.clubid;
           objAdmin.activestatus  = true;
           objAdmin.ProductTypeID = Convert.ToInt32(ddltype.SelectedValue);
           objAdmin.Description = txtdescription.Text.Trim();
           objAdmin.DefaultPrice = Convert.ToDouble(txtdprice.Text);
           //DateTime dtime = DateTime.ParseExact(txtpfdate.Text.Trim(),"MM/dd/yyyy");
           //dtime = Convert.ToDateTime(DateTime.Today.Date.ToShortDateString());
          // objAdmin.ProductFinishDate = string.Format("{0:MM/dd/yyyy}", dtime);
           //objAdmin.ProductFinishDate = string.Format("{0:MM/dd/yyyy}", dtime);
           //objAdmin.ProductFinishDate = DateTime.ParseExact(txtpfdate.Text.Trim(), "dd/MM/yyyy", null);
           //dt = DateTime.ParseExact(txtpfdate.Text,string.Format("dd/MM/yyyy"));
           //objAdmin.ProductFinishDate = Convert.ToDateTime(dt.ToString("dd/MM/yyyy"));
           //CultureInfo ci = new CultureInfo("en-GB");
DateTime dt=new DateTime();           
string strdt = txtpfdate.Text.Trim();
           dt = Convert.ToDateTime(strdt);
           objAdmin.ProductFinishDate = Convert.ToDateTime(dt);
           //objAdmin.ProductFinishDate = Convert.ToDateTime(txtpfdate.Text);
           objAdmin.AttachmentID = Convert.ToInt32(ctrl.Text);

           if (Page.IsValid)
           {
               if (Request["Flag"] == "Edit")
               {
                   objAdmin.ClubId  = GlobleVariable.clubid;
                   objAdmin.productid  = Convert.ToInt32(Request["productid"]);
                   if (!Convert.ToBoolean(objAdmin.checkforsameproduct(true)))
                   {
                       objAdmin.Update();
                       lblmsgs.Text = "Products Details Updated Successfully.";
                       Response.Redirect("products.aspx?flag=edit&email=" + Request["email"]);
                   }
                   else
                   {
                       lblmsgs.Text = "This Product Name is already exist";
                   }
               }
               else
               {
                   if (!Convert.ToBoolean(objAdmin.checkforsameproduct(false)))
                   {
                       objAdmin.Save();
                       lblmsgs.Text = "Product Details Added Successfully.";
                       txtpname.Text = "";
                       txtdescription.Text = "";
                       txtdprice.Text = "";
                       txtpfdate.Text = "";
                       Response.Redirect("products.aspx?flag=add");
                   }
                   else
                   {
                       lblmsgs.Text = "Product Name Already Exist";
                   }
               }
           }
       }
       catch (Exception ex)
       {
           lblmsgs.Text = ex.Message;
       }
  }


Thanks
Posted
Comments
m@dhu 19-Jan-11 7:07am    
Check whether the value you are passing in txtpfdate.Text is a valid datetime.
narendrarathod 19-Jan-11 7:30am    
Thanks for your useful reply i tried lots of think and i use java script for pop up calendar i dont know i get error

1 solution

C#
DateTime dt=new DateTime();
string strdt = txtpfdate.Text.Trim();
dt = Convert.ToDateTime(strdt);
objAdmin.ProductFinishDate = Convert.ToDateTime(dt);

Why are you converting a DateTime to a DateTime?

The error will occur if the textbox format is not correct for the current regional time format settings. Basically, if the user gets it wrong. Instead of using Convert.ToDateTime(string)
consider using DateTime.TryParse which returns a bool for success / fail. You can then report the error and stop what you are doing until he gets it right. MSDN[^]
 
Share this answer
 
Comments
Hiren solanki 19-Jan-11 7:15am    
Exactly what I was thinking. +5
Espen Harlinn 19-Jan-11 7:42am    
5+ Nice and simple, covers the essentials
narendrarathod 19-Jan-11 8:22am    
thank you very much now i solve it
thatraja 19-Jan-11 9:38am    
Good answer
fjdiewornncalwe 19-Jan-11 21:16pm    
Excellent

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