Error: BC30369 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

Simflex 301 Reputation points
2022-11-10T02:47:35.247+00:00

Hi experts,

I have followed the suggested solutions on this forum for this issue on the subject line of this thread but none has a solution to helps my situation.

I have the following function with SHARED method:

Public Shared Function GetDetails(ByVal speakerId As String) As String
Dim context As HttpContext = HttpContext.Current
Dim ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable("SpeakersData")
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim sqlCon As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT SpeakerId,SpeakerName,Email FROM Speakers WHERE SpeakerId = @SpeakerId", sqlCon)
cmd.Parameters.AddWithValue("@SpeakerId", If(Not String.IsNullOrEmpty(speakerId), speakerId, CObj(DBNull.Value)))
sqlCon.Open()
exampleWrap.Visible = True
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
sda.Fill(dt)
ds.Tables.Add(dt)
End Using
End Using
Return ds.GetXml()
End Function

When I added the exampleWrap.Visible = True to the function, I get the following:

BC30369 Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.

When I removed the Shared method from the function as recommended by some suggestions in some of the recommended solutions online, I get

"DataTables warning: table id=exampleData - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1"

Without removing the Shared method and the exampleWrap.Visible = True from the function, the code works.

The only problem is that I wanted to hide the no data found message until user clicks the Submit button.

I am stuck. Any ideas how to work around this?

Many thanks in advance.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,342 questions
{count} votes