Click to See Complete Forum and Search --> : ASP.NET GridView


vamsi_vajjah
May 9th, 2009, 02:22 AM
Hi All,
I get the error "System.ArgumentException: Value does not fall within the expected range."
when i try to add data to the asp.net gridview.
The following is the complete code


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("SNo", typeof(int));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Amount", typeof(int));
dt.Columns.Add("Nature of Recipts", typeof(string));
dt.Columns.Add("Self Cheque No",typeof(int));
dt.Columns.Add("Status", typeof(string));
dt.Columns.Add("Remarks/Narration", typeof(string));

SPSite site = new SPSite("http://moss:45939");
SPWeb web = site.AllWebs["Second Site"];
SPList list = web.Lists["Daily Cash"];
//SPListItem item = list.Items[0];
DataRow row;
foreach (SPListItem item in list.Items)
{
row = dt.Rows.Add();
row["SNo"] = item["SNo"].ToString() ;
row["Date"] = item["Date"].ToString() ;
row["Amount"] = item["Amount"].ToString() ;
row["Nature of Recipts"] = item["Nature of Recipts"].ToString() ;
row["Self Cheque No"] = item["Self Cheque No"].ToString() ;
row["Status"] = item["Status"].ToString() ;
row["Remarks/Narration"] = item["Remarks/Narration"].ToString() ;

}

GridView1.DataSource = dt;
GridView1.DataBind();
}
}

Hoping to get a reply soon.

dannystommen
May 9th, 2009, 04:58 AM
Hi All,
I get the error "System.ArgumentException: Value does not fall within the expected range."


row = dt.Rows.Add();
row["SNo"] = item["SNo"].ToString() ;
row["Date"] = item["Date"].ToString() ;
row["Amount"] = item["Amount"].ToString() ;
row["Nature of Recipts"] = item["Nature of Recipts"].ToString() ;
row["Self Cheque No"] = item["Self Cheque No"].ToString() ;
row["Status"] = item["Status"].ToString() ;
row["Remarks/Narration"] = item["Remarks/Narration"].ToString() ;


My guess is that exceptions happens in this part of the code. Probably one (or more) key you use to retrieve the data is incorrect. Use the debugger, step through the code, and take a look at what line the exception is thrown.

eclipsed4utoo
May 12th, 2009, 03:27 PM
Your problem could be that you have Columns with datatypes of DateTime and INT, yet you are assigning all the values as strings.


row = dt.Rows.Add();
row["SNo"] = int.Parse(item["SNo"].ToString());
row["Date"] = DateTime.Parse(item["Date"].ToString());
row["Amount"] = int.Parse(item["Amount"].ToString());
row["Nature of Recipts"] = item["Nature of Recipts"].ToString() ;
row["Self Cheque No"] = int.Parseitem["Self Cheque No"].ToString());
row["Status"] = item["Status"].ToString() ;
row["Remarks/Narration"] = item["Remarks/Narration"].ToString() ;