how to access variable from outside in webmethod [Resolved]

Posted by Klbaiju under ASP.NET on 3/25/2015 | Points: 10 | Views : 7351 | Status : [Member] | Replies : 3
Hi,

In my application iam using webmethod.and i want access value of variable declare outside of the web method. following is my code



[WebMethod]
public static StudentDetails[] GetStudentsNext(string id)
{

BAL obj = new BAL();
List<StudentDetails> details = new List<StudentDetails>();
DataSet ds = obj.DisplayStudents(id);
i = Convert.ToInt32(hdint.Value);
if (i < ds.Tables[0].Rows.Count - 1)
{
}


}


my problem is based on this code

i = Convert.ToInt32(hdint.Value);

here i is an integer declared in page_load event and hdint is a hidden field also declared and store value in page_load event.

my requirement is to access these values in webmethod.

how it is possible.

Regards

Baiju




Responses

Posted by: A2H on: 3/25/2015 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
You can declare a static variable in code behind and then assign the textbox value to static variable. You will be able to access the static variable inside your webmethod.

Sample Code
//Declare a Static Variable
public static string strval { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Assign the hiddenfield value to static variable
strval = hdint.Value;
}

}

[WebMethod]
public static StudentDetails[] GetStudentsNext(string id)
{

BAL obj = new BAL();
List<StudentDetails> details = new List<StudentDetails>();
DataSet ds = obj.DisplayStudents(id);
//Access the static variable value here
i = Convert.ToInt32(strval);

if (i < ds.Tables[0].Rows.Count - 1)
{

}
}


Thanks,
A2H
Please mark my reply as answer if it helps to resolve your problem

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 3/25/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks for your reply.
it is working fine

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/25/2015 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Glad to be of help.

Thanks,
A2H
Please mark my reply as answer if it helps to resolve your problem

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response