OK, so I've created a form that has 2 connecting drop down list. I have a button that redirects to another page. This one is currently blank, but I've tried adding a detail view to it, however, when I try and connect it to the data source it just shows the first of the column names and abc.
I need to know how to connect it to my data source which would create 4 rows and the corresponding information for those rows. In this case it would be something akin to Quantity, Category, Product Name, and the Description for the item selected on the first page.
Any help is seriously appreciated.
UPDATE
I still can't get this working.
Here is a screenshot of page one.
My instructions for this page are simply...
On the first screen, a user chooses a category and then chooses a product from the selected category. The Category and Product controls are populated using the following procedures: So I set up both of these with the respective procedures, and they work fine.
The order detail button simply redirects to the order detail page, but I'm not sure if I have to do anything to save the selection.
However, here is the code I've put together for those 3 buttons.
<div style="height: 182px">
Category: <asp:DropDownList ID="ddlCategory"
runat="server" DataSourceID="SqlDataSource1" DataTextField="CategoryName"
DataValueField="CategoryId" AutoPostBack="True"/>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:DeveloperInterviewConnectionString %>"
SelectCommand="CategoryListing" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
<br/>
Product: <asp:DropDownList ID="ddlProduct"
runat="server" DataSourceID="SqlDataSource2" DataTextField="ProductName"
DataValueField="ProductId"/>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:DeveloperInterviewConnectionString %>"
SelectCommand="CategoryProducts" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCategory" Name="CategoryId" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<input type="button" value="Order Details"
onClick="location.href = 'OrderDetails.aspx';">
So do I have to do anything to setup the data to go from one form to the next, and I still can't get the view operational.
Ok, So I tried linking up your stored proc to a details view and didn't get very far with it. I think the problem is the output params.
Try modifying the stored proc to the following:
CREATE PROCEDURE [dbo].[ProductDetails]
#ProductId INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
ProductName,
ProductDescription,
QuantityInStock
from Product
where productid = #ProductId
END
GO
Point the datasource at that Sproc, link up the ProductID param and you should be set.
UPDATE: adding my asp.net code
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataSourceID="SqlDataSource1">
<Fields>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="ProductDescription" HeaderText="ProductDescription" SortExpression="ProductDescription" />
<asp:BoundField DataField="QuantityInStock" HeaderText="QuantityInStock" SortExpression="QuantityInStock" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DemoDBConnectionString %>" SelectCommand="ProductDetails" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="ProductId" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Related
I have got very basic Product data table, I set up my grid view data source to sqlDataSoruce with paging and sorting options true.
But I also have a DropDownList and if a user chooses a certain product group ( Vegetables, Fruits, Frozen etc), grid view only shows that product group items.
String sqlSearchByProduct = " SELECT ID, pID,pGroupID, pName, pPrice, Status, Stock FROM productsTable WHERE (pGroupID= ProductGroupIDDropDownList.SelectedItem.Value)";
sqlDataSource.SelectCommand = sqlSearchByProduct ;
The grid shows all products from groupID selected from dropdownlist but the issue is when I want to sort it by name or price, grid view shows all products again.
I will be very grateful if you know how to fix this issue.
Thank you.
You need to keep SelectedValue of the dropdown and the easiest way to do so is to bind all data declaratively. Something like this:
<asp:DropDownList ID="ddCategory" runat="server" DataTextField="categoryname"
DataValueField="categoryid"
AppendDataBoundItems="true" DataSourceID="sqlCategory" AutoPostBack="true">
<asp:ListItem Value="0" Text="- Select -"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sqlCategory" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindCnn %>"
SelectCommand="select categoryid,categoryname from dbo.categories">
</asp:SqlDataSource>
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" DataKeyNames="ProductID" DataSourceID="sqlNWind">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlNWind" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindCnn %>"
SelectCommand="SELECT Products.ProductID, Products.ProductName FROM Products where (#categoryid=0 or CategoryID=#categoryId)">
<SelectParameters>
<asp:ControlParameter Name="categoryid" ControlID="ddCategory" Type="Int32" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
No code behind required.
Side note: never build sql query string like this
" ... WHERE (pGroupID= ProductGroupIDDropDownList.SelectedItem.Value)"
Such code is prone to SQL Injection attack. Always use parameters.
Have you rebound your data to the gridview, e.g. below
GridView1.DataBind();
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"
Been using this site for years and have always found it very helpful and normally all my questions get answered by reading the site.
I have some case studies on a site than need to be related to other sections of the site and i am using this code to pull the records from the database :-
<div id="sidebar">
<asp:accessdatasource
id="cskey"
runat="server"
datasourcemode="DataSet"
datafile="_db/db.mdb"
selectcommand="SELECT id FROM markets_case_assign WHERE productid = #catid">
<SelectParameters>
<asp:QueryStringParameter Name="catid" QueryStringField="id" DefaultValue="1" />
</SelectParameters>
</asp:accessdatasource>
<asp:Repeater id="cskeycontent" runat="server" DataSourceID="cskey">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Visible="false" Text='<%#Eval("id")%>'></asp:Label>
<asp:accessdatasource
id="csval"
runat="server"
datasourcemode="DataSet"
datafile="_db/db.mdb"
selectcommand="SELECT id,title,company FROM casestudies WHERE id = #pid">
<SelectParameters>
<asp:controlparameter name="pid" controlid="Label1" DefaultValue="1" />
</SelectParameters>
</asp:accessdatasource>
<asp:Repeater id="csvalcontent" runat="server" DataSourceID="csval">
<ItemTemplate>
<div class="case-studies">
<h2><%#Eval("company")%></h2>
<p><%#Eval("title")%></p>
<p class="no-margin-bottom">Read case study</p>
</div>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</div>
I was just wondering if there is a simpler way of doing it.
Any help would be much appreciated.
Cheers Andy
i was wondering if there was a way of doing it with only one query
To make it one query, just change the SQL call as:
SELECT id,title,company FROM casestudies
WHERE id IN (SELECT id FROM markets_case_assign WHERE productid = #catid)
and use only one repeater (the inner one).
Now you lose the extra advanced to separate them with some extra header, line etc..
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
I'm selecting table data of the current user:
SELECT [ConfidenceLevel], [LoveLevel], [HappinessLevel] FROM [UserData] WHERE ([UserProfileID] = #UserProfileID)
I have set a control to the unique user ID and it is getting the correct value:
HTML: <asp:Label ID="userID" runat="server" Text="Labeluser"></asp:Label>
C#: userID.Text = Membership.GetUser().ProviderUserKey.ToString();
I then use it in the where clause using the Configure Data Source window
unique ID = control then controlID userID
(fills in .text for me)
I compile and run but nothing shows up where the table should be. Any suggestions?
Here is the code it has created:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ConfidenceLevel" HeaderText="ConfidenceLevel"
SortExpression="ConfidenceLevel" />
<asp:BoundField DataField="LoveLevel" HeaderText="LoveLevel"
SortExpression="LoveLevel" />
<asp:BoundField DataField="HappinessLevel" HeaderText="HappinessLevel"
SortExpression="HappinessLevel" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringToDB %>"
SelectCommand="SELECT [ConfidenceLevel], [LoveLevel], [HappinessLevel] FROM [UserData] WHERE ([UserProfileID] = #UserProfileID)">
<SelectParameters>
<asp:ControlParameter ControlID="userID" Name="UserProfileID"
PropertyName="Text" Type="Object" />
</SelectParameters>
</asp:SqlDataSource>
i have answered on the asp.net page as well but in case you dont see that one i'll do it here as well...
take out any reference to "object" the sqldatasource code generates...
it should read:
PropertyName="Text" />
if you have update and insert and delete you'll have to remove it from there as well...