Hi Corobori,
Please refer below sample.
HTML
<asp:SqlDataSource runat="server" ID="sds1" SelectCommand="SELECT TOP 4 * FROM Customers"></asp:SqlDataSource>
<asp:SqlDataSource runat="server" ID="sds2" SelectCommand="SELECT TOP 4 * FROM Customers"></asp:SqlDataSource>
<asp:GridView runat="server" DataSourceID="sds1"></asp:GridView>
<hr />
<asp:GridView runat="server" DataSourceID="sds2"></asp:GridView>
Namespaces
C#
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
protected void Page_Init(object sender, EventArgs e)
{
SqlConnectionStringBuilder sConn = new SqlConnectionStringBuilder();
sConn.DataSource = @".s\SQL2019";
sConn.InitialCatalog = "AjaxSamples";
sConn.UserID = "sa";
sConn.Password = "pass@123";
sConn.ConnectTimeout = 45;
sConn.IntegratedSecurity = false;
foreach (SqlDataSource sds in form1.Controls.OfType<SqlDataSource>())
{
sds.ConnectionString = sConn.ConnectionString;
}
}
VB.Net
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
Dim sConn As New SqlConnectionStringBuilder()
With sConn
.DataSource = ".\SQL2019"
.InitialCatalog = "AjaxSamples"
.UserID = "sa"
.Password = "pass@123"
.ConnectTimeout = 45
.IntegratedSecurity = False
End With
For Each sds As SqlDataSource In form1.Controls.OfType(Of SqlDataSource)()
sds.ConnectionString = sConn.ConnectionString
Next
End Sub
Screenshot