以编程方式更改Sqldatasource Select语句不起作用 [英] Programatically Change Sqldatasource Select Statement not working

查看:119
本文介绍了以编程方式更改Sqldatasource Select语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个带有sqldatasource的telerik radGrid,它可以显示按酒店城市显示的所有酒店分支机构管理员的列表.(一个酒店城市具有不同的酒店分支机构)

因此,根据登录的管理员,sqldatasource应该加载正确的数据.

实际上我有3种od管理员:

1位超级管理员:登录后,网格将分散所有城市的酒店管理员及其分支机构的管理员(实际上这项工作终于完成了)

2城市酒店管理员:登录后,网格显示正确的结果.
问题是,当我单击网格的编辑命令时,sqldatasource加载了错误的数据(它显示sqldatasource的结果,默认情况下该结果是正确的,而不是正确的情况).


3家酒店管理员:与城市酒店管理员相同的问题:

谢谢您的帮助.

*** asp.net ***
1-网格:

Hello,

i have a telerik radGrid that have sqldatasource which enable it to display the list of all hotel branches admins by hotel city.(a hotel city has different branches of hotels)

so depending of the administrator logged on, the sqldatasource should load the right data.

actually i have 3 kinds od admin:

1-superadmin: when he is logged on, the grid dispaly all cities hotel admin and their branches admin (actually this work finally)

2-city hotel admin : when he is logged on, the grid display the right results .
the problem is that when i click on the edit command of the grid, sqldatasource load the wrong data (it show the result of sqldatasource which is by default and not which is in the right case)


3-branch hotel admin: same problem of city hotel admin:

thank you for your help.

***asp.net***
1-the grid:

    <telerik:RadGrid ID="gvS"  runat="server" DataSourceID="viewAdminCityBranchWebDetails" 

     GridLines="None" AllowPaging="True" AllowSorting="True" 

            Width="99%" AutoGenerateColumns="False"

            ShowStatusBar="True" OnPreRender="RadGrid1_PreRender" 

            OnNeedDataSource="RadGrid1_NeedDataSource" 

            OnUpdateCommand="RadGrid1_UpdateCommand" 

            OnInsertCommand="RadGrid1_InsertCommand" 

            OnDeleteCommand="RadGrid1_DeleteCommand" Skin="Black" 

        ShowGroupPanel="True"  önitemcommand="gvS_ItemCommand">
            
        
<HeaderContextMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</HeaderContextMenu>

        <AlternatingItemStyle BackColor="#CCCCCC" ForeColor="Black" />

        <ItemStyle ForeColor="White" />

<MasterTableView autogeneratecolumns="False" datasourceid="viewAdminCityBranchWebDetails"

EditMode="PopUp" commanditemdisplay="Top" >
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>

<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<GroupByExpressions>
                    <telerik:GridGroupByExpression>
                        <SelectFields>
                            <telerik:GridGroupByField FieldAlias="City" FieldName="City" HeaderText="Hotel City"></telerik:GridGroupByField>
                        </SelectFields>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="City"></telerik:GridGroupByField>
                        </GroupByFields>
                    </telerik:GridGroupByExpression>

                    <telerik:GridGroupByExpression>
                        <SelectFields>
                            <telerik:GridGroupByField FieldAlias="Branch" FieldName="Branch" HeaderText="Hotel Branchs"></telerik:GridGroupByField>
                        </SelectFields>
                        <GroupByFields>
                            <telerik:GridGroupByField FieldName="Branch"></telerik:GridGroupByField>
                        </GroupByFields>
                    </telerik:GridGroupByExpression>
