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
Solved

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

Posted on 2014-02-14
5
1,572 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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