This exception occurs when you call the FindBytotalcategory2, so I check this method from your souce code. I notice you use the this.Rows.Find method in the FindBytotalcategory2 method. This method, namely DataRowCollection.Find method gets the row that
contains the specified primary key values. That means you need to provide an object arrary which match the primary columns of your table. To reproduce your issue I create this simple sample code different with yours:
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Rows.Add("John", "Smith");
dt.Rows.Add("Spotty", "Bowles");
dt.Rows.Add("Super", "Man");
DataView view = dt.DefaultView;
view.Sort = "Col1 ASC, COL2";
////Colum order include both columns
object vals = "John";
//Dim vals(1) As Object
//vals(0) = "John"
//vals(1) = "Smith"
try {
int i = view.Find(vals);
if (i > -1) {
Response.Write(view[i]["Col1"] + view[i]["Col2"]);
} else {
Response.Write("Not match Found");
}
} catch (Exception ex) {
Response.Write(ex.Message);
}
When I modify my sample code like below, all work fine, please corret your source code with my comments:
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Rows.Add("John", "Smith");
dt.Rows.Add("Spotty", "Bowles");
dt.Rows.Add("Super", "Man");
DataView view = dt.DefaultView;
view.Sort = "Col1 ASC, COL2";
////Colum order include both columns
object[] vals = new object[2];
vals[0] = "John";
vals[1] = "Smith";
try {
int i = view.Find(vals);
if (i > -1) {
Response.Write(view[i]["Col1"] + view[i]["Col2"]);
} else {
Response.Write("Not match Found");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Please mark the replies as answers if they help or unmark if not.
Feedback to us
micnie2020
Member
308 Points
539 Posts
Expecting 1 value(s) for the key being indexed, but received 2 value(s).
Apr 24, 2012 10:29 AM|LINK
Hi All,
I getting error on bold section, error raised: Expecting 1 value(s) for the key being indexed, but received 2 value(s).
DataSet1.TotalRevenueByMonthRow totalPendingNew = (
DataSet1.TotalRevenueByMonthRow)dataSet1.TotalRevenueByMonth.FindBytotalcategory2(Helpers.TotalCategory.PNEWBUSINESS, productFamily);
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public TotalRevenueByMonthRow FindBytotalcategory2(int totalcategory, System.Guid[] productfamily)
{
return ((TotalRevenueByMonthRow)(this.Rows.Find(new object[] { totalcategory,productfamily})));
}
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public override global::System.Data.DataTable Clone()
{
TotalRevenueByMonthDataTable cln = ((TotalRevenueByMonthDataTable)(base.Clone()));
cln.InitVars();
return cln;
}
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
protected override global::System.Data.DataTable CreateInstance()
{
return new TotalRevenueByMonthDataTable();
}
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
internal void InitVars() {
this.columntotalcategory = base.Columns["totalcategory"];
this.columnname = base.Columns["name"];
this.columnfinancialmonth = base.Columns["financialmonth"];
this.columnfinancialyear = base.Columns["financialyear"];
this.columnproductfamily = base.Columns["productfamily"];
this.columnaccountmanagerid = base.Columns["accountmanagerid"];
this.columnaccountmanager = base.Columns["accountmanager"];
this.columncountdownregion = base.Columns["countdownregion"];
this.columnbuid = base.Columns["buid"];
this.columnregionid = base.Columns["regionid"];
this.columncurrencycode_rc = base.Columns["currencycode_rc"];
this.columninitial_rcCoS = base.Columns["initial_rcCoS"];
this.columninitial_rc = base.Columns["initial_rc"];
:
:
}
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
private void InitClass() {
this.columntotalcategory = new global::System.Data.DataColumn("totalcategory", typeof(int), null, global::System.Data.MappingType.Element);
base.Columns.Add(this.columntotalcategory);
this.columnname = new global::System.Data.DataColumn("name", typeof(string), null, global::System.Data.MappingType.Element); base.Columns.Add(this.columnname);
this.columnfinancialmonth = new global::System.Data.DataColumn("financialmonth", typeof(int), null, global::System.Data.MappingType.Element);
base.Columns.Add(this.columnfinancialmonth);
this.columnfinancialyear = new global::System.Data.DataColumn("financialyear", typeof(int), null, global::System.Data.MappingType.Element);
base.Columns.Add(this.columnfinancialyear);
this.columnproductfamily = new global::System.Data.DataColumn("productfamily", typeof(global::System.Guid), null, global::System.Data.MappingType.Element);
base.Columns.Add(this.columnproductfamily);
this.columnaccountmanagerid = new global::System.Data.DataColumn("accountmanagerid", typeof(global::System.Guid), null, global::System.Data.MappingType.Element);
base.Columns.Add(this.columnaccountmanagerid);
this.columnaccountmanager = new global::System.Data.DataColumn("accountmanager", typeof(string), null, global::System.Data.MappingType.Element);
:
:
this.Constraints.Add(new global::System.Data.UniqueConstraint("Constraint1", new global::System.Data.DataColumn[] {
this.columntotalcategory}, true));
this.columntotalcategory.AllowDBNull = false;
this.columntotalcategory.Unique = true;
this.columninitial_rcCoS.ReadOnly = true;
this.columninitial_rc.ReadOnly = true;
this.columnannual_rcCoS.ReadOnly = true;
this.columnannual_rc.ReadOnly = true;
this.columnrental_rcCoS.ReadOnly = true;
this.columnrental_rc.ReadOnly = true;
this.columnservice_rcCoS.ReadOnly = true;
this.columnservice_rc.ReadOnly = true;
this.columntraining_rcCoS.ReadOnly = true;
this.columntraining_rc.ReadOnly = true;
this.columngross_rc.ReadOnly = true;
this.columncos_rc.ReadOnly = true;
this.columnnet_rc.ReadOnly = true;
this.columnytd_gross_rc.ReadOnly = true;
this.columnytd_net_rc.ReadOnly = true;
}
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public TotalRevenueByMonthRow NewTotalRevenueByMonthRow() {
return ((TotalRevenueByMonthRow)(this.NewRow()));
}
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
protected override global::System.Data.DataRow NewRowFromBuilder(global::System.Data.DataRowBuilder builder) {
return new TotalRevenueByMonthRow(builder); }
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
protected override global::System.Type GetRowType() {
return typeof(TotalRevenueByMonthRow); }
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
private void InitExpressions() {
this.gross_rcColumn.Expression = "initial_rc + annual_rc + rental_rc + service_rc + training_rc";
this.net_rcColumn.Expression = "gross_rc - cos_rc";
this.ytd_gross_rcColumn.Expression = "ytd_initial_rc + ytd_annual_rc + ytd_rental_rc + ytd_service_rc + ytd_training_rc" + ""; this.ytd_net_rcColumn.Expression = "ytd_gross_rc - ytd_cos_rc"; }
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
protected override void OnRowChanged(global::System.Data.DataRowChangeEventArgs e) {
base.OnRowChanged(e);
if ((this.TotalRevenueByMonthRowChanged != null)) {
this.TotalRevenueByMonthRowChanged(this, new TotalRevenueByMonthRowChangeEvent(((TotalRevenueByMonthRow)(e.Row)), e.Action));
} }
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
protected override void OnRowChanging(global::System.Data.DataRowChangeEventArgs e) {
base.OnRowChanging(e);
if ((this.TotalRevenueByMonthRowChanging != null)) {
this.TotalRevenueByMonthRowChanging(this, new TotalRevenueByMonthRowChangeEvent(((TotalRevenueByMonthRow)(e.Row)), e.Action)); } }
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
protected override void OnRowDeleted(global::System.Data.DataRowChangeEventArgs e) {
base.OnRowDeleted(e);
if ((this.TotalRevenueByMonthRowDeleted != null)) {
this.TotalRevenueByMonthRowDeleted(this, new TotalRevenueByMonthRowChangeEvent(((TotalRevenueByMonthRow)(e.Row)), e.Action)); } }
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
protected override void OnRowDeleting(global::System.Data.DataRowChangeEventArgs e) {
base.OnRowDeleting(e); if ((this.TotalRevenueByMonthRowDeleting != null)) { this.TotalRevenueByMonthRowDeleting(this, new TotalRevenueByMonthRowChangeEvent(((TotalRevenueByMonthRow)(e.Row)), e.Action)); } }
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public void RemoveTotalRevenueByMonthRow(TotalRevenueByMonthRow row) {
this.Rows.Remove(row);
}
Please advise.
Thank you.
Regards,
Micheale
kidshaw
Participant
1178 Points
252 Posts
Re: Expecting 1 value(s) for the key being indexed, but received 2 value(s).
Apr 24, 2012 11:40 AM|LINK
Try setting totalPendingNew as an array. If multiple values are returned, you can inspect with a breakpoint and see what is happening in your dataset.
Mamba Dai - ...
All-Star
23530 Points
2683 Posts
Microsoft
Re: Expecting 1 value(s) for the key being indexed, but received 2 value(s).
Apr 26, 2012 03:41 AM|LINK
Hi,
This exception occurs when you call the FindBytotalcategory2, so I check this method from your souce code. I notice you use the this.Rows.Find method in the FindBytotalcategory2 method. This method, namely DataRowCollection.Find method gets the row that contains the specified primary key values. That means you need to provide an object arrary which match the primary columns of your table. To reproduce your issue I create this simple sample code different with yours:
DataTable dt = new DataTable(); dt.Columns.Add("Col1"); dt.Columns.Add("Col2"); dt.Rows.Add("John", "Smith"); dt.Rows.Add("Spotty", "Bowles"); dt.Rows.Add("Super", "Man"); DataView view = dt.DefaultView; view.Sort = "Col1 ASC, COL2"; ////Colum order include both columns object vals = "John"; //Dim vals(1) As Object //vals(0) = "John" //vals(1) = "Smith" try { int i = view.Find(vals); if (i > -1) { Response.Write(view[i]["Col1"] + view[i]["Col2"]); } else { Response.Write("Not match Found"); } } catch (Exception ex) { Response.Write(ex.Message); }When I modify my sample code like below, all work fine, please corret your source code with my comments:
DataTable dt = new DataTable(); dt.Columns.Add("Col1"); dt.Columns.Add("Col2"); dt.Rows.Add("John", "Smith"); dt.Rows.Add("Spotty", "Bowles"); dt.Rows.Add("Super", "Man"); DataView view = dt.DefaultView; view.Sort = "Col1 ASC, COL2"; ////Colum order include both columns object[] vals = new object[2]; vals[0] = "John"; vals[1] = "Smith"; try { int i = view.Find(vals); if (i > -1) { Response.Write(view[i]["Col1"] + view[i]["Col2"]); } else { Response.Write("Not match Found"); } } catch (Exception ex) { Response.Write(ex.Message); }Feedback to us
Develop and promote your apps in Windows Store
micnie2020
Member
308 Points
539 Posts
Re: Expecting 1 value(s) for the key being indexed, but received 2 value(s).
Apr 26, 2012 07:53 AM|LINK
Hi,
Thank you for the reply.
Let's said i change to be
Before:
DataSet1.TotalRevenueByMonthRow totalPendingNew = (DataSet1.TotalRevenueByMonthRow)dataSet1.TotalRevenueByMonth.FindBytotalcategory(Helpers.TotalCategory.PNEWBUSINESS);
After:
DataSet1.TotalRevenueByMonthRow totalPendingNew = (DataSet1.TotalRevenueByMonthRow)dataSet1.TotalRevenueByMonth.Where(p=>p.totalcategory.Equals(Helpers.TotalCategory.PNEWBUSINESS));
I getting error on bold section:-
Unable to cast object of type 'System.Linq.EnumerableQuery`1[ReviewTool.DataSet1+TotalRevenueByMonthRow]' to type 'TotalRevenueByMonthRow'.
Please advise.
Thank you.
Regards,
Micheale
kidshaw
Participant
1178 Points
252 Posts
Re: Expecting 1 value(s) for the key being indexed, but received 2 value(s).
Apr 26, 2012 08:51 AM|LINK
Add .Single() to the end of your statement.