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.Check whether Time range exists in database or not using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="democloud">democloud</a>,</p> <p>Take reference to below code and change your code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">FromDate : &lt;asp:TextBox ID=&#34;txtFromDate&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt; ToDate : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtToDate&#34;&gt;&lt;/asp:TextBox&gt; &lt;br /&gt; &lt;asp:Button ID=&#34;btnInsert&#34; runat=&#34;server&#34; Text=&#34;Insert&#34; OnClick=&#34;Insert&#34; /&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvDate&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &nbsp;&nbsp;&nbsp; &lt;Columns&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:BoundField DataField=&#34;FROMTime&#34; HeaderText=&#34;FROMTime&#34; /&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:BoundField DataField=&#34;TOTime&#34; HeaderText=&#34;TOTime&#34; /&gt; &nbsp;&nbsp;&nbsp; &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; using System.Data.SqlClient; using System.Configuration;</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 Page_Load(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; if (!this.IsPostBack) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(constr); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM TestCheckDate&#34;, con); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter da = new SqlDataAdapter(cmd); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(dt); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gvDate.DataSource = dt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gvDate.DataBind(); &nbsp;&nbsp;&nbsp; } } protected void Insert(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; &nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(constr); &nbsp;&nbsp;&nbsp; SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM TestCheckDate WHERE FROMTime = @FROMTime AND TOTime = @ToDate&#34;, con); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@FROMTime&#34;, Convert.ToDateTime(txtFromDate.Text.Trim()).ToShortTimeString()); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@ToDate&#34;, Convert.ToDateTime(txtToDate.Text.Trim()).ToShortTimeString()); &nbsp;&nbsp;&nbsp; SqlDataAdapter da = new SqlDataAdapter(cmd); &nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp; da.Fill(dt); &nbsp;&nbsp;&nbsp; if (dt.Rows.Count &lt;= 0) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd = new SqlCommand(&#34;INSERT INTO DateTable(FromDate,ToDate) VALUES(@FromDate,@ToDate)&#34;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@FromDate&#34;, txtFromDate.Text); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@ToDate&#34;, txtToDate.Text); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Close(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;Message&#34;, &#34;alert(&#39;Recurd Inserted !&#39;);&#34;, true); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;Message&#34;, &#34;alert(&#39;Already Exist Record !&#39;);&#34;, true); &nbsp;&nbsp;&nbsp; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load &nbsp;&nbsp;&nbsp; If Not Me.IsPostBack Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM TestCheckDate&#34;, con) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(dt) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gvDate.DataSource = dt &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gvDate.DataBind() &nbsp;&nbsp;&nbsp; End If End Sub Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp; Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString &nbsp;&nbsp;&nbsp; Dim con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp; Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM TestCheckDate WHERE FROMTime = @FROMTime AND TOTime = @ToDate&#34;, con) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@FROMTime&#34;, Convert.ToDateTime(txtFromDate.Text.Trim()).ToShortTimeString()) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@ToDate&#34;, Convert.ToDateTime(txtToDate.Text.Trim()).ToShortTimeString()) &nbsp;&nbsp;&nbsp; Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) &nbsp;&nbsp;&nbsp; Dim dt As DataTable = New DataTable() &nbsp;&nbsp;&nbsp; da.Fill(dt) &nbsp;&nbsp;&nbsp; If dt.Rows.Count &lt;= 0 Then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd = New SqlCommand(&#34;INSERT INTO DateTable(FromDate,ToDate) VALUES(@FromDate,@ToDate)&#34;) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@FromDate&#34;, txtFromDate.Text) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@ToDate&#34;, txtToDate.Text) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Open() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Close() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptManager.RegisterStartupScript(Me, Me.GetType(), &#34;Message&#34;, &#34;alert(&#39;Recurd Inserted !&#39;);&#34;, True) &nbsp;&nbsp;&nbsp; Else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ScriptManager.RegisterStartupScript(Me, Me.GetType(), &#34;Message&#34;, &#34;alert(&#39;Already Exist Record !&#39;);&#34;, True) &nbsp;&nbsp;&nbsp; End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/Ak7nqih.gif" alt="" width="253" height="340" /></p>https://www.aspforums.net:443/Threads/742498/Check-whether-Time-range-exists-in-database-or-not-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/742498/Check-whether-Time-range-exists-in-database-or-not-using-C-and-VBNet-in-ASPNet/Mon, 06 May 2019 00:48:08 GMT