Solved

ASP.NET - variable is not declared. It may be inaccessible due to its protection level

Posted on 2014-02-14
5
1,504 Views
Last Modified: 2014-02-27
Hi there

I've having an issue with a piece of code behind logic where the variable I have is showing the error:

'variablename' is not declared. It may be inaccessible due to its protection level.

I believe the issue is because the area in my file that is referencing the code behind Sub is within a LoggedInTemplate area (as this is a protected area of the site).

Can anyone tell me how I'd go about coding the code behind element to access this LoggedInTemplate section?

Thanx guys
0
Comment
Question by:Steven O'Neill
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Would need to see the code.
0
 
LVL 2

Author Comment

by:Steven O'Neill
Comment Utility
Apologies as I thought this would have been simply coding that I was unaware of (still probably is I guess lol):

Code Behind

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim QS As String = Request.QueryString("ID")
        Dim strConnection As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
        Dim SQL As String = "SELECT * FROM vw_OAS_BusinessDetails WHERE (CRM_BusinessGUID = @CRM_BusinessGUID)"
        Using conn As New SqlConnection(strConnection)
            Dim cmd As New SqlCommand(SQL, conn)
            cmd.Parameters.AddWithValue("@CRM_BusinessGUID", QS)
            conn.Open()
            Dim reader As SqlDataReader = cmd.ExecuteReader()

            'Response.Write(SQL)

            If reader.HasRows Then
                While reader.Read()
                    varCRM_BusinessGUID.Value = reader("CRM_BusinessGUID")
                    varBusinessName.Text = reader("BusinessName")
                    varBusNamePerformance.Text = reader("BusinessName")
                    varBusNamePerf01.Text = reader("BusinessName")
                    varBusNamePerf02.Text = reader("BusinessName")
                    varBusNamePerf03.Text = reader("BusinessName")
                    varBusNamePerf04.Text = reader("BusinessName")
                    varMainContact.Text = reader("MainContact")
                    If (reader("PhoneMain").Equals(DBNull.Value)) Then
                        varMainPhone.Text = String.Empty
                    Else
                        varMainPhone.Text = reader("PhoneMain")
                    End If
                    ' Check to see if Mobile Phone has data
                    If (reader("PhoneMobile").Equals(DBNull.Value)) Then
                        varMobilePhone.Text = String.Empty
                    Else
                        varMobilePhone.Text = reader("PhoneMobile")
                    End If

                    ' Check to see if E-mail Address has data
                    If (reader("EMailAddress").Equals(DBNull.Value)) Then
                        varEmail.Text = String.Empty
                    Else
                        varEmail.Text = reader("EMailAddress")
                    End If

                    ' Check to see if Address Line 1 has data
                    If (reader("AddLine1").Equals(DBNull.Value)) Then
                        varAddLine1.Text = String.Empty
                    Else
                        varAddLine1.Text = reader("AddLine1")
                    End If

                    ' Check to see if Address Line 2 has data
                    If (reader("AddLine2").Equals(DBNull.Value)) Then
                        varAddLine2.Text = String.Empty
                    Else
                        varAddLine2.Text = reader("AddLine2")
                    End If

                    ' Check to see if Address Line 3 has data
                    If (reader("AddLine3").Equals(DBNull.Value)) Then
                        varAddLine3.Text = String.Empty
                    Else
                        varAddLine3.Text = reader("AddLine3")
                    End If

                    ' Check to see if City has data
                    If (reader("AddCity").Equals(DBNull.Value)) Then
                        varAddCity.Text = String.Empty
                    Else
                        varAddCity.Text = reader("AddCity")
                    End If

                    ' Check to see if Post Code has data
                    If (reader("AddPostCode").Equals(DBNull.Value)) Then
                        varAddPostcode.Text = String.Empty
                    Else
                        varAddPostcode.Text = reader("AddPostCode")
                    End If

                    'Check to see if Loan has data
                    If (reader("MoneyLoan").Equals(DBNull.Value)) Then
                        varMoneyLoan.Text = String.Empty
                    Else
                        varMoneyLoan.Text = reader("MoneyLoan")
                    End If

                    'Check to see if Grant has data
                    If (reader("MoneyGrant").Equals(DBNull.Value)) Then
                        varMoneyGrant.Text = String.Empty
                    Else
                        varMoneyGrant.Text = reader("MoneyGrant")
                    End If

                    ' Check to see if Aftercare Start Date has data
                    If (reader("dteDateAdded").Equals(DBNull.Value)) Then
                        varACStartDate.Text = String.Empty
                    Else
                        varACStartDate.Text = reader("dteDateAdded")
                    End If

                End While
            End If

            reader.Close()
        End Using
    End Sub

