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.Create common class for Database connection using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="nauna"> nauna</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:Label ID=&#34;lblName&#34; runat=&#34;server&#34; /&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> <p><strong>CS.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { ConnectionClass connection = new ConnectionClass(); string name = &#34;&#34;; string query = &#34;get_CustomersData&#34;; SqlCommand cmd = connection.getCommand(query); cmd.Parameters.AddWithValue(&#34;@CustomerId&#34;, 1); cmd.Connection.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { name = dr[&#34;Name&#34;].ToString(); } cmd.Connection.Close(); lblName.Text = &#34;The Name is : &lt;b&gt;&#34; + name + &#34;&lt;/b&gt;&#34;; }</pre> <p><strong>ConnectionClass.cs</strong></p> <pre class="brush: csharp">public class ConnectionClass { public SqlCommand getCommand(string query) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString); SqlCommand cmd = new SqlCommand(query, con); cmd.CommandType = CommandType.StoredProcedure; return cmd; } }</pre> <p><strong>VB.Net</strong></p> <p><strong>VB.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load Dim connection As ConnectionClassVB = New ConnectionClassVB() Dim name As String = &#34;&#34; Dim query As String = &#34;get_CustomersData&#34; Dim cmd As SqlCommand = connection.getCommand(query) cmd.Parameters.AddWithValue(&#34;@CustomerId&#34;, 1) cmd.Connection.Open() Dim dr As SqlDataReader = cmd.ExecuteReader() If dr.Read() Then name = dr(&#34;Name&#34;).ToString() End If cmd.Connection.Close() lblName.Text = &#34;The Name is : &lt;b&gt;&#34; &amp; name &amp; &#34;&lt;/b&gt;&#34; End Sub</pre> <p><strong>ConnectionClassVB.vb</strong></p> <pre class="brush: vb">Public Class ConnectionClassVB Public Function getCommand(ByVal query As String) As SqlCommand Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString) Dim cmd As SqlCommand = New SqlCommand(query, con) cmd.CommandType = CommandType.StoredProcedure Return cmd End Function End Class</pre> <p><span style="text-decoration: underline;"><strong>Output</strong></span></p> <div><span id="lblName">The Name is : <strong>John Hammond</strong></span></div>https://www.aspforums.net:443/Threads/113087/Create-common-class-for-Database-connection-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/113087/Create-common-class-for-Database-connection-using-C-and-VBNet-in-ASPNet/Wed, 17 Apr 2019 07:13:07 GMT