Solved

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

Posted on 2014-02-14
5
1,597 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
ID: 39859394
Would need to see the code.
0
 
LVL 2

Author Comment

by:Steven O'Neill
ID: 39859499
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
ID: 39861460
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
ID: 39864774
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
ID: 39891395
I managed to resolve this issue myself with the code used...might not be correct but works for my site
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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