Hi Assimalyst, I have just noticed that you have been given a solution
creating ListItems manually, if you still want to use databinding you can try
this,
HTH jd
=============== == into aspx=========== =======
<body>
<form id="Form1" method="post" runat="server">
<asp:dropdownli st id="ddlCountry " runat="server"> </asp:dropdownlis t></form>
</body>
=============== = end aspx =============== =======
=============== =begin code behind========= ========
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Configur ation;
using System.Data;
using System.Data.Sql Client ;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
namespace csharp
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Drop DownList ddlCountry;
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
if(!Page.IsPost Back )
{
bindDropDown();
}
}
private void bindDropDown()
{
//
// get our datareader
//
IDataReader dr = DataAccess.Coun tryDB.GetCountr yReader();
//
// bind our drop down
//
ddlCountry.Data Source = dr;
ddlCountry.Data TextField = "CountryNam e";
ddlCountry.Data ValueField = "CountryNo" ;
ddlCountry.Data Bind ();
//
// close our datareader - note this will also close the database
connection
// because we used ocmd.ExecuteRea der(CommandBeha vior.CloseConne ction)
// when we opened it
//
dr.Close();
}
}
//
// ideally move these classes into a different .cs file / assembly
// so the functionality can be reused in different apps / pages
//
namespace DataAccess
{
//
//class for accessing data relating to country
//
public class CountryDB
{
public static IDataReader GetCountryReade r()
{
//should ideally use a stored procedure here
SqlCommand ocmd = new SqlCommand
("Your Select Query Here",
DBConnection.Ge tDBConnection() );
return ocmd.ExecuteRea der(CommandBeha vior.CloseConne ction) ;
}
}
//
// helper class to return a database connection
//
public class DBConnection
{
public static SqlConnection GetDBConnection ()
{
//
//ideally use configuration or other more secure store for
connectionstrin g
//
//SqlConnection oconn = new
SqlConnection(C onfigurationSet tings.AppSettin gs("DBConnectio nString")) ;
SqlConnection oconn = new SqlConnection(" Your Connection String Here");
oconn.Open();
return oconn;
}
}
}
}
=============== ==end codebehind===== =============== ==
"london calling" wrote:
if you take the try catch block out does it throw an error?
"Assimalyst " wrote:
Thanks for the ideas,
Removed the datamember, made no difference. Still getting the same
error.
Any more?