Click here to Skip to main content
15,914,209 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a public function that gets called from another class. As such the function needs to be shared in order for it to be called. However, when I make it shared, the connection string throws the exception Cannot access non-shared function GetConnection() in shared context. The function is ...

VB
Public Shared Function GetUsernames() As Object
     Dim cmdUsernames As SqlCommand = New SqlCommand("crc_GetUsernameForAccountNoChangesMadeReport", CType(GetConnection(), SqlConnection))
     Dim user As String

     cmdUsernames.CommandType = CommandType.StoredProcedure
     With cmdUsernames
         .Connection.Open()
         Dim results As New List(Of Usernames)()
         Using reader As SqlDataReader = .ExecuteReader()
             While reader.Read()
                 user = reader.GetString(0)
                 Dim item As New Usernames() With {.Name = user}
                 results.Add(item)
             End While
             reader.Close()
             .Connection.Close()
             Return results
         End Using
     End With
 End Function



This function gets called from a form with the following code...
VB
Private Sub frmAcctNoChgsMadeFilters_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     Dim Users As List(Of Usernames) = GetUsernames()
     cboUsername.DataSource = Users
     cboUsername.ValueMember = "Name"
     cboUsername.DisplayMember = "Name"
 End Sub


How can I resolve this error
Posted
Comments
Kenneth Haugland 16-Oct-13 10:30am    
Private Shared Sub frmAcctNoChgsMadeFilters_Load ?
rawilken 16-Oct-13 10:50am    
No.
Sergey Alexandrovich Kryukov 16-Oct-13 15:54pm    
A connection string cannot throw anything, it is just a string...
—SA

It's saying that because your GetConnection() method is not shared and requires an instance of the class containing it to be created first.
 
Share this answer
 
Changed to using controls on Report Viewer accessing return values. Method no longer needed to be shared.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900