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 duplicate before inserting to Database with ASP.Net SqlDataSource using C# and VB.Net<p>Hey <a class="username" rel="dorsa">dorsa</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div&gt; Name: &lt;asp:TextBox ID=&#34;txtName&#34; runat=&#34;server&#34; /&gt;&lt;br /&gt; Country: &lt;asp:TextBox ID=&#34;txtCountry&#34; runat=&#34;server&#34; /&gt; &lt;asp:SqlDataSource ID=&#34;sqlDtSrcEmployees&#34; runat=&#34;server&#34; ConnectionString=&#34;&lt;%$ ConnectionStrings:TestConnectionString %&gt;&#34; InsertCommand=&#34;INSERT INTO [Customers] ([Name], [Country]) VALUES (@Name, @Country)&#34; OnInserting=&#34;sqlDtSrcEmployees_Inserting&#34;&gt; &lt;InsertParameters&gt; &lt;asp:Parameter Name=&#34;Name&#34; Type=&#34;String&#34; /&gt; &lt;asp:Parameter Name=&#34;Country&#34; Type=&#34;String&#34; /&gt; &lt;/InsertParameters&gt; &lt;/asp:SqlDataSource&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;Insert&#34; /&gt; &lt;/div&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;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient</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)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT Name FROM Customers WHERE Name = @Name&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@Name&#34;, txtName.Text); con.Open(); string name = Convert.ToString(cmd.ExecuteScalar()); con.Close(); if (string.IsNullOrEmpty(name)) { sqlDtSrcEmployees.Insert(); } else { ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;CropImage&#34;, &#34;alert(&#39;record exists&#39;);&#34;, true); } } } } protected void sqlDtSrcEmployees_Inserting(object sender, SqlDataSourceCommandEventArgs e) { try { e.Command.Parameters[&#34;@Name&#34;].Value = txtName.Text; e.Command.Parameters[&#34;@Country&#34;].Value = txtCountry.Text; } catch { } }</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) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT Name FROM Customers WHERE Name = @Name&#34;, con) cmd.Parameters.AddWithValue(&#34;@Name&#34;, txtName.Text) con.Open() Dim name As String = Convert.ToString(cmd.ExecuteScalar()) con.Close() If String.IsNullOrEmpty(name) Then sqlDtSrcEmployees.Insert() Else ScriptManager.RegisterStartupScript(Me, Me.[GetType](), &#34;CropImage&#34;, &#34;alert(&#39;record exists&#39;);&#34;, True) End If End Using End Using End Sub Protected Sub sqlDtSrcEmployees_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Try e.Command.Parameters(&#34;@Name&#34;).Value = txtName.Text e.Command.Parameters(&#34;@Country&#34;).Value = txtCountry.Text Catch End Try End Sub</pre>https://www.aspforums.net:443/Threads/983639/Check-duplicate-before-inserting-to-Database-with-ASPNet-SqlDataSource-using-C-and-VBNet/https://www.aspforums.net:443/Threads/983639/Check-duplicate-before-inserting-to-Database-with-ASPNet-SqlDataSource-using-C-and-VBNet/Tue, 11 Dec 2018 04:29:18 GMT