[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c#.net public variable causing timeout

Posted on 2009-04-28
4
Medium Priority
?
569 Views
Last Modified: 2013-12-17
Hello everyone! -Please bear with me and even my approach. I'm very new to asp.net and c#.net.

-i plan on expanding this concept much much further, but have gotten stuck on something, so provide the simplest of examples.

-Ok, I've learned/worked through some stuff so far and can even get 'most' of the functionality i need. I would prefer however to use a public variable/property {get/set} to get and set the values (as they may be set from various controls, and possibly even called upon from various events.

-I have 2 formviews, 2 object data sources.
The first form view fv_EmpMain
second one fv_EmpName

I will supply applicable code below.


Basically, I want to use the public variable but when i replace what will work with references to the global variable the page won't load!? I've isolated it to specifically when i call on (either in or out set/get) of the global variable, the page just won't load. i know this should be simple, but i'm stumped.

Many thanks in advance.
-John



//The following code will work fine, I need to do some other stuff, like onpagingchanged, etc. but this will work for basic querying of info:
 
    <asp:FormView ID="fv_EmpMain" runat="server" AllowPaging="True" CellPadding="4" 
        DataKeyNames="employee_no" DataSourceID="ods_EmpMain" ForeColor="#333333" >
<!--
     Fields, etc.
-->
   </asp:FormView >
   <asp:FormView ID="fv_EmpName" runat="server" DataKeyNames="EMP_NAME_NO" 
        DataSourceID="ods_EmpName" AllowPaging="True" CellPadding="4" 
        ForeColor="#333333" >
<!--
     Fields, etc.
-->
   </asp:FormView>
 
    <asp:ObjectDataSource ID="ods_EmpMain" runat="server" 
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetMainData" 
        TypeName="EmployeeTableAdapters.EMPLOYEE_MAINTableAdapter" >
        <SelectParameters>
            <asp:FormParameter DefaultValue="0" FormField="txtEmpNo" 
                Name="EmpMainNo" Type="Int32" />
            <asp:FormParameter DefaultValue=" " FormField="txtEmpLNme" Name="EmpLNme" 
                Type="String" />
            <asp:FormParameter DefaultValue=" " FormField="txtEmpFNme" Name="EmpFNme" 
                Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>
 
    <asp:ObjectDataSource ID="ods_EmpName" runat="server" DeleteMethod="Delete" 
        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetEmpNameData"
        TypeName="EmployeeTableAdapters.EMPLOYEE_NAMETableAdapter" 
        UpdateMethod="Update" OnSelecting="odsEmpMain_OnSelecting" >
        <DeleteParameters>
            <asp:Parameter Name="Original_EMP_NAME_NO" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="EMP_NO" Type="Int32" />
            <asp:Parameter Name="FIRST_NAME" Type="String" />
            <asp:Parameter Name="MIDDLE_NAME" Type="String" />
            <asp:Parameter Name="LAST_NAME" Type="String" />
            <asp:Parameter Name="SUFFIX" Type="String" />
            <asp:Parameter Name="EFFECTIVE_DTE" Type="DateTime" />
            <asp:Parameter Name="EXPIRATION_DTE" Type="DateTime" />
            <asp:Parameter Name="Original_EMP_NAME_NO" Type="Int32" />
        </UpdateParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="txtEmpNo" DefaultValue="0" Name="EmpMainNo" 
                PropertyName="Text" Type="Int32" />
        </SelectParameters>
        <InsertParameters>
            <asp:Parameter Name="EMP_NO" Type="Int32" />
            <asp:Parameter Name="FIRST_NAME" Type="String" />
            <asp:Parameter Name="MIDDLE_NAME" Type="String" />
            <asp:Parameter Name="LAST_NAME" Type="String" />
            <asp:Parameter Name="SUFFIX" Type="String" />
            <asp:Parameter Name="EFFECTIVE_DTE" Type="DateTime" />
            <asp:Parameter Name="EXPIRATION_DTE" Type="DateTime" />
        </InsertParameters>
    </asp:ObjectDataSource>
 
 
 
//My Code Behind--- relevant sections
// This will work
 
    protected void odsEmpMain_OnSelecting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        string MainEmpNo = ((Label)fv_EmpMain.FindControl("employee_noLabel")).Text;
 
        if (MainEmpNo != string.Empty)
        {
            e.InputParameters["EmpMainNo"] = Convert.ToInt32(MainEmpNo);
        }
    }
 
 
 
//What i'd like to do is via a fv_EmpMain.OnDataBound Event, set public vaiable and call via the same //ods_EmpMain.OnSelecting Event as above like this:
//--Page never loads, just sits and timesout, i've isolated to use of this public variable.--
 
    static class MyGlobals
    {
 
        public static string gMainEmpNo
        {
          get
            {
            return gMainEmpNo;
            }
          set
            {
            gMainEmpNo = value;
            }
        }
    }
 
 
   protected void fvEmpMain_OnDataBound(object sender, EventArgs e)
    {
        string MainEmpNo = ((Label)fv_EmpMain.FindControl("employee_noLabel")).Text;
 
        if (MainEmpNo != string.Empty)
        {
            MyGlobals.gMainEmpNo = MainEmpNo;
        }
 
    }
 
    protected void odsEmpMain_OnSelecting(object sender, ObjectDataSourceMethodEventArgs e)
    {
 
            e.InputParameters["EmpMainNo"] = Convert.ToInt32(MyGlobals.gMainEmpNo);
 
    }

Open in new window

0
Comment
Question by:JohnSingleton
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
Gorkem Yuksel earned 2000 total points
ID: 24251939
Hi John,

The issue is that you have your variable name set as the same name as your property

gMainEmpNo is the property name.. if you are going to use a property get/set coding, you need to have a private static varaible named something other than the property name itself..

private static string prv_gMainEmpNo = "";

public static string gMaingEmpNo
{
     get { return prv_gMainEmpNo; }
     set { prv_gMainEmpNo = value; }
}

Now.. here is the other part.. If you aren't actually doing anything with these passed values other than just setting and retrieving them, it may be just easier to replace your get/set function with a straight variable declaration:

public static string gMainEmpNo = "";

and you can use this to set a value and get it afterwards...

Cheers,

G.
0
 
LVL 5

Author Comment

by:JohnSingleton
ID: 24252079
                                                              5


gyuksel,,, high five.                                                         I started down this path per some tutorials i'd seen, but apperently i didn't have the 'static' on the private vairable, which cause it to now work. you're configuration work perfectly after i re-pointed things to the way i'd like them.....


-regarding straight variable declaration,,, i've read of this but think that in the interest of flexibility moving forward will leave this.  it's my understaning that if i'm only using it for I/O and no data validation, etc. i don't need to do this?
correct me if i'm wrong.  I was also under the impresssion that this was the defacto standard and being new to the language, wanted to do things as right as i could, but making the jump from procedural programming straight into c#.net/ asp.net is a large pill to swallow, but again, i can't thank you enough!

-John
0
 
LVL 5

Author Closing Comment

by:JohnSingleton
ID: 31575518
gyuksel,
Can't thank you enough. perfect response to my imperfect construction of the question. literaly took 1 minute to convert code and be on my way.
many thanks again.
-John
0
 
LVL 9

Expert Comment

by:Gorkem Yuksel
ID: 24252219
Hi John,

My pleasure! :o)

usually, one would use the get/set method of setting variables if you intend to convert or modify the value in some way while setting and/or retrieving the value.  Otherwise, it is always a safe bet to use straight variable declaration.

Less code = less places where things can go wrong.

Cheers,

G.
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses

831 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