By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 1,327 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

datarow update problem

P: n/a
I am getting an error on line 133. I am trying to update a datarow
using the find method, but it keeps throwing the error below:

int ItemKey = dv.Find(Item.inv_mast_uid);
DataRow dr;
if (ItemKey==-1) // NOT FOUND
{
dr = dt.NewRow();
dr["uid"] = Item.inv_mast_uid;
dr["rank"] = 1;
dr["item_id"] = Item.item_id;
dr["item_desc"] = Item.item_desc;
dr["extended_desc"] = Item.extended_desc;
dt.Rows.Add(dr);
}
else // IF FOUND
{
dr = dt.Rows.Find(ItemKey);
dr["item_desc"] = "testing";

}

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:
Line 131:
Line 132: dr = dt.Rows.Find(ItemKey);
Line 133: dr["item_desc"] = "testing";
Thank you

Jul 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Try this. I can not tell you why your find is failing, only that it is. Is
the DV and the DT variables related? Is the DV a dataview of the datatable?

else // IF FOUND
{

dr = dt.Rows.Find(ItemKey);
if dr is nothing then
messagebox.show("The find failed")
else
dr["item_desc"] = "testing";
end if
}

"Rodusa" <rc**********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I am getting an error on line 133. I am trying to update a datarow
using the find method, but it keeps throwing the error below:

int ItemKey = dv.Find(Item.inv_mast_uid);
DataRow dr;
if (ItemKey==-1) // NOT FOUND
{
dr = dt.NewRow();
dr["uid"] = Item.inv_mast_uid;
dr["rank"] = 1;
dr["item_id"] = Item.item_id;
dr["item_desc"] = Item.item_desc;
dr["extended_desc"] = Item.extended_desc;
dt.Rows.Add(dr);
}
else // IF FOUND
{
dr = dt.Rows.Find(ItemKey);
dr["item_desc"] = "testing";

}

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:
Line 131:
Line 132: dr = dt.Rows.Find(ItemKey);
Line 133: dr["item_desc"] = "testing";
Thank you

Jul 21 '05 #2

P: n/a
Yes.
I tried this first.
//DataView dv = new DataView(dt);
//dv.Sort = "uid";

Now I am using this code
DataView dv = new DataView(dt, "", "uid",
DataViewRowState.CurrentRows);
int ItemKey = dv.Find(Item.inv_mast_uid);

Jul 21 '05 #3

P: n/a
Chris,
thank you

I converted your VB code to this, but dr is always null. Is there any
relation between primary key of a Dataview and the primary key of
Datatable? Are they sincronized?

dr = dt.Rows.Find(ItemKey);
if (dr!=null)
{
Response.Write("Not Null");
}

Jul 21 '05 #4

P: n/a
Rodusa,

The find method finds the index that it has in the current dataview
situation, so why don't you update using the dataview.
\\\
dv.NewRow();
dv[key]["rank"] = 1;
///
or by using a datarowview from that row in the dataview.

I hope this helps?

Cor

"Rodusa" <rc**********@yahoo.com>
....
I am getting an error on line 133. I am trying to update a datarow
using the find method, but it keeps throwing the error below:

int ItemKey = dv.Find(Item.inv_mast_uid);
DataRow dr;
if (ItemKey==-1) // NOT FOUND
{
dr = dt.NewRow();
dr["uid"] = Item.inv_mast_uid;
dr["rank"] = 1;
dr["item_id"] = Item.item_id;
dr["item_desc"] = Item.item_desc;
dr["extended_desc"] = Item.extended_desc;
dt.Rows.Add(dr);
}
else // IF FOUND
{
dr = dt.Rows.Find(ItemKey);
dr["item_desc"] = "testing";

}

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:
Line 131:
Line 132: dr = dt.Rows.Find(ItemKey);
Line 133: dr["item_desc"] = "testing";
Thank you

Jul 21 '05 #5

P: n/a
Cor,

Thank you very very much. Your suggestion seems to work. But now I am
confused. First I never thought that you could update a DataView. I
thought you had to update the DataTable and then create a DataView
instance.

In the modified code below I don't understand how the DataView is
maintaning the update value when I bind the results to a Datagrid. The
reason I ask this is because the Dataview is inside the foreach and is
being recreated for each loop (I know this is a waste of resources, but
I don't know any other way of doing it).
From my understanding, the changes in the DataView (for each loop)

should get overwritten, am I right? In addition, I still don't know why
the DatTable index is different of the index of a DataView? Was that
the cause of the problem before?

foreach(Common.InvmastContainer Item in ArSearchedItems)
{
// CHECK DUPLICATES
// Adds found items to keywords datatable int ranking = 0;

DataView dv = new DataView(dt);
dv.Sort = "uid";
int ItemKey = dv.Find(Item.inv_mast_uid);

DataRow dr;
int m =ItemKey;
if (ItemKey==-1) // NOT FOUND
{
dr = dt.NewRow();
dr["uid"] = Item.inv_mast_uid;
dr["rank"] = 1;
dr["item_id"] = Item.item_id;
dr["item_desc"] = Item.item_desc;
dr["extended_desc"] = Item.extended_desc;
dt.Rows.Add(dr);
}
else // IF FOUND
{
dv[ItemKey]["rank"] = (int) (dv[ItemKey]["rank"]) +1;

}
}

}

