简体   繁体   English

动态sql和sql的多个搜索条件中的问题加入asp.net c#sql server

[英]problem in multiple search criteria with dynamic sql and sql joins asp.net c# sql server

i have a search page with multiple search criteria and a different page show the search result. 我有一个包含多个搜索条件的搜索页面,另一个页面显示了搜索结果。 the problem i am facing is when two or more users(different pc or browser) search on that page the priviously searched results get affected by the newly searched result. 我面临的问题是,当两个或多个用户(不同的PC或浏览器)在该页面上进行搜索时,秘密搜索的结果会受到新搜索结果的影响。 basically the searched result of the privious user get replaced by the searched result of the last user when privious user click on paging or refresh the page. 基本上,当私有用户单击分页或刷新页面时,私有用户的搜索结果将替换为最后一个用户的搜索结果。 i have provided the code of the search page and search result page. 我提供了搜索页面和搜索结果页面的代码。

在此处输入图片说明

//code of the search criteria page
protected void btnRegularSrch_Click(object sender, EventArgs e)
{
            string strCondition = string.Empty;
            string strSql = string.Empty;
            string custGender = string.Empty;
            string custReli = string.Empty;
            string custCaste = string.Empty;
            string custMtonge = string.Empty;
            string custCountry = string.Empty;
            string custAge = string.Empty;
            string custMstate = string.Empty;

            strSql = "select * from tbl_CustomerInfo cust inner join tbl_Relig rel on rel.regid=cust.REGID inner join tbl_Locat loc on loc.regid=cust.REGID inner join tbl_Photos ph on ph.regid=cust.regid and cust.status=1 ";

            //gender
            if (Male.Checked)
            {
                custGender = "m";
            }
            else
            {
                custGender = "f";
            }
            strCondition += " where cust_gender='" + custGender + "'";

            //age
            if (drp_age_from.SelectedIndex > 0)
            {
                custAge = drp_age_from.SelectedItem.Text;

                if (!string.IsNullOrEmpty(strCondition))
                    strCondition += "and cust.cust_age between '" + drp_age_from.SelectedItem.Text + "' and '" + drp_age_to.SelectedItem.Text + "'";
                else
                    strCondition += "where cust.cust_age between '" + drp_age_from.SelectedItem.Text + "' and '" + drp_age_to.SelectedItem.Text + "'";
            }

            //religion
            if (ddlSearchReli.SelectedIndex > 0)
            {
                custReli = ddlSearchReli.SelectedItem.Text;

                if (!string.IsNullOrEmpty(strCondition))
                    strCondition += " and rel.Religion='" + custReli + "'";
                else
                    strCondition += " where rel.Religion='" + custReli + "'";
            }

            //caste
            if (DdlCaste2.SelectedIndex > 0)
            {
                custCaste = DdlCaste2.SelectedItem.Text;

                if (!string.IsNullOrEmpty(strCondition))
                    strCondition += " and rel.Caste='" + custCaste + "'";
                else
                    strCondition += " where rel.Caste='" + custCaste + "'";
            }
            //mothertonge
            if (ddlCommunty.SelectedIndex > 0)
            {
                custMtonge = ddlCommunty.SelectedItem.Text;

                if (!string.IsNullOrEmpty(strCondition))
                    strCondition += " and rel.MotherTongue='" + custMtonge + "'";
                else
                    strCondition += " where rel.MotherTongue='" + custMtonge + "'";
            }
            //country
            if (drp_country.SelectedIndex > 0)
            {
                custCountry = drp_country.SelectedItem.Text;

                if (!string.IsNullOrEmpty(strCondition))
                    strCondition += " and loc.Country='" + custCountry + "'";
                else
                    strCondition += " where loc.Country='" + custCountry + "'";
            }

            //marital status
            if (ddlMStatus.SelectedIndex > 0)
            {
                strCondition = strCondition + " and (";

                custMstate = ddlMStatus.SelectedItem.Text;

                if (!string.IsNullOrEmpty(strCondition))
                    strCondition += " cust.maritalstatus='" + custMstate + "'";
                else
                    strCondition += " where cust.maritalstatus='" + custMstate + "'";

                //strCondition = strCondition.TrimEnd(MyChar);
                strCondition = strCondition + ")";
            }

            strSql = strSql + strCondition;
            Response.Redirect("SearchResult.aspx?condition=" + Server.UrlEncode(Encrypt(strSql)));
        }



