473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

datarow update problem

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
8 4104
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Allan Bredahl | last post by:
Hi All I have a bit of a problem with using a datarow to store data that is show in a form. On form load I set the Value/Text properties of my form elements to the specific Column values of...
3
by: Phil | last post by:
Hi, I have a client/server app. that uses a windows service for the server and asp.net web pages for the client side. My server class has 3 methods that Fill, Add a new record and Update a record....
3
by: Jon S via DotNetMonster.com | last post by:
Hi all, I'm having a problem updating a simple change I've made to a Access 2000 table through databinding. The error I get is : An unhandled exception of type...
4
by: CaptRR | last post by:
I think this is the right group to post to, so here goes. My problem is this, I cannot update the datarow to save my life. Been on this for 2 days now, and still am no closer to figuring it out...
6
by: Wanda | last post by:
Hello, I tried to update a row in a table by using the datarow and data adapter. Unfortunately, it doesn't update. I try finding that datarow and set it to "dr", I can see dr could be just an...
5
by: rodchar | last post by:
Hey all, I thought what I was doing was pretty straightforward but apparently not. All I'm doing is loading a datarow, letting the user modify a field in the datarow by inputing into a textbox....
2
by: Steve Amey | last post by:
Hi all I am binding a DataRow to some controls on a form. When I've finished editing the DataRow I click on a toolbar button to save the changes. If the focus is on a particular control, the...
8
by: Rodusa | last post by:
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...
5
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.