TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Use DateTime Picker and Save in Database in Window Form
Munesh Sharma
Oct 09, 2014
7.7
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
Here i will explain how to DateTime Picker and how to save its value in database.
Step 1: Form
Drag and down a Datetime Picker from ToolBox. and go its property and set "Custom Format" as"yyyy-mm-dd", and other property "Format" as "custom".
Step 2:
Code
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Data.SqlClient;
namespace
First_Csharp_app
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
String gender;
//we have to define this
private
void
button1_Click(
object
sender, EventArgs e)
{
SqlConnection con =
new
SqlConnection(str);
String str =
"server=MUNESH-PC;database=windowapp;UID=sa;password=123"
;
String query =
"insert into data (E.id,name,surname,age,gender,DOB) values ('"
+
this
.eid_txt.tex t+
"','"
+
this
.nametxt.text+
"','"
+
this
.surname_txt.text+
"','"
+
this
.age_txt.text+
"' , '"
+
this
.gend er+
"' , '"
+
this
.DateTimePicker1.Text+
"')"
;
SqlCommand cmd =
new
sqComamnd(query,con); SqlDataReader dbr;
try
{con.open();
{
dbr = cmd.ExecuteReader();MessageBox.Show(
"saved"
);
while
(dbr.read()){}
}
catch
(Exception es)
{
MessageBox.Show(es.Message);
}
}
private
void
rediobutton1.
checked
(
object
sender, EventArgs e)
{
gender =
"male"
;
}
private
void
rediobutton1.
checked
(
object
sender, EventArgs e)
{
gender =
"female"
;
}
}
}
Step 3:
Output
Run your application:
you can go on my
Blog_Munesh
How to use DateTime Picker and save in database in window form
Next Recommended Reading
How To Create Login Form In Windows Application Using C#