c# pass selected value from dropdownlist to accessdatasource selectparameter - c#

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=?">

Related

Preloading a SQL databound listview in ASP.NET

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>

How to set initial value for dropdownlist while using sqldatasource as binding

I have 2 dropdownlist, which are bound to a SQLDataSource and value of 2nd dropdownlist is based on 1st dropdownlist. The problem is that by default dropdownlist are displaying actual values. I put initialvalue in dpsem so, it's working correctly.but when i do same in dpsubj dropdownlist it is showing previous value also.
If I select dpsem--4 then dpsubj should be A,B,C, and if if i select dpsem--6 then dpsubj should be D,E,F. But it is displaying A,B,C,D,E,F...
I am knowing that it is problem related to Autopostback...
<asp:DropDownList ID="dpsem" runat="server" Height="28px"
Width="99px" AutoPostBack="True" DataSourceID="selectsemester"
DataTextField="sem_no" DataValueField="sem_no" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Value="0">-select</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dpsubj" runat="server" Height="28px"
Width="99px" AutoPostBack="True" DataSourceID="Selectscode"
DataTextField="scode" DataValueField="scode" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Value="0">-select</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="Selectscode" runat="server"
ConnectionString="<%$ ConnectionStrings:AttendenceManagementConnectionString %>"
SelectCommand="SELECT DISTINCT [scode] FROM [Semester_Wise_Subject] WHERE (([branch_name] = #branch_name) AND ([sem_no] = #sem_no))">
<SelectParameters>
<asp:ControlParameter ControlID="lbdept" DefaultValue="IT" Name="branch_name"
PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="dpsem" DefaultValue="6" Name="sem_no"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="selectsemester" runat="server"
ConnectionString="<%$ ConnectionStrings:AttendenceManagementConnectionString %>"
SelectCommand="SELECT DISTINCT [sem_no] FROM [Batch_year]">
</asp:SqlDataSource>
You should use dpsubj.Items.Clear().. and then again load the filtered data on selectedindexchanged it will first delete all teh previous records in dpsubj dropdown and then use code to load value in dpsubj
I believe you have to create a SelectedIndexChanged event on the first drop down and have that event cause a databind on the second drop down. Also by default if you don't want the second drop down to display data get rid of the default parameter values.
If you are doing anything with ajax you also want to make sure that the first drop down updates the second in your script manager as well.
set AppendDataBoundItems="false" on dpsubj
since you set this property as true, items are not cleared before data binding.

Get parameters for SelectParameters from another dropdown in c# .net

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();
}

Not catching SelectedIndexChanged

I have dropdownlist in a GridView (Actually have a bunch) but I am only concerned about one of them. I need to capture when the user makes a new selection to see if they want to add a item into the dropdownlist. Here is my code ..
This never gets called:
protected void DebtorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.Rows[GridView1.EditIndex];
DropDownList list = (DropDownList)row.FindControl("DebtorDropDownList");
string temp = list.SelectedValue;
}
Here is the ASP code:
<ItemTemplate>
<asp:DropDownList ID="DebtorDropDownList" AppendDataBoundItems="true" runat="server"
DataSourceID="SqlDataSource4" DataTextField="FirstName" DataValueField="contactkey"
SelectedIndexChanged="DebtorDropDownList_SelectedIndexChanged" AutoPostBack="True" >
<asp:ListItem Selected="True">Select</asp:ListItem>
<asp:ListItem >Add New Contact</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource4" runat="server"
ConnectionString="<%$ ConnectionStrings:AuditDevConnectionString2 %>"
SelectCommand="sp_fc_vm_getSpokeTo" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="0" Name="DebtorKey"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
What am I missing? Also am using the right code to find the correct control?
I found my problem. If you look at my ASP closely you will notice that the "SelectedIndexChanged" event is missing "on"

How to implement nested DropDownLists insid a GridView in asp.NET?

I have two nested DropDownLists (the value selected in the 1st ddl will limit the content of the 2nd ddl).
The example attached here runs fine outside of a GridView. As soon as I try to insert this into a GridView, I get an error because I cannot identify the ControlID to use in the ControlParameter.
The code outside of the GridView looks like this:
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
And the DataSources look like this:
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [Items]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [SubItems] WHERE [itemId]=:v_ItemId">
<SelectParameters>
<asp:ControlParameter Name="v_ItemId" ControlID="ACTION1_DROPDOWNLIST" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
Thanks!
I have tried something similar and it works fine. I can't tell from your post whether the data sources are together with the dropdowns inside the ItemTemplate of the GridView. At least the second one should be inside the ItemTemplate so that you will have a data source for each line in the grid.
I have posted my page below. It's using different tables, but it's the same idea.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Surname" DataValueField="FamilyID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="PersonID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Person] WHERE ([FamilyId] = #FamilyId)">
<SelectParameters>
<asp:ControlParameter Name="FamilyId" ControlID="ACTION1_DROPDOWNLIST"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server">
</asp:LinqDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Family]">
</asp:SqlDataSource>
</form>
The actual id's are build based on where they are, so you will see something like $GridView1 inserted in the id's.
To solve this you can set ClientIDMode="Static" on your page properties (first line of your .aspx file).
Or use "javascript:var a = document.getElementById('" + ACTION1_DROPDOWNLIST.ClientID + "');

Categories

Resources