Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Populate dropdownlist from sql server 2000 database

  Asked By: Rusty    Date: Sep 17    Category: Sharepoint    Views: 1303

I am creating a web part on which I have to populate dropdownlist from sql
server 2000 database
I have created the web part, the code I am sending with this mail.
Please send me where to place the database handling code
I have added an element to the dropdownlist by hardcoding, It worked, but while
connecting to database it wont work.
Code is as follows:

using System;

using System.ComponentModel;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.WebPartPages;

namespace Lession


/// <summary>

/// Description for WebPart1.

/// </summary>


ToolboxData("<{0}:WebPart1 runat=server></{0}:WebPart1>"),


public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart


private const string defaultText = "";

private string text = defaultText;

private string connectString ="Data Source=xyz;Initial Catalog=abc;user='sa';" ;

private Label labelSearch = null;

private DropDownList drpName = null;

// private Button buttonSearch = null;

private Label labelResults = null;






Description("Text Property")]

public string Text




return text;




text = value;



/// <summary>

/// This method gets the custom tool parts for this Web Part by overriding the

/// GetToolParts method of the WebPart base class. You must implement

/// custom tool parts in a separate class that derives from

/// Microsoft.SharePoint.WebPartPages.ToolPart.

/// </summary>

///<returns>An array of references to ToolPart objects.</returns>

// public override ToolPart[] GetToolParts()

// {

// ToolPart[] toolparts = new ToolPart[2];

// WebPartToolPart wptp = new WebPartToolPart();

// CustomPropertyToolPart custom = new CustomPropertyToolPart();

// toolparts[0] = wptp;

// toolparts[1] = custom;

// return toolparts;

// }

/// <summary>

/// Render this Web Part to the output parameter specified.

/// </summary>

/// <param name="output"> The HTML writer to write out to </param>

protected override void CreateChildControls()


labelSearch = new Label();

labelSearch.Text = "Select Project Name";


drpName = new DropDownList();

drpName.Width = new Unit(100);

drpName.Load += new EventHandler(this.Populate);

drpName.SelectedIndexChanged+= new EventHandler(this.DropDownTextChanged);


drpName.Items.Insert(0, "Select Project Name");

drpName.SelectedIndex = 0;


labelResults = new Label();



public void Populate (object sender, EventArgs e)


ListItem li= new ListItem("Hi","Bye");



public void DropDownTextChanged (object sender, EventArgs e)


this.Title =connectString + drpName.SelectedItem.Text.ToString();


protected override void RenderWebPart(HtmlTextWriter output)









1 Answer Found

Answer #1    Answered By: Jocelyn Scott     Answered On: Sep 17

I don't see any database  loading code so I'm not sure what you mean by
"it wont work". Connecting to a database from a Web Part is no different
than connecting like any app using ADO.NET.

Where I would put it would be in either CreateChildControls or in the
OnLoad event. Note that after a postback these values are nulled out and
reloaded again so check for that so you're not always going back to the
database layer everytime someone changes the dropdown value.

Theres an example of populating a dropdown from SQL here:


Didn't find what you were looking for? Find more on Populate dropdownlist from sql server 2000 database Or get search suggestion and latest updates.