Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dropdownlist databind

Status
Not open for further replies.

zma

Programmer
May 30, 2006
25
US
I am a beginner at asp.net.
Here I have put data from a table into a dropdown list. I have only been able to get the leftmost column in the query to show. How can other columns be shown without combined them into 1 column like in the column r? For example, how can I include dxid?
Thanks.

<% @import namespace="system.data" %>
<% @import namespace="system.data.sqlclient" %>
<html>
<head>
<title>
</title>
<script language="vb" runat="server">
sub page_load(sender as object,e as eventargs)
if not page.ispostback
binddata()
end if
end sub
sub binddata()
dim connection1 as sqlconnection=new sqlconnection("server=111.111.11.11;uid=sa;pwd=aaaa;database=test")
const q1 as string="select code+' '+descriptor as r,dxid from icd9s where date_del is null order by code"
dim command1 as sqlcommand=new sqlcommand(q1,connection1)
connection1.open()
dim dr as sqldatareader=command1.executereader()
ddl1.datasource=dr
ddl1.databind()
connection1.close()
end sub
sub show(sender as object,e as eventargs)
l1.text="*"&left(ddl1.selecteditem.text,instr(ddl1.selecteditem.text,chr(32)))&"*"
end sub
</script>
</head>
<body>
<form runat="server">
<asp:dropdownlist id="ddl1" datatextfield="r" datavaluefield="dxid" runat="server"/>
<asp:button id="b1" text="click" onclick="show" runat="server" />
<asp:label id="l1" columns="8" runat="server" />
</form>
</body>
</html>
 
First.. you should use the code behind page for all of your code, not the classic ASP inline style. Debugging etc will be much easier.

Second.. You should used paramertized queries or Stored Procedures to get and manipulate your data. It will protect you from SQL injection and be much easier to debug and maintain.

Third, a drop down list will only use 2 columns from your query. One to show what you want, and one as a value. The same column can be used for both. In order to show more than one column of data, you will have to concatenate them as you are currently doing.

JIm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top