Hey akhter,
Please refer below sample.
HTML
EmployeeId :
<asp:TextBox ID="txtId" runat="server" />
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
<asp:GridView runat="server" ID="gvDaetTime" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Time" HeaderText="Time" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Insert(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand cmd = new SqlCommand("SELECT BirthDate FROM Employees WHERE EmployeeId=@EmployeeId", con);
cmd.Parameters.AddWithValue("@EmployeeId", txtId.Text);
con.Open();
DateTime birthdate = Convert.ToDateTime(cmd.ExecuteScalar());
con.Close();
string date = birthdate.Date.ToString("dd/MM/yyyy");
string time = birthdate.Date.ToString("hh:mm:ss tt");
cmd = new SqlCommand("INSERT INTO TestDatePart(Date,Time) VALUES(@Date,@Time)", con);
cmd.Parameters.AddWithValue("@Date", date);
cmd.Parameters.AddWithValue("@Time", time);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
this.BindGrid();
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Date,Time FROM TestDatePart", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
this.gvDaetTime.DataSource = dt;
this.gvDaetTime.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT BirthDate FROM Employees WHERE EmployeeId=@EmployeeId", con)
cmd.Parameters.AddWithValue("@EmployeeId", txtId.Text)
con.Open()
Dim birthdate As DateTime = Convert.ToDateTime(cmd.ExecuteScalar())
con.Close()
Dim datee As String = birthdate.Date.ToString("dd/MM/yyyy")
Dim time As String = birthdate.Date.ToString("hh:mm:ss tt")
cmd = New SqlCommand("INSERT INTO TestDatePart(Date,Time) VALUES(@Date,@Time)", con)
cmd.Parameters.AddWithValue("@Date", datee)
cmd.Parameters.AddWithValue("@Time", time)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Me.BindGrid()
End Using
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Date,Time FROM TestDatePart", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvDaetTime.DataSource = dt
Me.gvDaetTime.DataBind()
End Using
End Using
End Using
End Using
End Sub
Screenshot