</GroupByExpressions>

    <Columns>
        <telerik:GridBoundColumn DataField="Name" HeaderText="Name" 

            SortExpression="Name" UniqueName="Name">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="loginID" DataType="System.Decimal" 

            HeaderText="loginID" ReadOnly="True" SortExpression="loginID" 

            UniqueName="loginID" Visible="false">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="userName" HeaderText="User Name" 

            SortExpression="userName" UniqueName="userName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="password" HeaderText="Password" 

            SortExpression="password" UniqueName="password">
        </telerik:GridBoundColumn>
         <telerik:GridBoundColumn DataField="userTypeID" HeaderText="userType ID" 

            SortExpression="userTypeID" UniqueName="userTypeID" Visible="false">
        </telerik:GridBoundColumn>
         <telerik:GridBoundColumn DataField="userType" HeaderText="User Type" 

            SortExpression="userType" UniqueName="userType">
        </telerik:GridBoundColumn>

         <telerik:GridBoundColumn DataField="cityID" HeaderText="City ID" 

            SortExpression="cityID" UniqueName="cityID" Visible="false">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="city" HeaderText="City" 

            SortExpression="city" UniqueName="city">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="branchID" HeaderText="Branch ID" 

            SortExpression="branchID" UniqueName="branchID" Visible="false">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="branch" HeaderText="Branch" 

            SortExpression="branch" UniqueName="branch">
        </telerik:GridBoundColumn>

        <telerik:GridBoundColumn DataField="createdDate" DataType="System.DateTime" 

            HeaderText="Created Date" SortExpression="createdDate" UniqueName="createdDate">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="updatedDate" DataType="System.DateTime" 

            HeaderText="Updated Date" SortExpression="updatedDate" UniqueName="updatedDate" DataFormatString="{0:MM/dd/yyyy}" >
        </telerik:GridBoundColumn>
          <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                    <ItemStyle Font-Italic="True" Font-Underline="True" ForeColor="#CC3300" />
                    </telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn CommandName="find" HeaderText="" 

            Text="Delete" UniqueName="column">
                        <ItemStyle Font-Italic="True" Font-Underline="True" ForeColor="#CC3300" />
        </telerik:GridButtonColumn>   
    </Columns>
     <EditFormSettings UserControlName="addWebAdminCityBranchForm.ascx" EditFormType="WebUserControl" CaptionFormatString=" Manage Admin Form" FormStyle-Font-Bold=true >

<FormStyle Font-Bold="True"></FormStyle>

     <PopUpSettings Width="70%" />
                    <EditColumn UniqueName="EditCommandColumn1">
                    </EditColumn>
                </EditFormSettings>
    
</MasterTableView>


                
        <ClientSettings AllowDragToGroup="True">
        </ClientSettings>


                
<FilterMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</FilterMenu>
    </telerik:RadGrid>


2-Sql数据源


2-Sql data source

<asp:SqlDataSource ID="viewAdminCityBranchWebDetails" runat="server" 

        ConnectionString="<%$ ConnectionStrings:SmartBookingEngineConn %>" 

        SelectCommand="SELECT [loginID], [Name], [userName], [password],[CityID],[city],[branchID],[Branch],[userTypeID], [userType], [createdDate],[updatedDate] FROM [ViewCityBranchWebAdmin]"

        > </asp:SqlDataSource>


*** C#***


*** C# ***

public string UserName;
        public string UserType;
        protected void Page_Load(object sender, EventArgs e)
        {
            rp.AjaxSettings.AddAjaxSetting(gvS, gvS);
            UserName = Session["uname"].ToString();
            UserType = Session["userType"].ToString();
            if (Session["uname"] != null)
            {
                txtuser.Text = UserType;
                if (!IsPostBack)
                {
                    if (UserType == "SuperAdmin")
                    {
                        viewAdminCityBranchWebDetails.SelectCommand = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin";
                        viewAdminCityBranchWebDetails.DataBind();

                    }
                    else if (UserType == "CityAdmin")
                    {
                        txtcityID.Text = Session["cityID"].ToString();

                        viewAdminCityBranchWebDetails.SelectCommand = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin Where CityID=@cityID";
                        viewAdminCityBranchWebDetails.SelectParameters.Add("cityID", txtcityID.Text.ToString());
                        viewAdminCityBranchWebDetails.DataBind();
                    }
                    //branches Admin

                    else if (UserType == "Super Admin")
                    {
                        txtcityID.Text = Session["cityID"].ToString();
                        txtbranchID.Text = Session["branchID"].ToString();

                        viewAdminCityBranchWebDetails.SelectCommand = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin Where CityID=@cityID and branchID=@branchID";
                        viewAdminCityBranchWebDetails.SelectParameters.Add("cityID", txtcityID.Text.ToString());
                        viewAdminCityBranchWebDetails.SelectParameters.Add("branchID", txtbranchID.Text.ToString());
                        viewAdminCityBranchWebDetails.DataBind();
                    }

                }
            }
                
            else
            {
                Response.Redirect("frmlogin1.aspx");
            }
                
        }

