We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Store DropDownList selection in session for use as scalar in SQL query on postback.

Hi,

I have a Drop Down List that is populated from a remote SQL database via a SqlDatasource control. The Drop Down List has an automatic selection which is used to populate 3 charts with data on page load. I want the user to be able to change the selection in the Drop Down List, and to store this as a session variable so that I can use it in codebehind as a scalar for the SQL query to repopulate the chart with the user's selection. Is this possible or is there a better way to populate the charts using the user's selection?

I have attached my ASP file and C# file below.

Thanks,

Alex

Attachment: PlanningAnalysis_3f2fc2d1.zip

1 Reply

CI Christopher Issac Sunder K Syncfusion Team April 26, 2019 08:40 AM UTC

Hi Alex,  

Greetings from Syncfusion support. 

We suggest you to store the selected value in a session variable through “OnValueChange” event of DropDownList and use this in SQL query based on your requirement. Please refer to the below given code 

   <ej:DropDownList runat="server" ID="TradesDropDown" DataTextField="CustomerID" DataValueField="ShipCity" AutoPostBack="true" DataSourceID="SqlData" Width="200px" EnableIncrementalSearch="False" FilterType="Contains" SelectedIndex="0" OnValueSelect="TradesDropDown_ValueSelect"> 
  </ej:DropDownList> 
    <asp:SqlDataSource ID="SqlData" runat="server" ConnectionString="<%$ ConnectionStrings:SQLConnectionString %>" SelectCommand="SELECT * FROM [Orders]"> 
        <SelectParameters> 
    <asp:Parameter Name="ShipCity" Type="String" DefaultValue="Reims" /> 
  </SelectParameters> 
    </asp:SqlDataSource> 
    <ej:Button runat="server" ID="btn" OnClick="btn_Click" Text="Check"></ej:Button> 

Aspx.CS: 

protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString()); 
                dt = new DataTable("Order"); 
                SqlCommand cmd = new SqlCommand(); 
                cmd.Connection = myConnection; 
                cmd.CommandText = "select * from Orders"; 
                cmd.CommandType = CommandType.Text; 
                SqlDataAdapter da = new SqlDataAdapter(); 
                da.SelectCommand = cmd; 
                if ((myConnection.State == ConnectionState.Closed)) 
                { 
                    myConnection.Open(); 
                } 
 
                da.Fill(dt); 
                Session["SqlDataSource"] = dt; 
            } 
        } 
 
        protected void TradesDropDown_ValueSelect(object sender, Syncfusion.JavaScript.Web.DropdownListEventArgs e) 
        { 
            Session["Selected"] = e.Value; 
        } 
 
        protected void btn_Click(object Sender, Syncfusion.JavaScript.Web.ButtonEventArgs e) 
        { 
            var value = Session["Selected"]; 
        } 


In our sample, we have just assigned the session variable value to a variable on button click. You can use this in SQL query based on your scenario.  Sample can be downloaded from the below link 


Please let us know if you require any further assistance. 

Thanks, 
Christo 


Loader.
Live Chat Icon For mobile
Up arrow icon