473,394 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Binding a GridView Programmatically

I've seen how to set up a gridview at design time, but I believe I need to do
this and run time and cannot figure it out. I am sending a concatenated list
of IDs that is gets bigger each time a user enters another ID. The code to
get the data back is below and it works and I am returning an ArrayList. I
get an error when I bind the gridview control:

-- A field or property with the name 'ItemName' was not found on the
selected data source. --

I have the grid defined with one column, named "ItemName". Can someone tell
me what I am doing wrong and show me where to fix it? Thank you.

//Code in the code behind page that is calling the method below it.
gridview1.DataSource = Media.Lookup((ArrayList)Session["Media"]);
gridview1.DataBind();
public static ArrayList Lookup(ArrayList mediaID)
{
string mediaIDs = string.Empty;

for (int x = 0; x <= mediaID.Count - 1; x++)
{
mediaIDs += mediaID[x].ToString() + ",";
}
mediaIDs = mediaIDs.Substring(0, mediaIDs.Length - 1);

using (SqlConnection conn = new SqlConnection(connectionString))
{

using (SqlCommand command = new SqlCommand("GetMedia", conn))
{
command.CommandType = System.Data.CommandType.StoredProcedure;

SqlParameter param;
param = command.Parameters.Add("@inventoryIDs",
System.Data.SqlDbType.VarChar,
50);
param.Value = mediaIDs;

SqlDataReader dataReader;
conn.Open();
dataReader = command.ExecuteReader();
ArrayList titles = new ArrayList();
while (dataReader.Read())
{
titles.Add(dataReader["ItemName"].ToString());
}

return titles;
}
}

Nov 5 '06 #1
1 10576
Instead of databinding to an ArrayList, do directly to the datareader:

gridview1.DataSource = dataReader;

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Mike Collins" <Mi*********@discussions.microsoft.comwrote in message
news:F2**********************************@microsof t.com...
I've seen how to set up a gridview at design time, but I believe I need to
do
this and run time and cannot figure it out. I am sending a concatenated
list
of IDs that is gets bigger each time a user enters another ID. The code to
get the data back is below and it works and I am returning an ArrayList. I
get an error when I bind the gridview control:

-- A field or property with the name 'ItemName' was not found on the
selected data source. --

I have the grid defined with one column, named "ItemName". Can someone
tell
me what I am doing wrong and show me where to fix it? Thank you.

//Code in the code behind page that is calling the method below it.
gridview1.DataSource = Media.Lookup((ArrayList)Session["Media"]);
gridview1.DataBind();
public static ArrayList Lookup(ArrayList mediaID)
{
string mediaIDs = string.Empty;

for (int x = 0; x <= mediaID.Count - 1; x++)
{
mediaIDs += mediaID[x].ToString() + ",";
}
mediaIDs = mediaIDs.Substring(0, mediaIDs.Length - 1);

using (SqlConnection conn = new SqlConnection(connectionString))
{

using (SqlCommand command = new SqlCommand("GetMedia", conn))
{
command.CommandType = System.Data.CommandType.StoredProcedure;

SqlParameter param;
param = command.Parameters.Add("@inventoryIDs",

System.Data.SqlDbType.VarChar,
50);
param.Value = mediaIDs;

SqlDataReader dataReader;
conn.Open();
dataReader = command.ExecuteReader();
ArrayList titles = new ArrayList();
while (dataReader.Read())
{
titles.Add(dataReader["ItemName"].ToString());
}

return titles;
}
}

Nov 6 '06 #2

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

Similar topics

0
by: rob | last post by:
Here is my scenario: One of my aspx pages has a CheckBoxList (Item1, Item2) and a GridView. Then I have a database that has the columns Item1 and Item2 (among others) with the data type bit....
5
by: sck10 | last post by:
Hello, I have a GridView that is using the following to connect to a SQL Server 2000 stored procedure: <asp:SqlDataSource ID="dsWebDocList" SelectCommand="sp_web_WebDocument"...
5
by: Brian McClellan | last post by:
Just wondering if anyone has a simple example of creating a gridview completely programmatically, i'm not doing anything terribly sophisticated. When creating the gridview declaratively evertying...
0
by: bsaran | last post by:
Hi, I have a code that sort of works. The event flow is as follows: In a user control of type "FilterControl" user selects certain values from 3 related dropdown lists. (two of them have auto...
0
by: Mike P | last post by:
I am using a GridView with 3 SelectParameters 2 of which are ControlParameters, but the third of my SelectParameter values is derived through my code rather than a QueryString, Control etc, so I...
2
by: michel | last post by:
Hi, there is a gridview with a Commandfield. I would like to set the 'ShowEditButton' on 'true' programmatically. I tried two ways without succes: with "'Dim cf As CommandField with "'Dim cf...
4
by: Tomasz | last post by:
Hello Developers, Here is interesting problem I just came across: how do I wire a GridView control programmatically? Here is my sample code using Object Data Source: protected void...
0
by: Sobin Thomas | last post by:
Hi All, How can I bind the Gridview control to Sql Datasource control on a button click(I see majority of the articles binding datasource at page load) I need to enable the paging and sorting of...
1
by: Sobin Thomas | last post by:
Hi All, How can I bind the Gridview control to Sql Datasource control on a button click(I see majority of the articles binding datasource at page load) I need to enable the paging and sorting of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.