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: - <asp:DropDownList ID="cboDay" runat="server" Width="55px" Height="32px" AppendDataBoundItems="true">
-
<asp:ListItem></asp:ListItem></asp:DropDownList >
-
<asp:DropDownList ID="cboMonth" runat="server" Width="80px" Height="30px" AppendDataBoundItems="true">
-
<asp:ListItem></asp:ListItem></asp:DropDownList>
-
<asp:DropDownList ID="cboYear" runat="server" Width="65px" Height="30px" AppendDataBoundItems="true">
-
<asp:ListItem></asp:ListItem></asp:DropDownList>
-
code behind:
-
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (!IsPostBack)
-
{
-
GetDaysListed(cboDay);
-
GetMonthsListed(cboMonth);
-
GetYearsListed(cboYear);
-
}
-
}
-
protected void btnSave_Click(object sender, EventArgs e)
-
{
-
try
-
{
-
if (txtfname.Text == String.Empty)
-
{
-
lblname.Text = "First Name Required";
-
lblname.Visible = true;
-
}
-
if (txtlname.Text == String.Empty)
-
{
-
lbllname.Text = "Last name Required";
-
lbllname.Visible = true;
-
}
-
else
-
{
-
SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
-
SqlCommand cmd = new SqlCommand("qryINSERTprof", con);
-
cmd.CommandType = System.Data.CommandType.Text;
-
-
String str = cboDay.SelectedItem.Text + "/" + cboMonth.SelectedItem.Text + "/" + cboYear.SelectedItem.Text;
-
lbltxtage.Text = str;
-
DateTime dt = Convert.ToDateTime(str);
-
-
string bdate = lbltxtage.Text;
-
DateTime dts = DateTime.Parse(bdate);
-
TimeSpan d = DateTime.Now.Subtract(dts);
-
double years = d.TotalDays / 365;
-
int months = (int)((years - (int)years) * 12);
-
txtage.Text = ((int)years).ToString();
-
-
Response.Write(dt.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture));
-
cmd.Parameters.AddWithValue("@Lname", txtlname.Text);
-
cmd.Parameters.AddWithValue("@Mname", txtmname.Text);
-
cmd.Parameters.AddWithValue("@Fname", txtfname.Text);
-
cmd.Parameters.AddWithValue("@BirthDate", lbltxtage.Text);
-
cmd.Parameters.AddWithValue("@age", txtage.Text);
-
cmd.CommandType = System.Data.CommandType.StoredProcedure;
-
con.Open();
-
cmd.ExecuteNonQuery();
-
Response.Write("<script>alert('Record Successfully Added');</script>");
-
clear();
-
}
-
}
-
catch (Exception exe)
-
{
-
throw exe;
-
}
-
-
}
-
public void GetDaysListed(DropDownList DDDayList)
-
{
-
for (int i = 1; i < 32; i++)
-
{
-
ListItem list = new ListItem();
-
list.Text = i.ToString();
-
list.Value = i.ToString();
-
DDDayList.Items.Add(list);
-
}
-
ListItem mlist = new ListItem();
-
mlist.Text = null;
-
mlist.Value = null;
-
mlist.Selected = true;
-
DDDayList.Items.Add(mlist);
-
}
-
-
-
public void GetMonthsListed(DropDownList DDMonthList)
-
{
-
DateTime month = Convert.ToDateTime("1/1/2007");
-
for (int i = 0; i < 12; i++)
-
{
-
-
-
DateTime NextMont = month.AddMonths(i);
-
ListItem list = new ListItem();
-
list.Text = NextMont.ToString("MMMM");
-
list.Value = NextMont.Month.ToString();
-
DDMonthList.Items.Add(list);
-
}
-
-
-
ListItem mlist = new ListItem();
-
mlist.Text = null;
-
mlist.Value = null;
-
mlist.Selected = true;
-
DDMonthList.Items.Add(mlist);
-
}
-
-
-
public void GetYearsListed(DropDownList DDYearList)
-
{
-
int yearLast = DateTime.Now.Year + 18;
-
int yearThen = yearLast - 90;
-
for (int i = yearThen; i < yearLast; i++)
-
{
-
ListItem list = new ListItem();
-
list.Text = yearThen.ToString();
-
list.Value = yearThen.ToString();
-
DDYearList.Items.Add(list);
-
yearThen += 1;
-
}
-
-
ListItem mlist = new ListItem();
-
mlist.Text = null;
-
mlist.Value = null;
-
mlist.Selected = true;
-
DDYearList.Items.Add(mlist);
-
}
-
public string GetAge(DateTime dateOfBirth)
-
{
-
int iToday = int.Parse(DateTime.Today.ToString("yyyyMMdd"));
-
int iDateOfBirth = int.Parse(dateOfBirth.ToString("yyyyMMdd"));
-
-
string diff = (iToday - iDateOfBirth).ToString();
-
string age = diff.Substring(0, diff.Length - 4);
-
-
return age;
-
}
-
my query:
-
-
ALTER PROCEDURE qryINSERTprof
-
@Fname Varchar(50),
-
@Mname Varchar(50),
-
@Lname Varchar(50),
-
@BirthDate datetime,
-
@age varchar(50)
-
AS
-
BEGIN
-
INSERT INTO Profile ( Fname, Mname, Lname, BirthDate, Age )
-
VALUES (@Fname, @Mname, @Lname, @BirthDate, @age)
-
-
END
-
RETURN
Any help would be appreciated. Thanks in advance.
2 7689
It would help if you told us:
1) What line throws the error.
2) What the string value was at the time of error.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
...
|
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
...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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'...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |