Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Borislav Borissov
  Where is Borislav Borissov?
 Sofia
 Bulgaria
 Borislav Borissov
 To: Santosh Mishra
  Where is Santosh Mishra?
 Lucknow
 India
 Santosh Mishra
 Tags
Subject: RE: .Net Variable problem
Thread ID: 416185 Message ID: 416187 # Views: 62 # Ratings: 2
Version: Unknown Category: C#.NET
Date: Tuesday, December 23, 2014 8:36:49 AM         
   


> At my learning stage I have a prob.
>
> I declared a variable "branch" in Page_Load event
> but when I tried to use it in Button2_Click event, it is giving error.
>
> Please help.....
>
> It would good for me, if help will be in my level
>
>
>
> protected void Page_Load(object sender, EventArgs e)
> {
> string branch= null;
> if (Session["sfld1"].ToString() == "ITOFF")
> {
> branch = "IT";
> Label1.Text = "INDIRA NAGAR BRANCH";
> }
> }
>
> protected void Button2_Click(object sender, EventArgs e)
> {
> SqlConnection con = new SqlConnection("Server=SDCHO\\SQLEXPRESS;Database=LoginPage;Trusted_Connection=true");
> con.Open();
> SqlCommand cmd = new SqlCommand();
>
> cmd.CommandText = "Update LeaveDataTable SET BRANCH_OFF= @branch_off1 where Lsr=@Lsr1 and Lbr=@Lbr1";
>
> cmd.Parameters.AddWithValue("@branch_off1", RadioButtonList2.SelectedItem.Value.ToString());
> cmd.Parameters.AddWithValue("@Lsr1", Convert.ToInt32(TextBox1.Text));
>
> cmd.Parameters.AddWithValue("@Lbr1", branch);
>
> cmd.Connection = con;
> int result = cmd.ExecuteNonQuery();
>
>
> GridView1.DataBind();
> con.Close();
> }


Variable branch is LOCAL (in VFP terms) to Load event, you can not use it everywhere else.
You must declare it as a property of the class.
namespace SomeNameSpace
{
    public partial class YourPageName : YourPageClass
    {
      string branch;

      protected void Page_Load(object sender, EventArgs e)
     {
         if (Session["sfld1"].ToString() == "ITOFF")
         {
             branch = "IT";
             Label1.Text = "INDIRA NAGAR BRANCH";
         }
     }
 ...   

    }
}

-----------------
Borislav Borissov

Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.

ENTIRE THREAD

.Net Variable problem Posted by Santosh Mishra @ 12/23/2014 8:22:55 AM
RE: .Net Variable problem Posted by Borislav Borissov @ 12/23/2014 8:36:49 AM
RE: .Net Variable problem Posted by Zia Mughal @ 12/23/2014 8:51:12 AM
RE: .Net Variable problem Posted by Anil Sharma @ 12/25/2014 7:25:28 PM