473,395 Members | 1,468 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,395 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 4107
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.