推荐答案

ConnectionStrings:SmartBookingEngineConn%>" span> SelectCommand =" 选择[loginID],[Name],[userName],[password],[CityID],[city],[branchID],[Branch],[userTypeID],[userType],[createdDate ],[updatedDate] FROM [ViewCityBranchWebAdmin]" span> > < /asp:SqlDataSource >
ConnectionStrings:SmartBookingEngineConn %>" SelectCommand="SELECT [loginID], [Name], [userName], [password],[CityID],[city],[branchID],[Branch],[userTypeID], [userType], [createdDate],[updatedDate] FROM [ViewCityBranchWebAdmin]" > </asp:SqlDataSource>


*** C#***


*** C# ***

public string UserName;
        public string UserType;
        protected void Page_Load(object sender, EventArgs e)
        {
            rp.AjaxSettings.AddAjaxSetting(gvS, gvS);
            UserName = Session["uname"].ToString();
            UserType = Session["userType"].ToString();
            if (Session["uname"] != null)
            {
                txtuser.Text = UserType;
                if (!IsPostBack)
                {
                    if (UserType == "SuperAdmin")
                    {
                        viewAdminCityBranchWebDetails.SelectCommand = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin";
                        viewAdminCityBranchWebDetails.DataBind();

                    }
                    else if (UserType == "CityAdmin")
                    {
                        txtcityID.Text = Session["cityID"].ToString();

                        viewAdminCityBranchWebDetails.SelectCommand = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin Where CityID=@cityID";
                        viewAdminCityBranchWebDetails.SelectParameters.Add("cityID", txtcityID.Text.ToString());
                        viewAdminCityBranchWebDetails.DataBind();
                    }
                    //branches Admin

                    else if (UserType == "Super Admin")
                    {
                        txtcityID.Text = Session["cityID"].ToString();
                        txtbranchID.Text = Session["branchID"].ToString();

                        viewAdminCityBranchWebDetails.SelectCommand = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin Where CityID=@cityID and branchID=@branchID";
                        viewAdminCityBranchWebDetails.SelectParameters.Add("cityID", txtcityID.Text.ToString());
                        viewAdminCityBranchWebDetails.SelectParameters.Add("branchID", txtbranchID.Text.ToString());
                        viewAdminCityBranchWebDetails.DataBind();
                    }

                }
            }
                
            else
            {
                Response.Redirect("frmlogin1.aspx");
            }
                
        }


Actuallay问题出在select命令本身,所以这是解决方案:

***页面加载背后的C#代码****

Actuallay the problem was in the select command itself, so this is the solution:

***C# code behind page load****

protected void Page_Load(object sender, EventArgs e)
      {
          rp.AjaxSettings.AddAjaxSetting(gvS, gvS);
          UserName = Session["uname"].ToString();
          UserType = Session["userType"].ToString();
          if (Session["uname"] != null)
          {
              string sql;
              txtuser.Text = UserType;
          
             if (UserType == "SuperAdmin")
                  {
                      sql = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin ";
                      viewAdminCityBranchWebDetails.SelectCommand = sql;
                      gvS.DataBind();

                  }
                  else if (UserType == "CityAdmin")
                  {
                      txtcityID.Text = Session["cityID"].ToString();

                      sql = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin Where CityID='" + txtcityID.Text.ToString() + "'";
                      viewAdminCityBranchWebDetails.SelectCommand = sql;
                      gvS.DataBind();

                  }
                  //branches Admin

                  else if (UserType == "Super Admin")
                  {
                      txtcityID.Text = Session["cityID"].ToString();
                      txtbranchID.Text = Session["branchID"].ToString();

                      sql = "SELECT loginID,Name,userName,password,CityID,city,branchID,Branch,userTypeID,userType,createdDate, updatedDate FROM ViewCityBranchWebAdmin Where CityID='" + txtcityID.Text.ToString() + "'and branchID='" + txtbranchID.Text.ToString() + "'";
                      viewAdminCityBranchWebDetails.SelectCommand = sql;
                      gvS.DataBind();
                  }

              }
        
      }


这篇关于以编程方式更改Sqldatasource Select语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