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.
Prashant
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>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebPart1 runat=server></{0}:WebPart1>"),
XmlRoot(Namespace="Lession")]
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;
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("Text"),
Description("Text Property")]
public string Text
{
get
{
return text;
}
set
{
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";
this.Controls.Add(labelSearch);
drpName = new DropDownList();
drpName.Width = new Unit(100);
drpName.Load += new EventHandler(this.Populate);
drpName.SelectedIndexChanged+= new EventHandler(this.DropDownTextChanged);
drpName.AutoPostBack=true;
drpName.Items.Insert(0, "Select Project Name");
drpName.SelectedIndex = 0;
this.Controls.Add(drpName);
labelResults = new Label();
this.Controls.Add(labelResults);
}
public void Populate (object sender, EventArgs e)
{
ListItem li= new ListItem("Hi","Bye");
drpName.Items.Add(li);
}
public void DropDownTextChanged (object sender, EventArgs e)
{
this.Title =connectString + drpName.SelectedItem.Text.ToString();
}
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(SPEncode.HtmlEncode(Text));
RenderChildren(output);
}
}
}