ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Split DateTime column into Date and time using C# and VB.Net in ASP.Net<p>Hey <a class="username" rel="akhter"> akhter</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">EmployeeId : &lt;asp:TextBox ID=&#34;txtId&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;Insert&#34; /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;gvDaetTime&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Date&#34; HeaderText=&#34;Date&#34; /&gt; &lt;asp:BoundField DataField=&#34;Time&#34; HeaderText=&#34;Time&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Configuration; using System.Data; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Insert(object sender, EventArgs e) { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { SqlCommand cmd = new SqlCommand(&#34;SELECT BirthDate FROM Employees WHERE EmployeeId=@EmployeeId&#34;, con); cmd.Parameters.AddWithValue(&#34;@EmployeeId&#34;, txtId.Text); con.Open(); DateTime birthdate = Convert.ToDateTime(cmd.ExecuteScalar()); con.Close(); string date = birthdate.Date.ToString(&#34;dd/MM/yyyy&#34;); string time = birthdate.Date.ToString(&#34;hh:mm:ss tt&#34;); cmd = new SqlCommand(&#34;INSERT INTO TestDatePart(Date,Time) VALUES(@Date,@Time)&#34;, con); cmd.Parameters.AddWithValue(&#34;@Date&#34;, date); cmd.Parameters.AddWithValue(&#34;@Time&#34;, time); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } this.BindGrid(); } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT Date,Time FROM TestDatePart&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { da.Fill(dt); this.gvDaetTime.DataSource = dt; this.gvDaetTime.DataBind(); } } } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT BirthDate FROM Employees WHERE EmployeeId=@EmployeeId&#34;, con) cmd.Parameters.AddWithValue(&#34;@EmployeeId&#34;, txtId.Text) con.Open() Dim birthdate As DateTime = Convert.ToDateTime(cmd.ExecuteScalar()) con.Close() Dim datee As String = birthdate.Date.ToString(&#34;dd/MM/yyyy&#34;) Dim time As String = birthdate.Date.ToString(&#34;hh:mm:ss tt&#34;) cmd = New SqlCommand(&#34;INSERT INTO TestDatePart(Date,Time) VALUES(@Date,@Time)&#34;, con) cmd.Parameters.AddWithValue(&#34;@Date&#34;, datee) cmd.Parameters.AddWithValue(&#34;@Time&#34;, time) con.Open() cmd.ExecuteNonQuery() con.Close() Me.BindGrid() End Using End Sub Private Sub BindGrid() Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT Date,Time FROM TestDatePart&#34;, 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</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><span style="text-decoration: underline;"><strong><img src="https://imgur.com/JtfL7YS.gif" alt="" width="257" height="208" /></strong></span></p>https://www.aspforums.net:443/Threads/601098/Split-DateTime-column-into-Date-and-time-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/601098/Split-DateTime-column-into-Date-and-time-using-C-and-VBNet-in-ASPNet/Thu, 06 Dec 2018 23:57:13 GMT