DropDownList value selected base on database value?

Hi, is there anyway how I can allow my dropdownlist (on pageload event) to select the value base on the value found in the database, in addition I also want the dropdownlist to contain other values too however the selectedValue must be the one found in the database. Sorry if it sounds confusing.


I'm using VB.NET 2003 with ASP.NET

Thanks.
0
coranyge
5/22/2006 2:57:53 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

8 Replies
1742 Views

Similar Articles

[PageSpeed] 30

Hi,

Whatever you want to get selected for your dropdown use following code.

if(dropdownid.items.findbyvalue("value") != null)
{
    dropdownid.items.selecteditem = false;
    dropdownid.items.findbyvalue("value").selected = true;
}

thanks
www.lookforemails.com
0
rajkiran
5/22/2006 5:35:23 AM
Hi rajkiran I have tried using findbytext instead of value but now I'm getting this error instead


Server Error in '/SPOTSII_Authorise_V3' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 93: ddlROTEAM.DataTextField = "CODE_DESC"
Line 94: ddlROTEAM.DataValueField = "CODE_VALUE"
Line 95: ddlROTEAM.Items.FindByText("NIGHTSPOT CHECKING TEAM").Selected = True
Line 96: ddlROTEAM.DataBind()
Line 97: db.Close()


Source File: c:\inetpub\wwwroot\SPOTSII_Authorise_V3\Query\SummonReport.aspx.vb Line: 95

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
SPOTSII_Authorise_V3.SummonReport.test() in c:\inetpub\wwwroot\SPOTSII_Authorise_V3\Query\SummonReport.aspx.vb:95
SPOTSII_Authorise_V3.SummonReport.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\SPOTSII_Authorise_V3\Query\SummonReport.aspx.vb:79
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032



this is my code in vb

Private Sub test()
Dim db As New DbMgmt
Dim strSql As String
Dim rdr As SqlClient.SqlDataReader
Dim abc As String
abc = Session("ROTEAM")
db.Connect()
strSql = "SELECT * FROM TB_CODE WHERE CODE_TYPE = 'EE'"
rdr = db.ExecuteReader(strSql)
ddlROTEAM.DataSource = rdr
ddlROTEAM.DataTextField = "CODE_DESC"
ddlROTEAM.DataValueField = "CODE_VALUE"
ddlROTEAM.Items.FindByText("NIGHTSPOT CHECKING TEAM").Selected = True
ddlROTEAM.DataBind()
db.Close()
End Sub




What's going wrong?

0
coranyge
5/23/2006 7:39:04 AM

use following code

Private Sub test()
Dim db As New DbMgmt
Dim strSql As String
Dim rdr As SqlClient.SqlDataReader
Dim abc As String
abc = Session("ROTEAM")
db.Connect()
strSql = "SELECT * FROM TB_CODE WHERE CODE_TYPE = 'EE'"
rdr = db.ExecuteReader(strSql)
ddlROTEAM.DataSource = rdr
ddlROTEAM.DataTextField = "CODE_DESC"
ddlROTEAM.DataValueField = "CODE_VALUE"
ddlROTEAM.DataBind()
db.Close()
if(ddlROTEAM.Items.FindByText("NIGHTSPOT CHECKING TEAM") != null)
ddlROTEAM.Items.FindByText("NIGHTSPOT CHECKING TEAM").Selected = True
End Sub

Thanks
www.lookforemails.com
0
rajkiran
5/23/2006 8:34:28 AM
use are facing this error becuase u r trying set selected an item using findbytext function when there is no item with that text in drop down. so first databind() items then set selected allways perform a null check

thanks
www.lookforemails.com
0
rajkiran
5/23/2006 8:39:21 AM
Hey rajikran it works now thanks!


but I've a question here I'm using VB to code and VB doesnt accept != null, so is there a replacement to check for null in VB?
0
coranyge
5/24/2006 2:20:07 AM
I not quite aware if thing in VB.Net 

u search over the net for that

thanks
0
rajkiran
5/24/2006 3:56:44 AM
coranyge:
Hey rajikran it works now thanks! but I've a question here I'm using VB to code and VB doesnt accept != null, so is there a replacement to check for null in VB?


