Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Index (zero based) must be greater than or equal to zero and less than

Status
Not open for further replies.

helloaspnet

Programmer
May 31, 2007
37
US
Hi
I searched the above problem in google but couldnt correlate in my case plz help



using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace product
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
SqlConnection con=new SqlConnection(ConfigurationSettings.AppSettings["sqlconnorthwind"]);
private void Page_Load(object sender, System.EventArgs e)
{

if(!IsPostBack)
{
con.Open();
String strselectcategory="select categoryname from categories";
SqlCommand cmd=new SqlCommand(strselectcategory,con);
DropDownList1.DataSource=cmd.ExecuteReader();
DropDownList1.DataTextField="categoryname";
DropDownList1.DataBind();
con.Close();
}

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
con.Open();
String strddlselect="select productid, productname from products join categories on products.categoryid=categories.categoryid where categoryname=@cat";
SqlCommand cmd=new SqlCommand(strddlselect,con);
cmd.Parameters.Add("@cat",DropDownList1.SelectedItem.Text);
CheckBoxList1.DataSource=cmd.ExecuteReader();

CheckBoxList1.DataTextField="productname";
CheckBoxList1.DataValueField="productid";
CheckBoxList1.DataBind();
con.Close();

}

private void Button1_Click(object sender, System.EventArgs e)
{
ArrayList prodid=new ArrayList();
//String[] prodid=new String[10];
for(int i=0;i<CheckBoxList1.Items.Count;i++)
{
if(CheckBoxList1.Items.Selected)
{
prodid.Add(CheckBoxList1.Items.Value);
//Label1.Text=CheckBoxList1.Value .ToString();

}
}
DataGrid2.DataSource=prodid;
DataGrid1.DataBind();

// for(int i=0;i<prodid.Count;i++)
// {
// if(prodid.Count>0)

// {


con.Open();
String selpricename="select productname,unitprice from products where productid in(@pid)";
SqlCommand cmd=new SqlCommand(selpricename,con);
SqlParameter sqp=new SqlParameter("@pid",SqlDbType.Int);
cmd.Parameters.Add("@pid",prodid.ToArray());


SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
DataSet ds=new DataSet();
da.Fill(ds);


DataGrid1.DataSource=ds;
DataGrid1.DataBind();


con.Close();
//dr.Close();
// }
//
//
//
// }

}
}
}



helloaspnet
 
Code:
for(int i=0;i<CheckBoxList1.Items.Count;i++)
At a guess, I'd say that the problem is here. As the index is zero based I'd say that the loop should go to "CheckBoxList1.Items.Count - 1". However, a better method would be to use an IEnumerator e.g.
Code:
        Dim ienum As IEnumerator = CheckBoxList1.Items.GetEnumerator
        While ienum.MoveNext
            prodid.Add(CType(ienum.Current, ListItem).Value)
        End While


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top