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.Read ConnectionStrings from Class File using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="SUJAYS"> SUJAYS</a>,</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; ID=&#34;gvCustomers&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;CustomerId&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#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.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>Connection.cs</strong></p> <pre class="brush: csharp">using System.Configuration; public class Connection { public string GetConnectionString() { string constring = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; return constring; } }</pre> <p><strong>CS.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { Connection connectioString = new Connection(); using (SqlConnection con = new SqlConnection(connectioString.GetConnectionString())) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM Customers&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); this.gvCustomers.DataSource = dt; this.gvCustomers.DataBind(); } } } } }</pre> <p><strong>VB.Net</strong></p> <p><strong>ConnectionVB.vb</strong></p> <pre class="brush: vb">Imports Microsoft.VisualBasic Imports System.Configuration Public Class ConnectionVB Public Function GetConnectionString() As String Dim constring As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Return constring End Function End Class</pre> <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 If Not Me.IsPostBack Then Dim connectioString As ConnectionVB = New ConnectionVB() Using con As SqlConnection = New SqlConnection(connectioString.GetConnectionString()) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM Customers&#34;, con) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) Me.gvCustomers.DataSource = dt Me.gvCustomers.DataBind() End Using End Using End Using End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/y5eCtLz.gif" alt="" width="336" height="299" /></p>https://www.aspforums.net:443/Threads/110849/Read-ConnectionStrings-from-Class-File-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/110849/Read-ConnectionStrings-from-Class-File-using-C-and-VBNet-in-ASPNet/Thu, 07 Feb 2019 23:02:43 GMT