Dynamic SelectCommand that Changes View based on DDL - asp.net

Here's a dropdown list I have...
<asp:DropDownList
ID="selectTimeFrame"
runat="server"
AutoPostBack="true"
DataTextField="Increment"
DataValueField="Increment"
DataSourceID="SqlTimeFrame"
</asp:DropDownList>
And its datasource:
<asp:SqlDataSource
ID="SqlTimeFrame"
runat="server"
ConnectionString="<% connectionstring %>"
SelectCommand="Select [IncrementID], [Increment] FROM [TimeFrame] ORDER BY [IncrementID]" >
</asp:SqlDataSource>
and then I have a gridview, whos datasource looks like:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<% connectinstring %>"
ProviderName="<% connectionstring %>"
SelectCommand="SELECT * FROM #TimeFrame">
<SelectParameters>
<asp:ControlParameter ControlID="selectTimeFrame"
Name="TimeFrame"
PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
And obviously the place where I'm having problems is the fact that " FROM #TimeFrame " doesn't do what I want to. I have Different views whose names correspond to different timeframes, and I want to be able to change the gridview to populate with that information based off of the option a user selects via the dropdown menu. Any insight would be much appreciated... THANKS!! :D

You could use dynamic SQL to achieve what you're looking for although you would need to test this very thorughly to prevent SQL injection attacks as we can never trust the input being received from users.
I've created a simple stored proc which checks whether the table exists in the db and if so it constructs and executes your dynamic SQL statement:
Stored procedure:
CREATE PROCEDURE dbo.GetData
#TableName NVARCHAR(200)
AS
BEGIN
IF OBJECT_ID(#TableName , N'U') IS NOT NULL
BEGIN
EXEC('SELECT * FROM ' + #TableName);
END
END
ASPX:
<asp:DropDownList ID="selectTimeFrame" runat="server" AutoPostBack="true" DataTextField="Increment"
DataValueField="Increment" DataSourceID="SqlTimeFrame" />
<asp:SqlDataSource ID="SqlTimeFrame" runat="server" ConnectionString="<%$ ConnectionStrings:connectionstring %>"
SelectCommand="Select [IncrementID], [Increment] FROM [TimeFrame] ORDER BY [IncrementID]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="dynamicDS" runat="server" ConnectionString="<%$ ConnectionStrings:connectionstring %>"
SelectCommand="GetData" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="selectTimeFrame" Name="TableName" PropertyName="SelectedValue"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvData" DataSourceID="dynamicDS" runat="server">
</asp:GridView>

Related

I Can't Show the Total Number of Users with Eval, Repeater, SqlDataSource and Count

I want to find the total number of active users who are members of the user table in my database. But I get an error like this:
DataBinding:'System.Data.DataRowView' does not contain a property
with name 'userid'.
My code is as follows:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<h3><%#Eval("userid") %></h3>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aytasarimConnectionString %>" SelectCommand="SELECT COUNT([userid]) FROM [user] WHERE ([isactive] = #isactive)">
<SelectParameters>
<asp:Parameter DefaultValue="True" Name="isactive" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>
Your select command not including userid field. You can change your command as
SELECT COUNT([userid]) as userid FROM [user] WHERE ([isactive] = #isactive)
then Eval can find this column from your data source.

Update dropDownList using SQL query

I am trying to update my dropdownlistB according to the categoryId chosen in dropdownlistA Using this code:
<asp:DropDownList ID="DropDownListA" runat="server" DataSourceID="SqlDataSourceA" DataTextField="Description" DataValueField="Description" AutoPostBack="True"></asp:DropDownList>
<asp:DropDownList ID="DropDownListB" runat="server" DataSourceID="SqlDataSourceB" DataTextField="Title" DataValueField="Title"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceA" runat="server" ConnectionString="<%$ ConnectionStrings:MainDbConnectionString1 %>" SelectCommand="SELECT [Description] FROM [BookCategory]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceB" runat="server" ConnectionString="<%$ ConnectionStrings:MainDbConnectionString1 %>" SelectCommand="SELECT [Title] FROM [BooksInfo] WHERE ([CategoryId] = #CId)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="" Name="CId" QueryStringField="SELECT [CategoryId] FROM [BookCategory]" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
I am new to using SQL and queries in ASP.NET and cant figure out what Im doing wrong, dropdownlistB stays empty. (AutoPostBack = true in dropdownlistA, so it should update?)
I think what you are looking for is the asp:ControlParameter like this
<asp:ControlParameter ControlID="DropDownListA" PropertyName="SelectedValue"
Name="EmpID" Type="Int32" DefaultValue="0" />
So the the query is based on the selection of DropDownListA.

Data not being populated in dropdown using sqlDataSource

I need to populate the data from database in Dropdown using sqlDataSource. The SqlDataSource is using a querystring. The data is not being populated in dropdown. Can you please suggest what I am doing wrong here?
Code for Dropdown:
<ajaxToolkit:ComboBox ID="SelectDropDown1" runat="server" DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend" AppendDataBoundItems="true" Width="200px" Height="16pt"
Font-Size="8pt" DataSourceID="SqlDataSource1" DataTextField="Rubric"
DataValueField="Rubric">
<asp:ListItem Value="all">All</asp:ListItem>
</ajaxToolkit:ComboBox>
Code for SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Education_Data %>"
SelectCommand="SELECT DISTINCT [Rubric] FROM [table1] WHERE ([Program] = #Program)">
<SelectParameters>
<asp:QueryStringParameter Name="Program" QueryStringField="Program"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The #program needs to be set somehow.
This MSDN article will show you how to do it. Go to section "Passing Parameters to SQL Statements"

Dropdown List appears empty when using #parameter

I am trying to make a dropdownlist work for me. A user can be in many 'Field1's, so I wanted the dropdown to show those Field1s.
Currently what happens is I just get a blank list. Here is a sample of the code:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT DISTINCT Field1 FROM table
WHERE (Field3= #Field3)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
If I remove the select parameters and the WHERE clause the list of all DHBs appears - it just seems to have issues with #Field3.
Edit: I have tested the query in SQL Server and it works for me (by replacing #Field3 with a known value)
Any suggestions? I am very new to asp.net, and have next to nothing in the .cs file (besides some authorisation and such), so if people could point me in the right direction that would be fantastic.
Edit 2: It would probably help if I gave you the whole template:
<asp:TemplateField HeaderText="Field1" SortExpression="Field2">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Field2") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Field1" DataValueField="Field1">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="Field1 FROM table
WHERE (Field3 = #Field3)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
You're populating another control using Dropdownlist selectedValue as a parameter, that is dictated by your code. In this case
You're missing property in the controlParameter
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Property="SelectedValue" Name="Field3" Type="String" />
</SelectParameters>
If you're trying to populate DropDownList1 you're completely derailed. In this case you need to show up more markup code and details.
Update: You cannot take the parameters value from the DropDownList control which you're populating. Either remove the parameter or use another control to provide the value

passing querystring

Consider a page, when the page loads, nothing shows up.
It works when i pass the querystring on the browser as this:
http://localhost:51765/foo/foo.aspx?ID=c516f4f4-36a9-40a7-baad-d2419ea631b9
I want it to work when the page load not when i pass the querystring on the browser.
Can someone help me with this?
<asp:SqlDataSource ID="categoriesDataSource" runat="server"
connectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [CategoryID], [Name] FROM [Categories] WHERE ([UserId] = #UserId) ORDER BY [Name]">
<SelectParameters>
<asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="categories" runat="server" AutoPostBack="True"
DataSourceID="categoriesDataSource" DataTextField="Name"
AppendDataBoundItems="True" DataValueField="CategoryID">
<asp:ListItem Value="">-- All Albums --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="picturesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [PictureID], [Title], [UploadedOn] FROM [Pictures] WHERE UserId = #UserId AND
(CategoryID = #CategoryID Or #CategoryID IS NULL) ORDER BY UploadedOn DESC"
CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:ControlParameter ControlID="categories" Name="CategoryID" PropertyName="SelectedValue"/>
<asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="picturesDataSource">
</asp:GridView>
It is difficult to answer your question without showing the code of the page or at least explaining what it does. From the url it seems that the page relies on the ID parameter and tries to parse it to a Guid. You need to test whether the ID parameter is passed and use it only in this case:
string id = Request["ID"];
if (!string.IsNullOrEmpty(id))
{
// The ID parameter has been passed => use its value here
}

Resources