Articles → .NET → Conversion Of A Varchar Data Type To A Datetime Data Type Resulted In An Out-Of-Range Value
Conversion Of A Varchar Data Type To A Datetime Data Type Resulted In An Out-Of-Range Value
Software Requirement
- Visual studio 2005(or above) is installed on your machine.
- SQL Server 2008(or above) is installed on your machine.
Prerequisite Knowledge
- Basics about SQL objects like tables.
- Basics about C#.
- How to create tables in SQL server and how to insert data into the table?
- How to create a project using visual studio?
- Basic knowledge about basic controls like button textbox etc and their events.
- How to connect to SQL server database using ADO.NET objects.
Steps Of Execution
- Create a new table
- Create a new project
- Add controls on the page
- Add code to insert data in database table.
Create A New Table
Click to Enlarge
Create A New Project
Click to Enlarge
Add Controls On The Page
Click to Enlarge
Add Code To Insert Data In Database Table
protected void btnInsert_Click(object sender, EventArgs e) {
using(SqlConnection connection = new SqlConnection("connection_string")) {
// Open a connection
connection.Open();
string query = string.Empty;
if (string.IsNullOrEmpty(txtDOB.Text)) query = string.Format("insert into Employee values('{0}','{1}')", txtEmployeeName.Text, DateTime.MinValue);
else query = string.Format("insert into Employee values('{0}','{1}')", txtEmployeeName.Text, Convert.ToDateTime(txtDOB.Text));
using(SqlCommand command = new SqlCommand(query, connection)) {
command.ExecuteNonQuery();
}
}
}
Click to Enlarge