Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 3rd, 2009, 01:25 PM
Authorized User
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Default Data Reader

Hi...i am using data reader to search the company name,contact name and phone by using customerID....but there is an error....see down for the error...i highlight it


protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conCust;
SqlCommand cmdSelect;
SqlDataReader dtrCustomer;
string connStr = ConfigurationManager.ConnectionStrings["ConnectionCust"].ConnectionString;
conCust = newSqlConnection(connStr);
conCust.Open();
cmdSelect = newSqlCommand("Select CustomerID From Customers", conCust);
dtrCustomer = cmdSelect.ExecuteReader();
while (dtrCustomer.Read())
{

Label2.Text = Convert.ToString(dtrCustomer["CompanyName"]);\\error:index out of range exception)
Label3.Text = Convert.ToString(dtrCustomer["ContactName"]);error:index out of range exception)
Label4.Text = dtrCustomer["Phone"].ToString();error(index out of range exception)
}
dtrCustomer.Close();
conCust.Close();
}

why index out of range exception?....i did not declare any array....
 
Old July 3rd, 2009, 01:58 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You are only selecting the CustomerID field in your command statement

cmdSelect = newSqlCommand("Select CustomerID From Customers", conCust);

Therefore, CompanyName, ContactName, and Phone are not contained in the reader.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
Banishah (July 3rd, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter6.1.3 :Data Reader Source johe BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 0 November 28th, 2008 04:35 AM
Data reader checking.... janees ASP.NET 1.0 and 1.1 Professional 1 March 11th, 2007 06:09 AM
oracle data reader silvia C# 10 February 2nd, 2006 07:30 AM
Fill data set from data reader sunil menghani ADO.NET 3 March 29th, 2005 07:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.