473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String was not recognized as a valid DateTime

I am getting this error whenever I leave my drop down lists blank: "String was not recognized as a valid DateTime". My code is below:

aspx:

Expand|Select|Wrap|Line Numbers
  1. <asp:DropDownList ID="cboDay" runat="server" Width="55px" Height="32px" AppendDataBoundItems="true">
  2.                 <asp:ListItem></asp:ListItem></asp:DropDownList >
  3.     <asp:DropDownList ID="cboMonth" runat="server" Width="80px" Height="30px" AppendDataBoundItems="true">
  4.                 <asp:ListItem></asp:ListItem></asp:DropDownList>
  5.         <asp:DropDownList ID="cboYear" runat="server" Width="65px" Height="30px" AppendDataBoundItems="true">
  6.                 <asp:ListItem></asp:ListItem></asp:DropDownList>
  7. code behind:
  8.  
  9.  protected void Page_Load(object sender, EventArgs e)
  10.  {
  11.         if (!IsPostBack)
  12.         {
  13.             GetDaysListed(cboDay);
  14.             GetMonthsListed(cboMonth);
  15.             GetYearsListed(cboYear);
  16.         }
  17.  }
  18.  protected void btnSave_Click(object sender, EventArgs e)
  19.     {
  20.         try
  21.         {
  22.             if (txtfname.Text == String.Empty)
  23.             {
  24.                 lblname.Text = "First Name Required";
  25.                 lblname.Visible = true;
  26.             }
  27.             if (txtlname.Text == String.Empty)
  28.             {
  29.                 lbllname.Text = "Last name Required";
  30.                 lbllname.Visible = true;
  31.             }
  32.             else
  33.             {
  34.                 SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
  35.                 SqlCommand cmd = new SqlCommand("qryINSERTprof", con);
  36.                 cmd.CommandType = System.Data.CommandType.Text;
  37.  
  38.                 String str = cboDay.SelectedItem.Text + "/" + cboMonth.SelectedItem.Text + "/" + cboYear.SelectedItem.Text;
  39.                 lbltxtage.Text = str;
  40.                 DateTime dt = Convert.ToDateTime(str);
  41.  
  42.                 string bdate = lbltxtage.Text;
  43.                 DateTime dts = DateTime.Parse(bdate);
  44.                 TimeSpan d = DateTime.Now.Subtract(dts);
  45.                 double years = d.TotalDays / 365;
  46.                 int months = (int)((years - (int)years) * 12);
  47.                 txtage.Text = ((int)years).ToString();
  48.  
  49.                 Response.Write(dt.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture));
  50.                 cmd.Parameters.AddWithValue("@Lname", txtlname.Text);
  51.                 cmd.Parameters.AddWithValue("@Mname", txtmname.Text);
  52.                 cmd.Parameters.AddWithValue("@Fname", txtfname.Text);
  53.                 cmd.Parameters.AddWithValue("@BirthDate", lbltxtage.Text);
  54.                 cmd.Parameters.AddWithValue("@age", txtage.Text);
  55.                 cmd.CommandType = System.Data.CommandType.StoredProcedure;
  56.                 con.Open();
  57.                 cmd.ExecuteNonQuery();
  58.                 Response.Write("<script>alert('Record Successfully Added');</script>");
  59.                 clear();
  60.             }
  61.         }
  62.         catch (Exception exe)
  63.         {
  64.             throw exe;
  65.         }
  66.  
  67.     }
  68. public void GetDaysListed(DropDownList DDDayList)
  69.     {
  70.         for (int i = 1; i < 32; i++)
  71.         {
  72.             ListItem list = new ListItem();
  73.             list.Text = i.ToString();
  74.             list.Value = i.ToString();
  75.             DDDayList.Items.Add(list);
  76.         }
  77.         ListItem mlist = new ListItem();
  78.         mlist.Text = null;
  79.         mlist.Value = null;
  80.         mlist.Selected = true;
  81.         DDDayList.Items.Add(mlist);
  82.     }
  83.  
  84.  
  85.     public void GetMonthsListed(DropDownList DDMonthList)
  86.     {
  87.         DateTime month = Convert.ToDateTime("1/1/2007");
  88.         for (int i = 0; i < 12; i++)
  89.         {
  90.  
  91.  
  92.             DateTime NextMont = month.AddMonths(i);
  93.             ListItem list = new ListItem();
  94.             list.Text = NextMont.ToString("MMMM");
  95.             list.Value = NextMont.Month.ToString();
  96.             DDMonthList.Items.Add(list);
  97.         }
  98.  
  99.  
  100.         ListItem mlist = new ListItem();
  101.         mlist.Text = null;
  102.         mlist.Value = null;
  103.         mlist.Selected = true;
  104.         DDMonthList.Items.Add(mlist);
  105.     }
  106.  
  107.  
  108.     public void GetYearsListed(DropDownList DDYearList)
  109.     {
  110.         int yearLast = DateTime.Now.Year + 18;
  111.         int yearThen = yearLast - 90;
  112.         for (int i = yearThen; i < yearLast; i++)
  113.         {
  114.             ListItem list = new ListItem();
  115.             list.Text = yearThen.ToString();
  116.             list.Value = yearThen.ToString();
  117.             DDYearList.Items.Add(list);
  118.             yearThen += 1;
  119.         }
  120.  
  121.         ListItem mlist = new ListItem();
  122.         mlist.Text = null;
  123.         mlist.Value = null;
  124.         mlist.Selected = true;
  125.         DDYearList.Items.Add(mlist);
  126.     }
  127.     public string GetAge(DateTime dateOfBirth)
  128.     {
  129.         int iToday = int.Parse(DateTime.Today.ToString("yyyyMMdd"));
  130.         int iDateOfBirth = int.Parse(dateOfBirth.ToString("yyyyMMdd"));
  131.  
  132.         string diff = (iToday - iDateOfBirth).ToString();
  133.         string age = diff.Substring(0, diff.Length - 4);
  134.  
  135.         return age;
  136.     }
  137. my query:
  138.  
  139. ALTER PROCEDURE qryINSERTprof
  140.     @Fname Varchar(50),
  141.     @Mname Varchar(50),
  142.     @Lname Varchar(50),
  143.     @BirthDate datetime,
  144.     @age varchar(50)
  145. AS
  146.     BEGIN
  147.         INSERT INTO Profile ( Fname, Mname, Lname, BirthDate, Age )
  148. VALUES (@Fname, @Mname, @Lname, @BirthDate, @age)
  149.  
  150.     END
  151.     RETURN

