Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:53:05+00:00 2026-05-29T07:53:05+00:00

I am running a Sql query whose where clause changes everytime when button click

  • 0

I am running a Sql query whose where clause changes everytime when button click occurs.

Now the where clause consists of an index to the list. When the button click happens ,page load occurs which causes the index value intialisation.

int i=0;

protected void Button1_Click(object sender, EventArgs e)
{    
    string str = lst1.ElementAt<string>(i++);
    qr = "Select BdId,First_Name,Last_Name,Age,Gender,Relationship_status,Birthday from [User] where BdId='" + str + "'";
    da2 = new SqlDataAdapter(qr, con);
    da2.Fill(ds2);
    Label1.Text = Label1.Text + "  " + i.ToString();
}

How can I avoid this happening? I want index value to be global so that it doesn’t instantiate again every time button click occurs.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:53:06+00:00Added an answer on May 29, 2026 at 7:53 am

    Every time there is a request for your page, a new instance of that page-class is created to handle that request. So any fields are re-initialized.

    You can store a value in ViewState to remember a value over various requests:

    private int TheIndex
    {
       set { ViewState["TheIndex"] = value; }
       get 
       {
          if (ViewState["TheIndex"] == null)
             return 0;
          return (int)ViewState["TheIndex"];
       }
    }
    

    And use TheIndex instead of i.

    EDIT
    Plus, as CodeInChaos notes, use a more descriptive name. That will help in understanding the code if someone else (or you yourself in a few months) needs to maintain this code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently discovered that a sql query that was running fine earlier is now
using SQL server 2005+ for over a year now, I've been running a query
I'm running SQL query for MySQL server with ... where name in (term1,term2,term3) I
I'm running an SQL query against a mainframe DB2 database using the OdbcDataReader class
the current SQL query works fine locally on MAMP 1.8.4 running MySQL 5.1.37. SELECT
I am running a sql in PHP query that is dieing with the mysql_error()
I have been running SQL Server 2005 Express Management Studio (SSMSE), and I now
I'm in a very, very tight situation here. I have an SQL query running
I am running a sql query to return the largest value in a column
I have a long-running SQL Server 2005 query that I have been hoping to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.