Using a different value from a drop down list - asp.net

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>

Related

SQL insert always take dropdownlist default value

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

How to populate a dropdown list in asp.net from a DB table?

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

Adding Value to databound DropDownlist (asp.net)

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

change text on a hyperlink in a datalist

I'm trying to bind a column from a SQL code that i have written to all hyperlinks in a datalist. This should be really simple but I'm getting the error
DataBinding: 'System.Data.Common.DataRecordInternal' does not contain
a property with the name 'NumberOfComments'.
Well I'm pretty sure the column exist but in this case it's created by a function maybe that has something to do with it. When I run the SQL code i get the values I should.
The hyperlink
<asp:HyperLink ID="lnkComment" runat="server"
NavigateUrl='<%# Eval("ID", "~/Default.aspx?ID={0}") %>'
Text='<%# Eval("NumberOfComments") %>'></asp:HyperLink>
The SQLDataSource
<asp:SqlDataSource ID="sdsNews" runat="server"
ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString %>" SelectCommand="SELECT News.ID, News.Topic, News.Text, News.PostTime, aspnet_Users.UserName, "NumberOfComments" = dbo.fnNumberOfCommentOnNews(News.ID)
FROM News INNER JOIN
aspnet_Users ON News.UserId = aspnet_Users.UserId
WHERE (News.ID = ISNULL(#ID, News.ID))
ORDER BY News.PostTime DESC ">
<SelectParameters>
<asp:QueryStringParameter DbType="Guid" Name="ID" QueryStringField="ID" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
Why do you have """ surrounding NumberOfComments FieldName?
Try pasting the select command into SSMS (SQL Server Mgmt studio) and you should most definitely get an incorrect syntax error. If you drop those quot html code you will still be able to reference and bind to that column.

How to update an ASP.NET label with SqlDataSource return value

On an ASP.NET page, I have a SqlDataSource configured with the following SELECT command:
SELECT AVG(Rating) FROM [Ratings] WHERE ([AlbumID] = #AlbumID)
How would I place that average value into a label?
You need to use FormView control to put the Label in.
Something like this:
<asp:formview id="formview1" runat="server" datasourceid="your-datasource-id">
<itemtemplate>
<asp:label id="label1" runat="server" text='<%# Eval("column-name") %>' />
</itemtemplate>
</asp:formview>
Replace your-datasource-id and column-name in the above code.
PS: you might need to alter the query to have a column-name for that one value:
SELECT AVG(Rating) AS "average" FROM [Ratings] WHERE ([AlbumID] = #AlbumID)

Resources