Open in new window


Main file:

<asp:LoginView runat="server" ViewStateMode="Disabled">
        <AnonymousTemplate>
            <div class="jumbotron">
                <div class="row">
                    <div class="col-lg-offset-2 col-lg-4">
                        <a href="Account/Register.aspx" class="btn btn-primary btn-large">Register &raquo;</a>
                    </div>
                    <div class="col-lg-offset-2 col-lg-4">
                        <a href="Account/Login.aspx" class="btn btn-primary btn-large">Login &raquo;</a>
                    </div>
                 </div>
            </div>            
        </AnonymousTemplate>
        <LoggedInTemplate>
            <div class="bs-docs-section">
                <div class="row">
                    <div class="bs-example">
                        <div class="alert alert-dismissable alert-warning">
                            <div class="row">
                                <div class="col-md-2">Business Name:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varBusinessName" EnableViewState="false" /></div>
                                <div class="col-md-2">Address Line 1:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varAddLine1" EnableViewState="false" /></div>
                            </div>
                            <div class="row">
                                <div class="col-md-2">Main Contact:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varMainContact" EnableViewState="false" /></div>
                                <div class="col-md-2">Address Line 2:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varAddLine2" EnableViewState="false" /></div>
                            </div>
                            <div class="row">
                                <div class="col-md-2">Main Phone:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varMainPhone" EnableViewState="false" /></div>
                                <div class="col-md-2">Address Line 3:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varAddLine3" EnableViewState="false" /></div>
                            </div>
                            <div class="row">
                                <div class="col-md-2">Mobile Phone:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varMobilePhone" EnableViewState="false" /></div>
                                <div class="col-md-2">City:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varAddCity" EnableViewState="false" /></div>
                            </div>
                            <div class="row">
                                <div class="col-md-2">E-Mail Address:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varEmail" EnableViewState="false" /></div>
                                <div class="col-md-2">Post Code:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varAddPostcode" EnableViewState="false" /></div>
                            </div>
                            <hr />
                            <div class="row">
                                <div class="col-md-2">Total Loan Amount:</div>
                                <div class="col-md-4">£<asp:Literal runat="server" ID="varMoneyLoan" EnableViewState="false" /></div>
                                <div class="col-md-2">Aftercare Start Date:</div>
                                <div class="col-md-4">
                                    <asp:Literal runat="server" ID="varACStartDate" EnableViewState="false" /></div>
                            </div>
                            <div class="row">
                                <div class="col-md-2">Total Loan Amount:</div>
                                <div class="col-md-4">£<asp:Literal runat="server" ID="varMoneyGrant" EnableViewState="false" /></div>
                                <div class="col-md-2"></div>
                                <div class="col-md-4"></div>
                            </div>
                        </div>
                    </div>                </div>

            <% 'GUID = <%: User.Identity.GetUserId() %>



            
            

        </LoggedInTemplate>
    </asp:LoginView>
</asp:Content>

Open in new window


I've stripped out most of my page but left in the relevant areas.

Hope it helps.
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
So which line is the error on and which variable is it complaining about?
0
 
LVL 2

Accepted Solution

by:
Steven O'Neill earned 0 total points
Comment Utility
I've managed to solve the issue myself by referencing the control in my loginview. Here's my code:

Dim CRMBusinessName As Literal = TryCast(VolView.FindControl("varBusinessName"), Literal)
                        If CRMBusinessName IsNot Nothing Then
                            CRMBusinessName.Text = reader("BusinessName")
                        End If

Open in new window

Seems to work for me.

Thanx for your assistance though
0
 
LVL 2

Author Closing Comment

by:Steven O'Neill
Comment Utility
I managed to resolve this issue myself with the code used...might not be correct but works for my site
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now