Any help would be appreciated. Thanks in advance.
Jun 10 '13 #1
2 7689
Rabbit
12,516 Expert Mod 8TB
It would help if you told us:
1) What line throws the error.
2) What the string value was at the time of error.
Jun 10 '13 #2
Frinavale
9,735 Expert Mod 8TB
What Line?
Have you tried using one of the DateTime.Parse methods?

-Frinny
Jun 12 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: RSH | last post by:
I am having a bit of trouble filling a datatable with a query. I'm getting the error message: System.FormatException: String was not recognized as a valid DateTime. at...
6
by: Rookie Card | last post by:
I know this is a very common issue but cannot find a thread that resolves the issue. I need to re-format my dates to "MMM d, yyyy". The problem I have is I have an MSSQL database that allows...
11
by: Bob Day | last post by:
The IsDate code below should result in False, instead it throws the exception below. Why? How do I check if a string can be converted to a date if this function does not work properly? Bob ...
1
by: database | last post by:
I AM FACING A PROBLEM WHEN I ACCESS ONE OF THE PAGES. IT USED TO WORK BEFORE BUT AFTER RESTARTING THE APPLICATION SEVER IT GIVES THE FOLLOWING ERROR. - Server Error ...
1
by: Scott Holman | last post by:
I'm connecting to a java web service that defines the following complex type in the wsdl <xs:complexType name="employee"> <xs:sequence> <xs:element minOccurs="0" name="firstName"...
10
by: mercea | last post by:
hi all, i'm trying to insert the time/date a button was clicked on a gridview and it generates an error:the string was not recognized as a valid DateTime.There is an unknown word starting at index 0...
0
by: Andres Bohren | last post by:
Hi i have a GridView and have Problems to put the values into the right Datatypes. Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As...
4
by: dhanashivam | last post by:
hi, in my web application i am getting date values from sql server database and store in the datatable. it works fine when i use the US culture. But when i change the culture to british i am...
1
by: Magnus.Moraberg | last post by:
Hi, I have the following event which checks if a cell in a dgv contains a date and if so, checks if it is formatted correctly. I don't think its good practice for be to try this by catching an...
3
by: lucindaa | last post by:
Hi, i have a string variable having date as following format "05-01-2010" when convert this date into DateTime Format i got the following error 'String was not recognized as a valid DateTime'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.