Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
Dear friends,
 
Here, i've used public static string A; variable in my application.
Suppose, user1 hav A="1" and now user2 come to this form and he has A=2.
So, in this case original value of A for user1(which is A=1) replaced with user2(which is now A=2), But i want to keep both of the value saprately for both the users..
 
Note :
1) i used public string A; but during postback it lost its value.
 
i dont want to use session variable for this.
 
sample coding part for reference-->
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Web;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
 
public partial class PO_REG : System.Web.UI.Page
{
   
    public static string A; 
    public static string _print; 
    public void Page_Init(object sender, EventArgs e)
    {
        if (_print == "P")
        {
            ViewReport();
        }
    }
       #endregion
 
    protected void btnShowReport_Click(object sender, ImageClickEventArgs e)
    {
        _print = "P";
        A = Rdbtn.SelectedValue;        
        ViewReport()
    }
 
    private void ViewReport()
    {
        ReportDocument crystalReport = new ReportDocument();
        if (A == "1")
        {
            crystalReport.Load(Server.MapPath("sample1.rpt"));
        }
        else if (A == "2")
        {
            crystalReport.Load(Server.MapPath("sample2.rpt"));
        }
    }
 

any help would be highly appriciated....
thank you.
Posted 4-Nov-12 22:18pm
Edited 4-Nov-12 23:06pm
v7
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Why not to hold the value A in a viewstate and use update panels for asynchronous post backs. it will be much helpful in multi user applications
  Permalink  
Comments
Dev.Vikas.S at 5-Nov-12 5:27am
   
Thanks dear for reply..
but im not using ajax.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why can't you use "Rdbtn.SelectedValue" directly in the ViewReport() routine.

        if (Rdbtn.SelectedValue == "1")
        {
            crystalReport.Load(Server.MapPath("sample1.rpt"));
        }
        else if (Rdbtn.SelectedValue == "2")
        {
            crystalReport.Load(Server.MapPath("sample2.rpt"));
        }
 
Then another thing we can try is using switch Case; which is a better way to increase the performance.
 
[Update]: I didnt noticed you have used public static string _print in the code ; whats it for? Some logical mistake is there; I beleive.
  Permalink  
v3
Comments
Dev.Vikas.S at 5-Nov-12 5:05am
   
Bcoz, when user clicks print button in crystal reportviewer then it will fire Page_init event of page.. so, its value for (Rdbtn.SelectedValue) will not there. it will show you null value only..
Ankur\m/ at 5-Nov-12 5:13am
   
Then another thing we can try is using switch Case; which is a better way to increase the performance
I disagree. For just 2 conditional statements, there won't be any difference. Switch cases are good from readability perspective as well but if there are few conditional statements, if-else is just fine.
Dev.Vikas.S at 5-Nov-12 5:15am
   
yeah.. ri8
Baji Jabbar at 5-Nov-12 5:30am
   
:thumbsup:
For sure Switch Case is better way than if else conditions not only for readability but for performance too. Yeah for few conditional statements we cant notice any difference
Dev.Vikas.S at 5-Nov-12 5:14am
   
if (_print == "P"){ViewReport();}
Well, this is for, i want to run this code only if user have already clicked on the asp:button --> ShowReport, otherwise not. dat is y..
Baji Jabbar at 5-Nov-12 5:27am
   
This approach is not needed.
Dev.Vikas.S at 5-Nov-12 5:33am
   
ok, but that is not a problem.. kindly forget that.
my problem is, value of A overwritten by the value of another user..
Baji Jabbar at 5-Nov-12 5:35am
   
that happens when you use static variable. Did you tried my solution. It will work if you are not loosing the radiobutton state. Try it ( if not ) and give feedback on that.
Dev.Vikas.S at 5-Nov-12 5:49am
   
if i use if(Rdbtn.SelectedValue == "1") directly..
will i get its value during Page_Init??
Answer is not, dat is why i am using variable there.
 
now, if i'll not use Page_Init then if user clicks on print or export button in reportviewer then it is showing me an error "Load report failed.."
Baji Jabbar at 5-Nov-12 5:56am
   
Remove your codes from init block , try SelecteValue or SelecteItem.Value. Hope this works , still I don't know why you need that code in Page_Init.
Dev.Vikas.S at 5-Nov-12 6:00am
   
i did this, report displayed properly for 1st time, but its giving me an error when user tries to print or export reports from crystalreportviewer.
Baji Jabbar at 5-Nov-12 6:03am
   
So it works ! What error you are getting, Did you tried debugging it. From where is the error thrown?
Dev.Vikas.S at 5-Nov-12 6:08am
   
"Load Report Failed.." this error im getting.
but its giving me an error when user tries to print or export reports from crystalreportviewer.becoz page_init is not there..
Now, when i put above page_init code then it is working fine except multiuser problem..
 
is there any way, i can use to avoid this but i dont want to use session..
Ankur\m/ at 5-Nov-12 5:25am
   
Then another thing we can try is using switch Case; which is a better way to increase the performance
I disagree. For just 2 conditional statements, there won't be any difference. Switch cases are good from readability perspective as well but if there are few conditional statements, if-else is just fine.
PS: Sorry I meant to post the comment to the answerer. Re-posting it.
Baji Jabbar at 5-Nov-12 5:32am
   
I replied to your original comment. Thanks Ankur for correcting me .
Cheers :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
 
Correct me if i am wrong.Did you try usng hidden field?
      <asp:HiddenField ID="hdnValue" runat="server" Value="Secret Value" />
 
In that way you can make your variable as public string A.
Assign the hidden variable while assigning the string variable.
On Postback reassign the string variable using the hidden variable.
 
Hope this helps.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 395
1 DamithSL 218
2 Sergey Alexandrovich Kryukov 215
3 Dave Kreskowiak 200
4 Maciej Los 195


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid