Solved

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

Posted on 2014-02-14
5
1,637 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

726 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