.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Populating DropDownList from Database

Posted By:      Posted Date: September 18, 2010    Points: 0   Category :ASP.Net
Hi there,I'm really struggling with this tonight (this being exactly what it says on the tin, (or the subject!)).  Code I'm working with right now is:If Not Page.IsPostBack Then Dim Category As String Dim CategoryReader As OdbcDataReader = Database.Read("SELECT * FROM tblBlogCategory") While CategoryReader.Read() Category = CategoryReader("Category").ToString() End While DropDownList1.DataSource = Category DropDownList1.DataBind() End IfI've also tried things likeDropDownList1.DataSource = Database.Read("SELECT * FROM tblBlogCategory") but it just stubbornly refuses to populate the list (i.e. the pagealways loads with a blank drop down list).  An idea I'm playing around with is putting the values of the Category table into an array, but it seems to have a problem with that as well.  Hope someone can advise me and point me in the right direction.  Feel free to be critical of my code too.ThanksDouglas

View Complete Post

More Related Resource Links

dynamically populating the database in the dropDownlist using c# in asp.net


hi everyone , plz listen my question very carefully

i have 2 tables  Desgn & EmpForm

Desgn table is lik this

CREATE TABLE [dbo].[Desgn](
    [DesignationCode] [int] IDENTITY(1,1) NOT NULL,
    [DesignationName] [varchar](50) NOT NULL,
    [DesignationCode] ASC

EmpForm table is like this

CREATE TABLE [dbo].[Empform](
    [EmpId] [varchar](50) NOT NULL,
    [EmpName] [varchar](50) NOT NULL,
    [EmpSalary] [numeric](18, 0) NULL,
    [EmpDesignationcode] [numeric](18, 0) NOT NULL,
    [EmpDateofBirth] [date] NOT NULL

now my ques is i want to dynamically populate the dropdown list of designation on the basis of the code but instead of code name should get populated .i have written the code like this 

public void Populate1()
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["co

Filling state dropdownlist according to country dropdownlist from database

Hello,I've been looking for an answer to this problem of mine but not yet able to get any solution.Please help me out. I'm having 2 dropdownlists, one for country and another for state.I want the states to be filled according to the country selected from the country ddl. I'm having:  Two dropdownlist controls as-ddlCountry and ddlState Two database tables as-Country and State Country table has fields named- C_ID(pk) and Country State table fields are-S_ID(pk), State, C_ID During page load, I'm able to bind the ddlCountry to the "Country" table in  databaseHence I could bind ddlState too  But I'm getting confused, how to bind the ddlState control so that it will show state as per country selected. I don't want to use ajax Please help me out. Thanks and Regards, Zinnia

how to display the database value in to the Dropdownlist box


how to create dropdownlist user control(acsx) and fill from database


how to create dropdownlist user control(acsx) and fill from database

Populating DropDownList in FormView from Code Behind


On my formview I have a drop down list that is populated with the names of the files in a certain directory. This works fine, but when the drop down list selected value is bound to the database field I get the following error :

'ImageFileDropDownList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

I have setup other drop down lists in the same formview populated from SqlDataSources and these all work fine. I can see perhaps why this is an issue because I have not specified a DataValueField or a DataTextField for this drop down that is populated in code (whereas I can for the SQL bound ones) because I'm not sure what these settings would be.

I need to have a "blank" option as well so that the field does not default to the first value in the list.

My code (I have stripped out some meaningless controls) :

<asp:FormView ID="FormView1" runat="server" DataKeyNames="BannerID" DataSourceID="SqlDataSource2" OnItemDeleted="FormView1_ItemDeleted">
    <table width="100%" cellspacing="0" cellpadding="4">                          

Populating dropdownlist from session variable


Hi all,

To speed things up for the visitor of my web page, I put a personalised dropdownlist in a session variable (as below).

The idea is that if the user is logged in and the ddl session variable is Nothing then it should be set here, and the next times the dropdownlist should be set from this session variable.

The session seems to be populated ok, but the resulting dropdownlist (ddlMenu) doesn't (it just becomes blank). I suspect that it has soemething to do with the way I try to assign it. No matter what I try I run into problems, saying that one thing is a one-dimensional array and the other thing a listitemcollection, or the like. What should I write in the last line?

        If Session("aid") IsNot Nothing And Session("ddl") Is Nothing Then
            Dim ddlny As New DropDownList

            ddlny.Items.Add(New ListItem(vstr, ""))
            ddlny.Items.Add(New ListItem(kontrstr, "PageA.aspx"))
            ddlny.Items.Add(New ListItem(bstr, "PageB.aspx"))

DropDownList, SelectedValue, From Database


I have a form, using a few DropDownLists.  The DropDownList pulls the list of items from a Table in the database. But, I would like it to select the value from the UserProfile Table, also in the database.  It populates correctly, but it is not selecting the value correctly.  The data from the UserProfile Table will be a string, not an ID column.

Here is the code I am using:



<asp:Label ID="lblSportName" runat="server" Text="Sport:"></asp:Label>
                    <asp:DropDownList ID="ddSportName" runat="server" DataSourceID="edsSports" DataTextField="SportName" DataValueField="SportID" Width="80%" ></asp:DropDownList>
                    <asp:EntityDataSource ID="edsSports" runat="server" 
                        ConnectionString="name=BFEntities" DefaultContainerName="BFEntities" 
                        EnableFlattening="False" EntitySetName="Sports" >


var query = (
                                                from c in context.aspnet_Users.OfType<UserProfile>()
                                                where c.UserId == _userGuid
                                                select c

Populating text box using SQL database.


So this seems like it should be easier than it has been for me.  I'm very new to ASP, so perhaps that's the reason.  I've searched the forum and found a couple previous postings that dealt with a similar issue to this, but I got nowhere fast with it. 

So here's what I've got.

I've created a website.  The user signs into that website, and they goto a Companies.aspx form.  Once in the companies.aspx, there are some fields where you can enter in a companie's name, number of clients with that company, and the company's website.  It's real simple so far. 

Once they finish that, they hit at a button called "Add Company"  This then adds a company to the sql database.   Just beneath this, I have a Gridview in which I display all of that user's companies that they have entered.

I've had no problem with getting any of this set up.  It's working great.  However, what I want to happen is to use the select function of the gridview to then re-populate the empty fields with the data that coresponds to the selected company in my gridview.  Then I will have an update button that will update that entery in my database.  I realize there is a quick edit function in the Gridview, but I dont like the way it looks. 

Now I've downloaded the employee info starter kit off of a

Multiple database support with Entity Framework

One of the features introduced in Entity Framework is being database independent. Which mean each database provider can support Entity Framework by implementing its provider.

This feature allows you build applications independent from the underplaying database provider. In this post I'm going to show how to build an application that support multiple databases using same conceptual model made by Entity Framework.

Export Visio Database Table Names to Excel

If you use the Enterprise Architect edition of Microsoft Visio for data modeling regularly, then there is a good chance that at some point you've wanted to export just the table names into Excel. You might want to do this to map logical ERD entities to physical data model tables, track project status by entity, or track overlap between database versions.

MS SQL Server: Disconnect Users From Database - Kill User Session

If you ever wanted to restore your database from a SQL backup file (.bak), but there are still users connected to your database, the backup operation will fail causing the error: Exclusive access could not be obtained because the database is in use.

jQuery: Styling DropDownList Alternating Items (Rows)

In the following example, I will show how you can style any asp:dropdownlist very easily.

The example demonstrates how you can change alternating row items background color, using the fadeTo() to add a nice touch in the dropdownlist. You can change alt rows with a couple lines.

Publishing SQL Server Database using Publishing Wizard : Tips & Tricks

We can use SQL Server Publishing Wizard to deploy our local Database to remote hosting server/production server.This feature is available in SQL Server 2008/VWD 2008.

Below is the complete step by step guide of this process.

Adding Rows in GridView without using a Database

This article describes on how to add rows in GridView without using a database. Basically the GridView will be populated with data based on the values entered in the TextBox on Button Click and retain the GridView data on post back.

Adding Multiple Columns and Rows in GridView without using a Database

This article describes on how to add multiple columns and rows in GridView without using a database. Basically the GridView will be populated with data based on the values entered in each TextBoxes on Button Click and retain the GridView data on post back.

ASP.NET Database Tutorial

This tutorial will show you how to display data using the .NET GridView Control, ASP.NET 2.0 and VB.NET

The GridView control is a powerful tool and is simple to implement.

First, you will need to import the System.Data.SqlClient namespace.

Working with DropDownList and ListBox Controls in ASP.NET

"In my application i am using dropdownlist to display all categories from a table. When the user clicks this control I display all records in that category. Category table also contains column called color.

I wanted display some categories in dropdown list different color and others in different color using ASP.NET."-Anonymous
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend