by Nathan
2. June 2009 10:17
If you would like your application to set the selected item in your dropdownlist to an item from a variable passed to it, or from the database
Here are some examples:
ASP.NET
<asp:DropDownList ID="DropDownList1" runat="server"/><br />
DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(value.ToString()));
VBScript ASP Example:
response.write "<select size=""1"" name=""selectData"" id=""selectData"">"
response.write "<option value='0'>-- Select Data --</option>"
do while not objRs.eof
if (ContactType = objRS("Value")) then
response.write "<option value='"+cstr(objRS("Value"))+"' selected>"+objRS("Description")+"</option>"
else
response.write "<option value='"+cstr(objRS("Value"))+"'>"+objRS("Description")+"</option>"
end if
objRs.MoveNext
loop
response.write "</select></font></p>"
If you have a lot of variables i suggest using a for loop:
response.write "<select name='select1'>"
Dim MyWebSiteArray
PrefOddsArray = MyWebSiteArray("web design","design","website","nathan","baker")
For each Item in MyWebSiteArray
DatabaseValue = RS("ValueFromDatabase")
if(DatabaseValue = Item) then
response.write "<option value="""+Item+""" selected>"+Item+"</option>\n"
else
response.write "<option value="""+Item+""">"+Item+"</option>\n"
end if
Next
response.write "</select></td>"
response.write "</tr>"
by Nathan
29. March 2009 08:45
If you receive this error
ADODB.Recordset error '800a0e78'
Operation is not allowed when the object is closed.
you will need to check the state of the recordset as in the code below before you access any properties of the object
if not oRs.state <> 1 then
if not oRs.eof then
if oRs("Value").Value > 0 then
myval = oRs("Value").Value
else
myval = 0
end if
end if
else
Response.Write "Cannot find any records"
end if
by Nathan
9. February 2009 14:26
This is an example of a connection string for ASP (JScript and VBScript)
var connString = "Provider=sqloledb; Network Library=DBMSSOCN; Data Source=10.1.1.1;User
ID=exampleUser; Password=examplePassword;Connection
Timeout=90;Application Name=MyApplication;"
I came across the following errors in my ASP Code when my connection string was not setup correctly.
Microsoft OLE DB Provider for SQL Server error '80040e4d'
Invalid authorization specification
The problem was my original connection string did not have the userid and password credentials
Invalid connection string attribute
Invalid Data in either Password and/or Username or Initial Catalog etc