I have two asp dropdown lists,the second one will generate options when the first dropdown list has a selected value. Both dropdown list are getting data from database.
However, when I try to insert these two values into my table,the first dropdown list item will insert correctly,the second dropdown list always inserts the first item into my table.
After some testing, I found out that the second dropdown list is not selected and the first item was inserted into my table because its the default value.
BTW, my first dropdown list is autopostback while the second one is not.
I am using Microsoft Web Developer 2010 express and I am using Visual Basic.
first dropdown list:
<asp:DropDownList ID="stockcodeddl" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="stockcode"
DataValueField="stockcode" Width="174px" Height="19px">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:oakwell %>"
SelectCommand="SELECT [stockcode] FROM [orders] GROUP BY [stockcode]">
</asp:SqlDataSource>
second dropdown list:
<asp:DropDownList ID="componentddl" runat="server" DataSourceID="ComponentsByStockCode" DataTextField="component"
DataValueField="stockcode" Width="174px" Height="19px"
Enable="true" onchange="getComponent()" >
</asp:DropDownList>
<asp:TextBox ID="comtxt" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="ComponentsByStockCode" runat="server"
ConnectionString="<%$ ConnectionStrings:oakwell %>"
SelectCommand="SELECT component, stockcode FROM orders WHERE ([stockcode] = #stockcode) GROUP BY stockcode, component">
<SelectParameters>
<asp:ControlParameter ControlID="stockcodeddl" Name="stockcode" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Please check that your dropdown binding data from the database was within the IsPostback on Page init. If not then before button event fires it will rebind the dropdown on page init function.
Try
dropdown2.selectedvalue
or
dropdown2.selecteditem
Related
Sorry my title is crap but cant think of a better way to put it.
Using MS VS 2013 and MS SQL server 2012. VB
I have a drop down list and the stored procedure it calls gets two values from a table in SQL. The values are the ID and the name. The drop down list is displaying the name but I want to use the ID when a name is selected in the drop down list in code.
How do I do this?
My code for the DDL is
<asp:DropDownList ID="DDLAllTreatments" runat="server" DataSourceID="AllTreatments" DataTextField="Name" DataValueField="Name"></asp:DropDownList>
<asp:SqlDataSource ID="AllTreatments" runat="server" ConnectionString="<%$ ConnectionStrings:InfinitySPa %>" SelectCommand="SP_GetAllTreatments" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Change DataValueField to ID:
<asp:DropDownList ID="DDLAllTreatments" runat="server" DataSourceID="AllTreatments" DataTextField="Name" DataValueField="ID"></asp:DropDownList>
<asp:SqlDataSource ID="AllTreatments" runat="server" ConnectionString="<%$ ConnectionStrings:InfinitySPa %>" SelectCommand="SP_GetAllTreatments" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
I am using Visual Basic for ASP. NET
I know how to populate a gridview with data using only code behind, like this
Dim ds as new datasourse
Dim da as new dataadaptor
Dim con as new SqlConnection
Dim cmd as new SqlCommand
...
Cmd = "select ticketID, problem_text from problems where support_engineer = " & Session("Logged_in_user_id")
...
Gridview1.Datasource = ds
Gridview1.Datasources. DataBind
And that works fines, but my question is: how can i drag and drop a gridview control, and using only the wizard to populate the gridview in design time, how can i define the SELECT statement to only select rows that are related to the logged in user id? See the sql statement above, it uses a session variable that i create in Page_Load event, but how can i use the same logic in design mode? (i don't want to modify the datasource control in code behind)
I looked in youtube and google, and this sites, but all results are simply showing me how to populate the gridview with all rows or with a static condition, not a dynamic one like the one i demonestrated.
Any help is highly appreciated
You could try replacing the form tag in the aspx file
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT ticketID, problem_text FROM Tabs WHERE (support_engineer = #Param1)">
<SelectParameters>
<asp:SessionParameter Name="Param1" SessionField="Logged_in_user_id" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
DataSourceID="SqlDataSource1">
</asp:GridView>
</div>
</form>
I found out the proper way of binding the gridview to a dynamic session variable, without involving code behind:. Simply use the property <SessionParameter> inside the <SelectParameter>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="SELECT ticketID, problem_text FROM problems
WHERE (support_engineer = #eng_id)
<SelectParameters>
<asp:SessionParameter
Name="eng_id"
SessionField="LoggedInUser"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
DataSourceID="SqlDataSource1">
</asp:GridView>
I want to populate a dropdown list with values from a table I created. I only want to populate the list with one of the fields- the languages in my table. I think I have connected to the data source correctly, but I don't know what I have to do to get the values into the list. I can enter my own values but I'd rather have this automated.
This is what I have so far, but I'm guessing there's more to it than just linking the list to the data source.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HBshareIndexConnectionString %>"
SelectCommand="SELECT * FROM [Web_Metrics] WHERE ([LCID] = #LCID)">
<SelectParameters>
<asp:QueryStringParameter Name="LCID" QueryStringField="LCID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="Select LCID: " ></asp:Label>
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1" DataTextField="LCID" DataValueField="LCID">
<asp:ListItem>Select LCID...</asp:ListItem>
</asp:DropDownList>
Thanks for the help. I got the dropdown list populated now, but I was wondering how do I actually get the repeater I'm using to display the details of the LCID the person selects? I've seen people talking about page.isPostback but I don't know what that is or if it works with my current setup. I need to somehow get the LCID they selected and then refresh the page to show the details of that LCID. Does anyone have any ideas?
Your problem is that you're trying to define list items and a data source.
If you want to insert a "Select an item.." option, I would suggest prepending it to your resultset (getting it to always be first with a UNION and ORDER BY could be difficult depending on your fields) or inserting it after databinding in your code behind:
Modification to DropDownList1s attributes:
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1" DataTextField="CountryName" DataValueField="LCID" OnDataBound="InsertChooseItem" />
C#:
protected void InsertChooseItem(object sender, EventArgs e)
{
ListItem selectOnePlease = new ListItem("Select LCID..", 0);
DropDownList1.Items.Insert(0, selectOnePlease);
}
VB:
Protected Sub InsertChooseItem(sender As Object, e As EventArgs)
Dim selectOnePlease As New ListItem("Select LCID..", 0)
DropDownList1.Items.Insert(0, selectOnePlease)
End Sub
You've specified the select parameter to be a query string, so the data in your DropDownList will only be populated when the URL resembles something like:
http://{Your server name}/Default.aspx?LCID=1
That doesn't make any sense though because the LCID column in your table should be unique, so although this will work there will only be one value in the drop down list.
I think what you want is to display all the languages from the database in the drop down, here's an example:
<asp:SqlDataSource ID="sqlDS" runat="server"
ConnectionString="<%$ ConnectionStrings:HBshareIndexConnectionString %>"
SelectCommand="SELECT LCID,Language FROM [Web_Metrics]">
</asp:SqlDataSource>
<asp:DropDownList ID="ddlLanguages" AppendDataBoundItems="true" Width="150px" runat="server" DataSourceID="sqlDS" DataTextField="Language" DataValueField="LCID">
<asp:ListItem>Select Language</asp:ListItem>
</asp:DropDownList>
Just a note, you should never display the ID to the client, it's totally meaningless to them, the ids are mostly used by developers in the background that's why in the drop down I set the:
DataTextField="Language" (This is the language name visible to the user)
DataValueField="LCID" (Not visible to the user, but useful for any additional processing in code behind)
AppendDataBoundItems="true" - this line of code will keep all the items you've manually added to the drop down, e.g "Select Language" and will append any data bound items e.g from a SQL table
I am creating a website which has 1 dropdownlist that bound to Northwind database, Customers table. This Drop downlist will list all the countries from the tables. I have tried to add "Japan" (which was not in the list) to this dropdownlist. It was added but it always appear at the top (or default value). My question is: Is it possible to add Japan and not make it appear at the top, so it will follow the alphabetical order?
This is my code:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Country"
DataValueField="Country"><asp:ListItem Text ="Japan" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2 %>"
SelectCommand="SELECT Country FROM Customers GROUP BY Country ORDER BY Country">
</asp:SqlDataSource>
You can try this way in your sql script.
SELECT Country
FROM
(
SELECT Country
FROM Customers
GROUP BY Country
UNION ALL
SELECT 'JAPAN'
) M
ORDER BY Country
In ASP.NET, what is the definitive way to pull one record from the database and bind it and HTML tag? Brevity and style points count.
For brevity, you'll want to use the SqlDataSource.
<asp:SqlDataSource ID="sql" runat="server" ConnectionString='<%$ ConnectionStrings:MyConnectionString %>'
SelectCommandType="Text"
SelectCommand="select MyField FROM MyTable WHERE ID = #id"
>
<SelectParameters>
<asp:ControlParameter ControlID="txtUserName" PropertyName="Text" Name="id" />
</SelectParameters>
</asp:SqlDataSource>
<asp:BulletedList runat="server" DataTextField="MyField" DataSourceID="sql">
</asp:BulletedList>
It's not clear what you want but, detailsview control is designed for displaying only one record from data source.
DetailsView is a data-bound user
interface control that renders a
single record at a time from its
associated data source, optionally
providing paging buttons to navigate
between records.