using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; // namespace for gridview
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //namespace for sqlconnection
namespace retrive_data_by_using_datetime
{
public partial class Form1 : Form
{
public DateTime dt;
public Form1()
{
InitializeComponent();
}
//connection string
SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=student;Integrated Security=True");
//insert date into the database using date time picker
private void button1_Click(object sender, EventArgs e)
{
con.Open();
//here i am using datetime picker for inserting date
dt = Convert.ToDateTime(dateTimePicker1.Text);
SqlCommand cmd = new SqlCommand("insert into tbl_Date values('"+dt+"')", con);
int r = cmd.ExecuteNonQuery();
if (r >0)
{
MessageBox.Show("saved successfully");
}
con.Close();
}
//retrive (intermediate dates fromdate and todate) date from database and display in gridview
//in datebase when u create specify the datatype datetime
private void button2_Click(object sender, EventArgs e)
{
string from = Convert.ToString(dtpfrom.Text);
string to = Convert.ToString(dtpto.Text);
DateTime fromdate = Convert.ToDateTime(from);
DateTime todate = Convert.ToDateTime(to);
//sqlcommand for retrive date from date base
SqlCommand cmd = new SqlCommand("select datea from tbl_Date where datea between convert(date,'"+fromdate.ToLocalTime()+"',0)and convert(date,'"+todate.ToLocalTime()+"',0)", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
dataGridView2.DataSource = dt;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; // namespace for gridview
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //namespace for sqlconnection
namespace retrive_data_by_using_datetime
{
public partial class Form1 : Form
{
public DateTime dt;
public Form1()
{
InitializeComponent();
}
//connection string
SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=student;Integrated Security=True");
//insert date into the database using date time picker
private void button1_Click(object sender, EventArgs e)
{
con.Open();
//here i am using datetime picker for inserting date
dt = Convert.ToDateTime(dateTimePicker1.Text);
SqlCommand cmd = new SqlCommand("insert into tbl_Date values('"+dt+"')", con);
int r = cmd.ExecuteNonQuery();
if (r >0)
{
MessageBox.Show("saved successfully");
}
con.Close();
}
//retrive (intermediate dates fromdate and todate) date from database and display in gridview
//in datebase when u create specify the datatype datetime
private void button2_Click(object sender, EventArgs e)
{
string from = Convert.ToString(dtpfrom.Text);
string to = Convert.ToString(dtpto.Text);
DateTime fromdate = Convert.ToDateTime(from);
DateTime todate = Convert.ToDateTime(to);
//sqlcommand for retrive date from date base
SqlCommand cmd = new SqlCommand("select datea from tbl_Date where datea between convert(date,'"+fromdate.ToLocalTime()+"',0)and convert(date,'"+todate.ToLocalTime()+"',0)", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
dataGridView2.DataSource = dt;
}
}
}
No comments:
Post a Comment