SqlDataSource-Query in a repeater to use repeater-elements - asp.net

I would like to do the following with asp.net and C#:
An SqlDataSource fetches a list of templates from the database. Then, in a repeater, for each template a DropDownList-element is created, that contains the found types for each template. Is this possible?
<asp:SqlDataSource ID="src_template" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>" SelectCommand="SELECT [template] FROM [template]" />
<asp:Repeater ID="rpt_template" runat="server" DataSourceID="src_template" OnItemCommand="rpt_template_ItemCommand">
<ItemTemplate>
<p>
<asp:SqlDataSource ID="src_types" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>" SelectCommand='SELECT [type] FROM [templatetype] WHERE [template] = <%# Eval("template") %>' />
<label for="DropDownList1"><%# Eval("template") %></label>
<asp:DropDownList ID="DropDownList1" runat="server" DatasourceID="src_types" DataTextField="type" DataValueField="type" />
</p>
</ItemTemplate>
</asp:Repeater>
How do I fill the where-part correctly?

Try something like this:
<ItemTemplate>
<asp:HiddenField ID="template" runat="server"
Value='<%# Eval("template") %>'
/>
<asp:SqlDataSource ID="src_types" runat="server"
ConnectionString="<%$ ConnectionStrings:VConStr %>"
SelectCommand="SELECT [type] FROM [templatetype] WHERE [template] = #template"
>
<SelectParameters>
<asp:ControlParameter
Name="template"
Type="String"
ControlID="template"
PropertyName="Value"
/>
</SelectParameters>
</asp:SqlDataSource>
...
</ItemTemplate>

Related

if statement in asp.net ASP.DataList vb.net