The VB equivalent would be Is Nothing.


Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
5/25/2006 7:32:37 PM
Thanks Peter! I will try it out
0
coranyge
5/26/2006 2:08:22 AM
Reply:

Similar Artilces:

Selected value in select list with javascript based off value in database
 I've got a classic asp page with a select list. I need the select list's selected value to be whatever the user selected when they created their account. I've been trying to use <body onLoad="onLoad();"> to call my function, and then I'm trying to specify which value should be selected in the function. Also, when the select list selected values change, there is a javascript function I call to disable text boxes. I think there is an issue with my javascript function, and I can't find any examples of this from good ole' google.  My j...

Form: Focus on selected values in dropdownlists, Refresh after selection and hidding formfields based on selection.
Hello again,I have a form with several dropdown lists. Depending on the selected value in the dropdown lists other fields become available in that same form. It is working in classic ASP in combination of JavaScript. I want to migrate it to ASP.NET. I have posted a part of the form at the end of this post.My questions:1. I use <option <%If iFood="0" then%>selected<%End If%> value="0">No value selected</OPTION> to select the selected item when the form is reloaded after the selecting. I know that this also works in ASP.NET, but isn't there a typical ASP....

Setting a dropdownlist value based on a value in a Database
I have a page that allows users to create a user account. The aspx has various dropdowns that I populate by binding the dropdownlist to a dataset. OK so far? However, I also want a page where the user can edit their profile..so I need to be able to set the selected value based on what is saved in my user table and I also need to add the extra entries. Anyone know how I can do this easily. Do I need to join on my user table and my table that has all the various categories in order to get the selected value and add my other values?? Thanks, COG. For selecting the correct...

Dropdownlist selected value passes first value in list no matter what value is selected. why ?
 HI . I have a dropdownlist. Databinds ok but when I must pass a value as a Selected value it always passes the first value in the list even if I select another value. I tried to set the ddl to enableviewstate false but it doesn't change anything. Any ideas ? Code below, thanksLoading the ddl:       productCategories = CatalogAdmin.Admin_Product_Categories_NotIn(productId);        for (int i = 0; i < productCategories.Rows.Count; i++)        {       &...

Based on selected value from HTML.Dropdownlist, the values to be fetched
Hi,  I'm using Html.DropDownList.  When selection within this box changes, I need to retrieve the values from table based on the selected id(datavaluefield) of dropdownlist. Here my code ViewPost.cs viewpage <%=Html.DropDownList("CategoryID",ViewData["CategoryID"])%>  ---- based on selected value from the user the below table should fetch the value the db <table>        <tr>                   ...

How Select one dropdownlist value using down arrows corresponding other dropdownlist value are select
I am using two dropdownlist box.One is used for country selection. other one is used for state selection.I am using dropdown selectedindexchanged event. when i click country dropdownlist corresponding state is selected in state dropdownlist box. How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.R.Karthikeyan   kartmca: How select the country in country dropdownlist using down arrow keys at the same time corresponding state is select in state dropdown list.Generally, we don't implmenet...

How to set a dropdown value in a form based on a value read from a database?
We wish to populate a form based on information read from our database.One of the controls is a dropdown list. We first fill up the ddl from a table of about 60 values. Then we read a value from a different table that was saved data earlier to see which of the 60 values was picked. We wish to show the user that value in the dropdownlist when we display the form. How can we do this?For example, we fill the dropdown list with school course codes (French, German, Math, etc.) , then wish to read a different table in the database and select the dropdown item based on what we r...

adding a dropdownlist to a gridview and fill and then select the value on the database value
I am lost I want to add a dropdown list to a gridview (2005) fill the ddlist and then fill the gridview from a query and have the query value as the selected value by default.  hi !!Put your Dropdownlist in your <itemTemplate> and Fill it in GridView's RowDataBound Event..For that you need to find the DropDownList from that perticular Row by writing FindConrol Method of DataRow....LikeDropDownList DropDownList1 = (DropDownList)e.Row.FindControl("DropDownList1");ThanxHardik PatelThanx,Hardik Patel Hardik, you are right about putting the ddl in a templatefield.Howe...

