• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

c#.net public variable causing timeout

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
JohnSingleton
Asked:
JohnSingleton
  • 2
  • 2
1 Solution
 
Gorkem YukselCommented:
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
 
JohnSingletonAuthor Commented:
                                                              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
 
JohnSingletonAuthor Commented:
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
 
Gorkem YukselCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now