I am new to .net/c# and visual studio. I have been looking all over the internet for an answer but couldn't find it. Thanks for your help.
I'm populating a drop down list from a database table, I want to pass a dynamic parameter to the asp server control (the logged in persons username). The 2 areas I want to put this dynamic string I added PUT_LOGGED_IN_USERNAME_HERE".
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="PK_Task" AppendDataBoundItems="true">
<asp:ListItem Selected="True">Supportive</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [PK_Task], [Name] FROM [Task] WHERE ([PointPerson] LIKE '%' + #PointPerson + '%') AND [Status] LIKE 'Done'">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="PUT_LOGGED_IN_USERNAME_HERE" Name="PointPerson"
QueryStringField="PUT_LOGGED_IN_USERNAME_HERE"" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
How do I accomplish this?
I found the code to display username:
<%= Page.User.Identity.Name %>
However it is not working when I use it as shown:
<asp:QueryStringParameter DefaultValue="<%= Page.User.Identity.Name %>" Name="PointPerson"
QueryStringField="<%= Page.User.Identity.Name %>"" Type="String" />
You can access properties of SqlDataSource, including Parameters in code behind, like so:
SqlDataSource1.SelectParameters["PointPerson"].DefaultValue = "User";
Related
I've been looking online for a while and I may not be asking my question right but I can't find what I am looking for.
I have a dropdownlist on a web form that is databound to a SQL select statement that returns 3 items. I appended the list with a blank item at the top so that I am not selecting the other options on page load.
I also have a listview that has a ControlParameter of the DDL.
What I want to do is when the page loads to have all items selected instead of none. Do I need to set Selected to True on each of the items in the list and how would I do that if the information is fetched from a SQL query.
A snippet of the code with the queries:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSourceDockNumber"
DataTextField="DockID" DataValueField="DockID"
AutoPostBack="True" AppendDataBoundItems ="true">
<asp:ListItem Selected="False" Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceDockNumber" runat="server"
ConnectionString="<%$ ConnectionStrings:MarinaConnectionString %>"
SelectCommand="SELECT DISTINCT [DockID] FROM [Slip]">
</asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDataSourceAvailSlips" runat="server"
ConnectionString="<%$ConnectionStrings:MarinaConnectionString %>"
SelectCommand="SELECT * FROM [avilableSlips] WHERE ([DockID] = #DockID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="DockID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Pretty sure I know why I'm getting the error, just not sure how to go about fixing it. In a FormView ItemTemplate I've got a Label control for the Project ID# bound to the FormView's SQLDatasource. I've also got a DropDownList whose SQLDatasource I've got tied to the Label.Text as a ControlParameter. I am guessing since the Label control isn't bound yet, that's causing my DDL to kick out that error.
ASPX
<asp:FormView ID="FvChangeOrder" runat="server" DataKeyNames="ProjectChangeOrderID"
DataSourceID="FvChangeOrderSQL" OnItemCommand="FvChangeOrder_OnItemCommand"
OnDataBound="FvChangeOrder_OnDataBound">
<ItemTemplate>
<div>Project #: </div>
<div>
<asp:Label ID="LblProjectID" runat="server" Text='<%# Bind("ProjectID") %>' /></div>
<div>Shipment #: </div>
<div>
<asp:DropDownList runat="server" ID="DdlShipment" DataSourceID="DdlShipmentSQL"
SelectedValue='<%# Bind("ProjectShipmentID") %>' DataValueField="ProjectShipmentID"
DataTextField="ShipmentNo" Enabled="False" />
</div>
...
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="FvChangeOrderSQL" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>"
SelectCommand="SELECT [ProjectID], [ProjectChangeOrderID], [ProjectShipmentID], [SeqNo],
[Date], [EnteredBy_UserID], [Source], [Initiator], [Reason], [ReasonNotes],
[ApprovalCode], [Description], [NumPanels], [Amount], [IsCommissionable], [DateDue],
[DateRecd], [Status]
FROM [tblProjectChangeOrder] WHERE ([ProjectChangeOrderID] = #PCOID)">
<SelectParameters>
<asp:QueryStringParameter Name="PCOID" QueryStringField="PCOID" Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DdlShipmentSQL" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>"
SelectCommand="SELECT pco.ProjectShipmentID, ps.ShipmentNo FROM tblProjectChangeOrder pco
LEFT JOIN tblProjectShipment ps ON pco.ProjectShipmentID = ps.ProjectShipmentID
WHERE pco.ProjectID = #ProjectID">
<SelectParameters>
<asp:ControlParameter ControlID="FvChangeOrder$LblProjectID"
Name="ProjectID" PropertyName="Text"/>
</SelectParameters>
</asp:SqlDataSource>
Would I be better off leaving out the WHERE and ControlParameter on the DDL SQLDatasource and changing the DDL's SQL Source in codebehind on the OnDataBound method of the FormView?
Think I got it worked out. I changed the ControlParameter to just Parameter Type="String" and on Form_Load set a Label to FvChangeOrder.FindControl("LblProjectID"), assigned LblProject.Text to a string, then passed that as the DefaultValue for the Select Parameter.
On an semi-related problem I had to remove the SelectedValue on the markup side and handle SQLCommand in the Form_Load for the DDL SelectedValue. Probably a bad dataset but I kept getting an error that the value in the field wasn't in the List or something like that. There are multiple Change Orders for a given project that have NULL values for the ShippingID.
Anywho, all is working on the ItemTemplate side. Now to see if I can keep everything working on the EditItemTemplate. :P
I have a SQL table named doctor search and have 2 dropdownlists one shows expertise of the doctor and i want to fill dropdownlist 2 with the information of those doctor which are related to that expertise section.
This can be done easily if you are using the designer interface.
Try using the below code after making necessary changes (Connection String, Table name and Column name). If that doesn't work, I will share the steps
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [Expertise] FROM [Your Table]"></asp:SqlDataSource>
<asp:DropDownList ID="ddlExpertise" runat="server" DataSourceID="SqlDataSource1" DataTextField="Expertise" DataValueField="Expertise">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" SelectCommand="SELECT [DoctorDetails] FROM [Your Table] WHERE ([Expertise] = #Expertise)">
<SelectParameters>
<asp:ControlParameter ControlID="ddlExpertise" DefaultValue="" Name="Expertise" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddlDoctorDetails" runat="server" DataSourceID="SqlDataSource2" DataTextField="DoctorDetails" DataValueField="DoctorDetails">
</asp:DropDownList>
I have an access data source which loads a listbox values. I am trying to pass a parameter to the accessdatasource but I'm not sure what I am doing wrong.
<asp:DropDownList ID="customerName" runat="server">
<asp:ListItem value="69" Text="Danny"></asp:ListItem>
<asp:ListItem value="23" Text="Sarah"></asp:ListItem>
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="/App_Data/data.mdb" SelectCommand="SELECT * FROM table WHERE customerID='?'">
<selectparameters>
<asp:ControlParameter Name="customerID" ControlID="customerName" PropertyName="SelectedValue" />
</selectparameters>
<asp:ListBox ID="lstCustomers" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="customerName"
DataValueField="customerID" Width="175px" Height="365px"
onselectedindexchanged="lstCustomers_SelectedIndexChanged"></asp:ListBox>
The list comes back as blank... not sure what I'm doing wrong!
Just to clarify.. This was solved by Richard Deeming, but there's no answer to answer. I'll post the code here
It was solved by removing the single quote marks around the ? parameter in the SQL
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="/App_Data/data.mdb" SelectCommand="SELECT * FROM table WHERE customerID=?">
Does anyone how can I get the parameters for SelectParameters from another dropdown in c# .net?
I check online , and I follow the steps that been told, but it's doesn't seem working for me.
Code:
If I have the following code to retrieved the data for a dropdown:
<asp:DropDownList ID="DropDownVisaType" runat="server" DataSourceID="dsDropDownVisaType" CssClass="dsDropDownVisaType"
DataValueField="VisaTypeID" DataTextField="VisaType" AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsDropDownVisaType" runat="server" ConnectionString="<%$ ConnectionStrings:SmartStaffConnectionString %>"
SelectCommand="app_visa_type_select" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
After I select the dropdown, I want to use the VisaTypeID from the dropdown to retrieve data from another dropdown as below:
<asp:DropDownList ID="DropDownVisaTypeSpecific" runat="server" DataSourceID="dsDropDownVisaTypeSpecific" CssClass="dsDropDownVisaTypeSpecific"
DataValueField="VisaTypeSpecificID" DataTextField="VisaTypeSpecific" AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsDropDownVisaTypeSpecific" runat="server" ConnectionString="<%$ ConnectionStrings:SmartStaffConnectionString %>"
SelectCommand="app_visa_type_specific_select" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Property="SelectedValue" ControlID="DropDownVisaType" Name="VisaTypeID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Does anyone know what is my code going wrong and how can I make it work?
There is an event SelectedIndexChanged for A dropdown by using that you can get the value of selected Item.
Like below:
protected void DropDownVisaType_SelectedIndexChanged(object sender, EventArgs e)
{
Int32 value = Convert.ToInt32(DropDownVisaType.SelectedItem.Value);
DropDownVisaTypeSpecific.DataSource=Method_Name(Value);
DropDownVisaTypeSpecific.DataBind();
}