VB.NET
I have a DataGrid (see code at end of my question) with several dropdownlist controls within TemplateColumns for Editing.  For the first 2 ddls in my grid, ddlMedType and ddlMedicationName, I would like to populate ddlMedicationName based on the selecteditem of ddlMedType, but I don't know how to reference this. Thank you for saving me from confusion!!!! Here is my DataGrid code: NOTE: I've bolded the 2 controls that I need help with -- the rest you can really ignore!! ================== <asp:datagrid id="dgMeds" runat="server" OnEditCommand="dgMeds_EditRow" OnUpdateComm...

DropDownList selected value determines value of second DropDownList
I have a quick question that I'm sure has been addressed before, but I can't seem to find a solution. I have two drop down list fields: startTime and endTime coded as such: <asp:DropDownList id="startTime" runat="server"> <asp:ListItem Value="00:00">12:00 A.M.</asp:ListItem> <asp:ListItem Value="00:30">12:30 A.M.</asp:ListItem> <asp:ListItem Value="01:00">1:00 A.M.</asp:ListItem> <asp:ListItem Value="...

Change Another Column Value based on Dropdownlist selected value while gridview onrowediting
Hi everyone. Another gridview question again. I have a gridview, with three columns, Code, Description and Action. The Code column, when I click on the Edit linkbutton on the Action column becomes a dropdown list. How can I do that when I selected a value from the dropdownlist, the value of the description on would be changed to the description corresponding to the dropdownlist? Thanks.   Hi:   From your description I think you can try: 1. Set AutoPostBack=true for your DropDownList. 2. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) &n...

Hiding a web form field based on database value.
  Hi, i want a textbox to be hidden based on the user access ID, and if the admin has clicked on a checkbox which makes it deactivated. I have the following; Code behind which displays all the fileds;private void DisplaySelectedRecord(DataView dv) { //populate the controls on the page to show the data on the page controls  this.txtMA_Code.Text = dv[0]["MA_Code"].ToString().Trim();   this.txtRate.Text = dv[0]["Rate"].ToString() ;if (dv[0]["Rate_Active"] != null && dv[0]["Rate_Active"] != System.DBNull.Value)this.CheckBox1...

dropdownlist's selected index value is not printing the selected value
Here iS my code .Aspx Page <asp:DropDownList ID="ddlkeyword" runat="server" Width="204px" DataTextField="Form" DataValueField="Form" OnSelectedIndexChanged="ddlkeyword_SelectedIndexChanged" >   </asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="Submit" Width="110px" OnClick="Button1_Click" /> my aspx.cs protected void Page_Load(object sender, EventArgs e) { fillddlkeyword(); }protected void Page_Init(object sender, EventArgs e) { ...

compare dropdownlist value to database value
I'm new to asp.net. I use a dropdownlist as a navigation menu. When i select one of the customers in the Customers dropdownlist, it will check whether the dropdownlist value is equal to the value in database. Then it will link to another web page, called Outlets.aspx which contains the Customers outlets information. Below is the code i use to bound data into dropdownlist. I have no idea how to compare the dropdownlist value to database value. Anyone can help?Dim cn1 As New OleDbConnection(strConn) Dim da1 As New OleDbDataAdapter("select * from Customers or...

Web resources about - DropDownList value selected base on database value? - asp.net.web-forms

Tag Synonyms - Stack Overflow
Q&A for professional and enthusiast programmers

Accidental entities - what about the UI?
This post is a follow-up to my previous blog post " Accidental entities - you don't need that identity ". In that post, we followed a consultant ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Air Astana - Join Now
Thank you for considering membership of Nomad Club – Kazakhstan’s first Frequent Flyer Program. You could soon be on your way to earning free ...

Languages -> C# Articles, Tutorials, Examples: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Blog - Avisra
Avisra Web Development Home Blog Services Contact The Blog Latest Module Builder: How can we make it better? Sitefinity by Chris on July 2, 2013 ...

Online Booking Tool Support
This Online Booking Tool Support page might help you as you navigate through the process of booking travel through your OBT provided by CWT. ...

@Html and beyond
... ); } return string .Format( “{0}{1}” , me.Member.Name, id); } The Html building methods are quite simple. Building a dropdownlist for a ...

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Unit Testing DateTime.Now Without Isolation
public virtual void SeedWithYearsSince(DropDownList list, int year){ for (int index = year; index

Resources last updated: 12/21/2015 3:59:28 PM