DataView dvfinal = new DataView(dt);
dvfinal.Sort = "rank DESC";
DataGrid1.DataSource= dvfinal;
DataGrid1.DataBind();
Rod

Jul 21 '05 #6

P: n/a
Rodusa,

I did not know that it was about a webform solution with a datagrid.

However the dataview is a filter on your datatable, nothing more, one of its
important properties is the "DataTable", which is nothing more than the
datatable that it supports. You can have as much dataviews you have and in
the datatable itself exists one as well with as property Defaultview.

When I see it right are you getting information back, make some updates
using that filter and create than a new dataview to show. In a windowform
situation is the last a waste of time, because the dataview is dynamic. In a
webform situation I never did this and would the winform solution not work.
Therefore when this works, do I know a nice solution as well now.

I hpe this helps?

Cor

"Rodusa" <rc**********@yahoo.com>
Cor,

Thank you very very much. Your suggestion seems to work. But now I am
confused. First I never thought that you could update a DataView. I
thought you had to update the DataTable and then create a DataView
instance.

In the modified code below I don't understand how the DataView is
maintaning the update value when I bind the results to a Datagrid. The
reason I ask this is because the Dataview is inside the foreach and is
being recreated for each loop (I know this is a waste of resources, but
I don't know any other way of doing it).
From my understanding, the changes in the DataView (for each loop)

should get overwritten, am I right? In addition, I still don't know why
the DatTable index is different of the index of a DataView? Was that
the cause of the problem before?

foreach(Common.InvmastContainer Item in ArSearchedItems)
{
// CHECK DUPLICATES
// Adds found items to keywords datatable int ranking = 0;

DataView dv = new DataView(dt);
dv.Sort = "uid";
int ItemKey = dv.Find(Item.inv_mast_uid);

DataRow dr;
int m =ItemKey;
if (ItemKey==-1) // NOT FOUND
{
dr = dt.NewRow();
dr["uid"] = Item.inv_mast_uid;
dr["rank"] = 1;
dr["item_id"] = Item.item_id;
dr["item_desc"] = Item.item_desc;
dr["extended_desc"] = Item.extended_desc;
dt.Rows.Add(dr);
}
else // IF FOUND
{
dv[ItemKey]["rank"] = (int) (dv[ItemKey]["rank"]) +1;

}
}

}

DataView dvfinal = new DataView(dt);
dvfinal.Sort = "rank DESC";
DataGrid1.DataSource= dvfinal;
DataGrid1.DataBind();
Rod

Jul 21 '05 #7

P: n/a
Cor, you saved my life. So, when you say that the dataview is just a
filter and I use this code
dv[ItemKey]["rank"] = (int) (dv[ItemKey]["rank"]) +1; does it update
automatically the datatable?

And when I use this code

DataView dv = new DataView(dt)
dv.Sort = "uid";

I am not copying all the schema and data to dv, just creating an index
according the sort param specified. If this is true, it seems to me
that the performance is degraded compared to updating the datatable,
because it has to reindex it for each loop while if you use the
datatable you can create final dataview and reindex just once.

In summary, can I say that this process is the same as updating a
Database index where any changes reflects in the Table?

Anyway, I did not understand your second paragraph. What did you want
to say?
Thank you

Rod

Jul 21 '05 #8

P: n/a
Rodusa,

I never say it does. I say it would do it in my opinion, you have to test it
yourself.

Because I do not understand what you want to do with your code this sample
beneath, when you say

DataView dv = new DataView(dt);

and nothing more, than (when there is no filter and no sort)

dv[0] [0] and dt.Rows[0][0] are referencing to the same object and therefore
will an setting of data in dv[0][0] be the same as too dt.Rows[0][0]

However you have to test it all yourself.

The second paragraph was to tell you the difference in a windowform and
webform situation, however test it first yourself before you go further,
that problem can that not be.

Cor

"Rodusa" <rc**********@yahoo.com>
Cor, you saved my life. So, when you say that the dataview is just a
filter and I use this code
dv[ItemKey]["rank"] = (int) (dv[ItemKey]["rank"]) +1; does it update
automatically the datatable?

And when I use this code

DataView dv = new DataView(dt)
dv.Sort = "uid";

I am not copying all the schema and data to dv, just creating an index
according the sort param specified. If this is true, it seems to me
that the performance is degraded compared to updating the datatable,
because it has to reindex it for each loop while if you use the
datatable you can create final dataview and reindex just once.

In summary, can I say that this process is the same as updating a
Database index where any changes reflects in the Table?

Anyway, I did not understand your second paragraph. What did you want
to say?
Thank you

Rod

Jul 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.