In Classic ASP I was able to display a section break based on state. I have an SQL table with menu items, columns:(ListOrder(int),BeginUrl(varchar(20),ShowDate(varchar(20),EndUrl(varchar(20),Section(varchar(20)) BeginURL,ShowDate,EndUrl concatenate to create the link. Section is state (WA,OR,ID).
In classic I created a var nLevel set to "" then proceed to loop through the RS.
Do While NOT menuRS.EOF
If menuRS.Fields("Section") <> nLevel Then
Response.Write("<br><br>" & menuRS.Fields("Section"))
nLevel = menuRS.Fields("Section")
End If
If menuRS.Fields("Display") = 0 Then
Response.Write("<br>"& menuRS.Fields("BeginUrl")&menuRS.Fields("ShowDate")&menuRS.Fields("EndUrl")) & VbCrLf
End If
menuRS.MoveNext
Loop
Which would give me (roughly)
WA
[date]
[date]
OR
[date]
[date]
ID
[date]
Trying to duplicate that in .Net using a DataList, ItemTemplate, and ASP:Label of course using VB, is giving me fits on the 'section' break.
I have the main menu items looping out. Just failing on the Section Break.
<asp:DataList ID="MenuList1" runat="server" DataSourceID="NwccMenuList">
<ItemTemplate>
<asp:Label ID="SectionBreak" runat="server"
Text='<%# If(Eval("Section") <> Me.nLevel) Then (Eval("Section") Me.nLevel= Eval("Section") End If%>' />
</ItemTemplate>
<ItemTemplate>
<asp:Label ID="BeginUrlLabel" runat="server"
Text='<%# Eval("MenuUrl") %>' />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="NwccMenuList" runat="server"
ConnectionString="<%$ ConnectionStrings:nwcctanningConnectionString %>"
SelectCommand="SELECT ([BeginUrl]+[ShowDate]+[EndUrl]) AS MenuUrl, Section
FROM [Menu] WHERE ([Display] = 0) ORDER BY [Section] DESC, [ListOrder]">
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="Display"
Type="Byte" />
</SelectParameters>
</asp:SqlDataSource>
Not sure the nLevel variable (in codefile) is reachable or not cannot get past the If statement!
I appreciate any pointers or where I went wrong!
Generally, you want to replace one dinosaur code with another ancient code. OK, Let's do it in ASP.NET way. First, asp:DataList is not the best choice because it renders table. Use the simplest databound control like asp:Repeater. This is a sample code which do the work declarativelly.
<asp:Repeater ID="rptMenu" runat="server" DataSource="NwccMenuList">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li>
<asp:Literal runat="server" ID="ltrSection" Text='<%#Eval("Section") %>'></asp:Literal>
<asp:Repeater runat="server" DataSource="sqlSubmenu">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<FooterTemplate></ul></FooterTemplate>
<ItemTemplate>
<li>
<asp:HyperLink runat="server" Text='<%#Eval("MenuUrl") %>' NavigateUrl='<%#Eval("MenuUrl") %>'></asp:HyperLink>
</li>
</ItemTemplate>
</asp:Repeater>
<%-- Note that a child datasource is inside parent ItemTemplate --%>
<asp:SqlDataSource ID="sqlSubmenu" runat="server"
ConnectionString="<%$ ConnectionStrings:nwcctanningConnectionString %>"
SelectCommand="SELECT ([BeginUrl]+[ShowDate]+[EndUrl]) AS MenuUrl
FROM [Menu] WHERE ([Display] = 0 and Section=#section) ORDER BY [ListOrder]">
<SelectParameters>
<%-- literal control from parent ItemTemplate --%>
<asp:ControlParameter ControlID="ltrSection" Name="section" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</li>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="NwccMenuList" runat="server"
ConnectionString="<%$ ConnectionStrings:nwcctanningConnectionString %>"
SelectCommand="SELECT distinct Section FROM [Menu] WHERE ([Display] = 0) ORDER BY [Section] DESC">
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="Display"
Type="Byte" />
</SelectParameters>
</asp:SqlDataSource>
Again, this approach is more than 10 year old.

ASP.net How to use dropdownlist for retriving data from database

I have a button,dropdownlist,and gridview. and i want that when i select any option from dropdownlist and click button . then the releted data is show into gridview .i have a problem that the data show in gridview but gridview d'nt work with the button
my code is
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="E_NAME" DataValueField="E_ID">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [E_ID], [E_NAME] FROM [EMP_DETAIL]">
</asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [E_ID], [E_NAME], [E_CITY], [E_AGE], [E_DEPARTMENT] FROM [EMP_DETAIL] WHERE ([E_ID] = #E_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="E_ID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="E_ID" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="E_ID" HeaderText="E_ID" ReadOnly="True"
SortExpression="E_ID" />
<asp:BoundField DataField="E_NAME" HeaderText="E_NAME"
SortExpression="E_NAME" />
<asp:BoundField DataField="E_CITY" HeaderText="E_CITY"
SortExpression="E_CITY" />
<asp:BoundField DataField="E_AGE" HeaderText="E_AGE" SortExpression="E_AGE" />
<asp:BoundField DataField="E_DEPARTMENT" HeaderText="E_DEPARTMENT"
SortExpression="E_DEPARTMENT" />
</Columns>
</asp:GridView>
</form>
Set the AutoPostBack property of Drop-down to False or remove it:-
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="E_NAME" DataValueField="E_ID">
</asp:DropDownList>
Since, you need the results on button click, the form will be refreshed after button click and taking the selected value in drop-down.

Cannot Find Control ID in ControlParameter

I am trying to insert values from a textbox but I get an error that it cannot find the controlid in the controlparameter. The TextBox is inside a formview, which is inside a listview. The SqlDataSource is outside the ListView.
My InsertButton_Click Method
protected void InsertButton_Click(object sender, EventArgs e)
{
var ctrl = (Control)sender;
var formview = (FormView)ctrl.NamingContainer;
formview.ChangeMode(FormViewMode.Insert);
SectionListView.DataSource = SectionDataSource;
SectionListView.DataBind();
}
The InsertItem Template
<InsertItemTemplate>
<tr style="">
<td>
<div style="font-size: .8em;">
<asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
<ItemTemplate>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" Font-Size="1.2em" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
<asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemTextBox" runat="server" />
<asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemLabelTextBox" runat="server"/>
</ItemTemplate>
</asp:FormView>
</div>
</td>
</tr>
</InsertItemTemplate>
My SqlDataSource
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
When you want to use the child controls as control parameters, you need to use the qualified id as control id. Try changing the SQL data source as below
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionFormView$SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionFormView$SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>

How To Insert USERID (Type: Raw, Size: 16) from Ora_aspnet_users to Another Table

Sorry I cannot upload the screenshot due to the limited number of my reputation since I am a newbie here.
ora_aspnet_user table with column name userid with datatype of Raw(16)
link to
Instruct Table with userid with datatype of Raw (16) - Other columns is ID (Auto Trigger No), Command (Varchar2 = 256)
MY INLINE ASPX CODE:
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" Visible="False" />
<asp:BoundField DataField="COMMAND" HeaderText="COMMAND" SortExpression="COMMAND" />
<asp:TemplateField HeaderText="USERID">
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="USERNAME" DataValueField="USERID">
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
InsertCommand="INSERT INTO INSTRUCT (COMMAND, USERID) VALUES (:COMMAND, :USERID)">
<InsertParameters>
<asp:Parameter Name="COMMAND" Type="String" />
<asp:Parameter Name="USERID" DbType="Binary" Size="16" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT USERID, USERNAME FROM ORA_ASPNET_USERS">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
The above code will only insert the command textbox and but will not insert the USERID from the USERID dropdown.
I would really appreciate your help.
Thank you very much.
Replace your datasource <InsertParameter> with this one, it might help you, because I don't have your source code I cannot test it locally, the problem could occur somewhere elese though:
<InsertParameters>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="USERID" Type="Byte"></asp:ControlParameter>
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedItem.Text" Name="COMMAND" Type="String"></asp:ControlParameter>
</InsertParameters>

ASP.NET error, must declare the scalar variable "#ServerName" after second dropdownlist is select

There are 3 dropdownlists which are parent-child, after the 3rd dropdownlist is selected, the DetailsView will display result from all 3 dropdownlist selected. first is ServerName, second is Instance, then third is DatabaseName. After select first dropdownlist of servername, then new list of instance value appear on second dropdownlist. When I select anything on second dropdownlist. There a error message that say, "Must declare the scalar variable "#ServerName"". I don't understand what it mean and please help. Here a dropdownlist codes,
<ajaxToolkit:ComboBox ID="ComboBox1" runat="server" AutoCompleteMode="SuggestAppend" AutoPostBack="True" DataSourceID="SqlDataSource4" DataTextField="ServerName" DataValueField="ServerName" DropDownStyle="Simple" MaxLength="0" style="display: inline;">
</ajaxToolkit:ComboBox>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ServerName] FROM [tblServer]">
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = #ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = #ServerName AND [Instance] = #Instance">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Then I pass those dropdownlist function to DetailsView that come with SQLDataSource2,
on SelectCommand in SQLDataSource2, I wrote
SelectCommand="SELECT * FROM [tblDatabase] WHERE (([DatabaseName] = #DatabaseName) AND ([Instance] = #Instance) AND ([ServerName] = #ServerName))"
Then after that I add SelectParameter codes,
<SelectParameters>
<asp:ControlParameter ControlID="DropDownDatabase" Name="DatabaseName" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
In SqlDataSource3, your query takes two parameters: #ServerName and #Instance. But in the select parameters, you only define #Instance. Yes, you defined #ServerName in the previous data source, but not in this one.
I fixed from what Jay metioned recently and there also another issue that I fix it on instance and database dropdownlist codes, where what I fix and it work fine.
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = #ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = #ServerName AND [Instance] = #Instance">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

Resources