Good morning DENTONE, welcome to Microsoft Newsgroup Support Service! My
name is Jialiang Ge (MSFT) and I will help you with this issue.
According to the post, you wonders how to write VB.NET code to bind a
table
content to an ASP.NET GridView control. The DB of the table is dynamically
specified by the name of the user. If I'm off base, please feel free to
let
me know.
To well demonstrate how to do this without ASP.NET GridView Wizard, I
write
the following VB.NET code for your reference. I will explain the codes in
detail, so that you can have a clearer picture of how it works:
First, because the DB connection is dynamically determined by the current
user name, we need a function that can return the corresponding DB's
connection string for the user name:
Public Function GetConnectionString(ByVal userName As String) As String
'in this function, please customize it to fit your specific scenario
'if the username is exactly equals connectstring name, we can write the
function as
'Return ConfigurationManager.ConnectionStrings(userName).ConnectionString
If userName = "Tom" Then Return
ConfigurationManager.ConnectionStrings("TomConnStr").ConnectionString
If userName = "Jef" Then Return
ConfigurationManager.ConnectionStrings("JefConnStr").ConnectionString
Return
ConfigurationManager.ConnectionStrings("DefaultConnStr").ConnectionString
End Function
We can put the connection strings in web.config:
<connectionStrings>
<add name="TomConnStr" connectionString="Data Source=[servername];Initial
Catalog=[dbname];User ID=[userid];Password=[pwd]"
providerName="System.Data.SqlClient"/>
<add name=" JefConnStr" connectionString="Data Source=[servername];Initial
Catalog=[dbname];User ID=[userid];Password=[pwd]"
providerName="System.Data.SqlClient"/>
¡¡ other connection strings
</connectionStrings>
Second, we write a function to retrieve the table content:
The first parameter specifies the username, which will be used to
determine
the DB connection. The second parameter is the table name in the DB.
Public Function GetDataTable(ByVal userName As String, ByVal tableName As
String) As DataTable
'build a SqlConnection object and open the connection to the DB specified
by userName
Dim conn As New SqlConnection(GetConnectionString(userName))
Try
conn.Open()
Dim dataSet1 As New DataSet("UserNameDS")
Dim dataAdapter As New SqlDataAdapter()
'the SQL select command
dataAdapter.SelectCommand = New SqlCommand("SELECT * FROM [" &
tableName & "]", conn)
dataAdapter.Fill(dataSet1, "UserNameDS")
Return dataSet1.Tables(0)
Catch ex As Exception
Throw ex
Finally
'close the connection
If conn.State = ConnectionState.Open Then conn.Close()
End Try
End Function
Now, to bind the table from GetDataTable to the GridView control, we do it
in this way:
Me.GridView1.DataSource = GetDataTable("Tom", "tblPeople")
Me.GridView1.DataBind()
This piece of binding code can be put in Page_Load:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Load
If Not Page.IsPostBack Then
Me.GridView1.DataSource = GetDataTable("Tom", "tblPeople")
Me.GridView1.DataBind()
End If
End Sub
Or other places where we need to do the binding according to your specific
business logic.
So far, we've bound the DataSource to the GridView control. You did not
mention whether you planed to do paging/sorting/editing or not, if you
want
to do it, the codeproject article:
http://www.codeproject.com/KB/aspnet/gridviewex.aspx
has a good demonstration.
Let me know if the above walkthrough helps you and any other
questions/concerns you have. Also feel free to contact me directly with my
email, which can be found in my signature, so that we can discuss the
issue
in a more efficient way.
Have a nice day!
Regards,
Jialiang Ge (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.