Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my some coding which so some error string was not valid date time
C#
protected void btnsave_Click(object sender, EventArgs e)
        {

            string dfs = "26-06-1986";
            DateTime dd = new DateTime();
            dd = Convert.ToDateTime(dfs);


            SqlCommand cmd = new SqlCommand("insert into dk values('"
+ TextBox1.Text+ "','" + TextBox2.Text+ "')", con);


            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
}
Posted
Updated 11-Jun-14 1:49am
v2
Comments
[no name] 11-Jun-14 7:47am    
You are getting that error because "dfs" is not the correct format for a datetime.
Chris875 11-Jun-14 7:48am    
Please give some more information. how does the table dk look like (column specification)? What are typical values for Textbox1 and Textbox2?

You can read more about Convert.ToDateTime from here[^].

Probably it's not a valid datetime format what you are using in the following line of code:
C#
string dfs = "26-06-1986";


Try this for example:
C#
string dfs = "26/06/1986";
 
Share this answer
 
Here's your date, if you can't make it out of the dashes and into the default date patterns:

C#
string dfs = "26-06-1986";
DateTime dd = new DateTime(Convert.ToInt32(dfs.Substring(6)), Convert.ToInt32(dfs.Substring(3, 2)), Convert.ToInt32(dfs.Substring(0, 2)));
 
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