473,739 Members | 7,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataBind to DropDownList Problem "The IListSource does not contain any data sources"

Hi,

I'm attempting to use a data reader to load data from a single table,
tblCountry, with two columns, countryNo (the Key) and countryName, into
a DropDownList box. Here's the code:
protected System.Data.Sql Client.SqlConne ction conn;
protected System.Data.Sql Client.SqlDataR eader drCountry;

private void Page_Load(objec t sender, System.EventArg s e)
{
string SQL = "SELECT * FROM tblCountry";

SqlConnection conn = new
SqlConnection(" Server=(local); Database=YLCdbS QL;Integrated
Security=SSPI") ;
SqlCommand countryLoad = new SqlCommand (SQL, conn);
SqlDataReader drCountry = null;

try
{
conn.Open();
drCountry = countryLoad.Exe cuteReader();

countryDropLst. DataSource = drCountry;
countryDropLst. DataMember = "countryNam e";
countryDropLst. DataTextField = "countryNam e";
countryDropLst. DataValueField = "countryNo" ;
countryDropLst. DataBind();
}
finally
{
drCountry.Close ();
conn.Close();
}
}

But when I run it I get the error "The IListSource does not contain any
data sources", and the DataBind line is highlighted.

I have checked the table using Enterprise Manager, it exists and has
numerous entries in both columns.

Any idea what the problem might be?

Thanks.

Nov 19 '05 #1
10 5336
you set your datareader to null... should be countryLoad.Exe cuteReader()

"Assimalyst " wrote:
Hi,

I'm attempting to use a data reader to load data from a single table,
tblCountry, with two columns, countryNo (the Key) and countryName, into
a DropDownList box. Here's the code:
protected System.Data.Sql Client.SqlConne ction conn;
protected System.Data.Sql Client.SqlDataR eader drCountry;

private void Page_Load(objec t sender, System.EventArg s e)
{
string SQL = "SELECT * FROM tblCountry";

SqlConnection conn = new
SqlConnection(" Server=(local); Database=YLCdbS QL;Integrated
Security=SSPI") ;
SqlCommand countryLoad = new SqlCommand (SQL, conn);
SqlDataReader drCountry = null;

try
{
conn.Open();
drCountry = countryLoad.Exe cuteReader();

countryDropLst. DataSource = drCountry;
countryDropLst. DataMember = "countryNam e";
countryDropLst. DataTextField = "countryNam e";
countryDropLst. DataValueField = "countryNo" ;
countryDropLst. DataBind();
}
finally
{
drCountry.Close ();
conn.Close();
}
}

But when I run it I get the error "The IListSource does not contain any
data sources", and the DataBind line is highlighted.

I have checked the table using Enterprise Manager, it exists and has
numerous entries in both columns.

Any idea what the problem might be?

Thanks.

Nov 19 '05 #2
sorry ignore last post (i keep doing that) don't set the datamember

"london calling" wrote:
you set your datareader to null... should be countryLoad.Exe cuteReader()

"Assimalyst " wrote:
Hi,

I'm attempting to use a data reader to load data from a single table,
tblCountry, with two columns, countryNo (the Key) and countryName, into
a DropDownList box. Here's the code:
protected System.Data.Sql Client.SqlConne ction conn;
protected System.Data.Sql Client.SqlDataR eader drCountry;

private void Page_Load(objec t sender, System.EventArg s e)
{
string SQL = "SELECT * FROM tblCountry";

SqlConnection conn = new
SqlConnection(" Server=(local); Database=YLCdbS QL;Integrated
Security=SSPI") ;
SqlCommand countryLoad = new SqlCommand (SQL, conn);
SqlDataReader drCountry = null;

try
{
conn.Open();
drCountry = countryLoad.Exe cuteReader();

countryDropLst. DataSource = drCountry;
countryDropLst. DataMember = "countryNam e";
countryDropLst. DataTextField = "countryNam e";
countryDropLst. DataValueField = "countryNo" ;
countryDropLst. DataBind();
}
finally
{
drCountry.Close ();
conn.Close();
}
}

But when I run it I get the error "The IListSource does not contain any
data sources", and the DataBind line is highlighted.

I have checked the table using Enterprise Manager, it exists and has
numerous entries in both columns.

Any idea what the problem might be?

Thanks.

Nov 19 '05 #3
Thanks for the ideas,

Removed the datamember, made no difference. Still getting the same
error.

Any more?

Nov 19 '05 #4
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?

Nov 19 '05 #5
Yep, same error.

Nov 19 '05 #6
> protected System.Data.Sql Client.SqlConne ction conn;
protected System.Data.Sql Client.SqlDataR eader drCountry;

private void Page_Load(objec t sender, System.EventArg s e)
Be sure to add the following

if (!this.IsPostBa ck)
{

{
string SQL = "SELECT * FROM tblCountry";

SqlConnection conn = new
SqlConnection(" Server=(local); Database=YLCdbS QL;Integrated
Security=SSPI") ;
SqlCommand countryLoad = new SqlCommand (SQL, conn);
try
{
conn.Open();
drCountry = countryLoad.Exe cuteReader(); //Try the following:
while(drCountry .Read())
{
ListItem newListItem = new ListItem();

newListItem.Val ue = drCountry.GetVa lue(0).ToString ();
newListItem.Tex t = drCountry.GetVa lue(2).ToString ();
// Add Items to DropDownAwardTy pe DropDownList Web Control
countryDropLst. Items.Add(newLi stItem);
}

//countryDropLst. DataSource = drCountry;
//countryDropLst. DataMember = "countryNam e";
//countryDropLst. DataTextField = "countryNam e";
//countryDropLst. DataValueField = "countryNo" ;
//countryDropLst. DataBind();
}
finally
{
drCountry.Close ();
conn.Close();
}
}


}

Nov 19 '05 #7
Moojjoo, thankyou so much. I have been struggling with this for far too
long now, at last it works!! :)

Nov 19 '05 #8
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?

Nov 19 '05 #9
No problem. Now if I could just get an answer to my problem... AGH!!!

Getting values from a user control that was added to a datagrid. This is
driving me batty...

"Assimalyst " wrote:
Moojjoo, thankyou so much. I have been struggling with this for far too
long now, at last it works!! :)

Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
6903
by: lkrubner | last post by:
I want to give users the power to edit files from an easy interface, so I create a form and a PHP script called "fileUpdate". It does a reasonable about of error checking and prints out some errors. It uses fileperms() to get the permissions of the file, and it includes that info in any error message. Today I'm getting the following error message. I've used SmartFtp to go in and set the test file's permissions to 777, but in this error...
14
1909
by: mshetty | last post by:
Hi, I get an error "Warning: b::a_method hides the virtual function a::a_method()." on compiling the following code.. #include <iostream.h> class a {
12
25465
by: Unbiased_me | last post by:
Hi There I recently read in book that the C compiler is written in C. I unable to comprehend the concept behind this. How is the compiler design started...Where dodoes one start. I tried Googling for the answer that C is written in C, however the web search proved futile. C # and .net is written in .net...What!, and the list goes on
145
6317
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two programs: /*** a.c ***/
0
1989
by: huobazi | last post by:
I have many dropdownlist controls in my ascx (and use LoadControl in a aspx fiel) file,so i write a method "InitList(DropDownList list,string strsql,string TextField,string ValueField)" but when i want to get the BigClassList.SelectedItem.Text and BigClassList.SelectedItem.Value in a button onclick method,btnSmallClassEdit_Click(....),there post an error " System.NullReferenceException: 佫¶ÔÏóÒýÓÃÉèÖõ½¶ÔÏóµÄʵÀý¡£", I Don't know the...
8
1806
by: Lars-Erik Aabech | last post by:
Hi! We've got an ASP.NET application that runs swell on development PC's and one live production server. Another prod. server though crashes a couple of times a week. Either something that should be bound reports that column X isn't found, or that the IListSource does not contain any datasources. We get the solution up again by restarting aspnet_wp. This may happen if aspnet_wp is at 40 mb or 400mb, so it's not memory related. The SQL...
8
7050
by: olrt | last post by:
Hello, I plan to install Visual C# 2005 Express. I need a source code control system. It seems that there's no express version of Visual Source Safe. What should I do ? Is Visual C# 2005 Express "compatible" with open source CVS system ?? Thanks !! Olivier.
2
3664
by: Martin Kulas | last post by:
Hallo! I have a problem with Py_BuildValue: I want to convert an unsigned int to a PyObject *. http://docs.python.org/api/arg-parsing.html says that I can use "I" as a format string. But it does not work :-\ Here is my simplified code:
3
3211
by: dvanderwel | last post by:
I have a .NET 1.1 web page with a DataGrid containing a TemplateColumn that contains a DropDownList. I need to fire an event when the selected item in the DropDownList changes for the current DataGrid row, and I'm having trouble figuring out how to do this. Other control types I have defined the "CommandName" property in the aspx file. The event returns the index of the row that contains the control, and I can thus pull more data out...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.