//code of the search result page
string conn = ConfigurationManager.ConnectionStrings["con_str"].ConnectionString;
       Utility objUtil = new Utility();
        static string Condition;
        #region Private Properties
        private int CurrentPage
        {
            get
            {
                object objPage = ViewState["_CurrentPage"];
                int _CurrentPage = 0;
                if (objPage == null)
                {
                    _CurrentPage = 0;
                }
                else
                {
                    _CurrentPage = (int)objPage;
                }
                return _CurrentPage;
            }
            set { ViewState["_CurrentPage"] = value; }
        }
        private int fistIndex
        {
            get
            {

                int _FirstIndex = 0;
                if (ViewState["_FirstIndex"] == null)
                {
                    _FirstIndex = 0;
                }
                else
                {
                    _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
                }
                return _FirstIndex;
            }
            set { ViewState["_FirstIndex"] = value; }
        }
        private int lastIndex
        {
            get
            {

                int _LastIndex = 0;
                if (ViewState["_LastIndex"] == null)
                {
                    _LastIndex = 0;
                }
                else
                {
                    _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
                }
                return _LastIndex;
            }
            set { ViewState["_LastIndex"] = value; }
        }
        #endregion

        #region PagedDataSource
        PagedDataSource _PageDataSource = new PagedDataSource();
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               if (Request.QueryString["condition"] != null)
                {
                    Condition = objUtil.Decrypt(Request.QueryString["condition"]);

                    this.BindItemsList();
                }

            }
        }
        protected DataTable getDataTable()
        {
            DataUtility objUtil = new DataUtility();
            return objUtil.getDataTable(Condition);
            //rptViewBasicInfo.DataSource = dt;
            //rptViewBasicInfo.DataBind();
        }
        private void BindItemsList()
        {

            _PageDataSource.DataSource = this.getDataTable().DefaultView;
            _PageDataSource.AllowPaging = true;
            _PageDataSource.PageSize = 10;
            _PageDataSource.CurrentPageIndex = CurrentPage;
            ViewState["TotalPages"] = _PageDataSource.PageCount;

            _PageDataSource.PageCount;
            this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
            this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;


            Repeater1.DataSource = _PageDataSource;
            Repeater1.DataBind();
            this.doPaging();
        }

        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");

            fistIndex = CurrentPage - 5;

            if (CurrentPage > 5)
            {
                lastIndex = CurrentPage + 5;
            }
            else
            {
                lastIndex = 10;
            }
            if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
            {
                lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
                fistIndex = lastIndex - 10;
            }

            if (fistIndex < 0)
            {
                fistIndex = 0;
            }

            for (int i = fistIndex; i < lastIndex; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }

            this.dlPaging.DataSource = dt;
            this.dlPaging.DataBind();
        }
        //#endregion
        protected void lbtnPrevious_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            this.BindItemsList();
        }
        protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("Paging"))
            {
                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
                this.BindItemsList();
            }
        }
        protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                lnkbtnPage.Style.Add("fone-size", "14px");
                lnkbtnPage.Font.Bold = true;
            }
        }
        protected void lbtnLast_Click(object sender, EventArgs e)
        {
            CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
            this.BindItemsList();
        }
        protected void lbtnFirst_Click(object sender, EventArgs e)
        {

            CurrentPage = 0;
            this.BindItemsList();
        }
        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            this.BindItemsList();
        }

On search result page, you declared the Condition variable to be static, so it behaves like a global variable and every time a user reached search result page, changes the value of Condition variable for all users. 在搜索结果页面上,您将Condition变量声明为静态变量,因此它的行为类似于全局变量,并且每次用户访问搜索结果页面时,都会为所有用户更改Condition变量的值。

Change this; 改变这个;

static string Condition;

To this; 对此

string Condition;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM