Home All Groups Group Topic Archive Search About

Problems editing datatable stored in viewstate

Author
9 Dec 2008 6:49 PM
adiel_g
I have the following function working correctly in VB.NET which
updates a table ON THE FLY in viewstate. (without copying it to a
datatable object and then back to viewstate again after editing it):

Public Function updateUser(ByVal userId As String, _
                                         ByVal firstName As String, _
                                         ByVal lastName As String) As
Boolean

        Dim rowIndex As Integer

            ' Find the index of the record to update
            rowIndex = ViewState("dtTempUserTable").Rows.IndexOf
(ViewState("dtTempUserTable").Rows.Find(userId))

            ' Update record
            ViewState("dtTempUserTable").Rows(rowIndex).Item
("firstName") = firstName
            ViewState("dtTempUserTable").Rows(rowIndex).Item
("lastName") = lastName

            ' Normal Completion
            Return True

    End Function


I need to use the same function in C# so I created the following code:


        public bool updateUser(string userId, string firstName, string
lastName)
        {

            int rowIndex = 0;

            // Find the index of the record to update
            rowIndex = ViewState["dtTempUserTable"].Rows.IndexOf
(ViewState["dtTempUserTable"].Rows.Find(userId));

            // Update record
            ViewState["dtTempUserTable"].Rows[rowIndex].Item
["firstName"] = firstName;
            ViewState["dtTempUserTable"].Rows[rowIndex].Item
["lastName"] = lastName;

            // Normal Completion
            return true;

        }


Now I am receiving the following error reguarding viewstate not
finding the Rows method:

'Object' does not contain a definition for 'rows' and no extension
method 'Rows' accepting a first argument of type 'object' could be
found.

I would like to be able to perform the exact same operation I am doing
in VB.NET in C#. (Without having to move the object to a temporary
datatable object and then back again into viewstate) What am I
missing?

Thanks Before Hand,
Adiel

Author
9 Dec 2008 7:11 PM
Jeff Johnson
<adie***@hotmail.com> wrote in message
Show quoteHide quote
news:ffe73d51-d0dc-4613-abeb-cc91b3526c46@r40g2000yqj.googlegroups.com...
>I have the following function working correctly in VB.NET which
> updates a table ON THE FLY in viewstate. (without copying it to a
> datatable object and then back to viewstate again after editing it):
>
> Public Function updateUser(ByVal userId As String, _
>                                         ByVal firstName As String, _
>                                         ByVal lastName As String) As
> Boolean
>
>        Dim rowIndex As Integer
>
>            ' Find the index of the record to update
>            rowIndex = ViewState("dtTempUserTable").Rows.IndexOf
> (ViewState("dtTempUserTable").Rows.Find(userId))
>
>            ' Update record
>            ViewState("dtTempUserTable").Rows(rowIndex).Item
> ("firstName") = firstName
>            ViewState("dtTempUserTable").Rows(rowIndex).Item
> ("lastName") = lastName
>
>            ' Normal Completion
>            Return True
>
>    End Function
>
>
> I need to use the same function in C# so I created the following code:
>
>
>        public bool updateUser(string userId, string firstName, string
> lastName)
>        {
>
>            int rowIndex = 0;
>
>            // Find the index of the record to update
>            rowIndex = ViewState["dtTempUserTable"].Rows.IndexOf
> (ViewState["dtTempUserTable"].Rows.Find(userId));
>
>            // Update record
>            ViewState["dtTempUserTable"].Rows[rowIndex].Item
> ["firstName"] = firstName;
>            ViewState["dtTempUserTable"].Rows[rowIndex].Item
> ["lastName"] = lastName;
>
>            // Normal Completion
>            return true;
>
>        }
>
>
> Now I am receiving the following error reguarding viewstate not
> finding the Rows method:
>
> 'Object' does not contain a definition for 'rows' and no extension
> method 'Rows' accepting a first argument of type 'object' could be
> found.
>
> I would like to be able to perform the exact same operation I am doing
> in VB.NET in C#. (Without having to move the object to a temporary
> datatable object and then back again into viewstate) What am I
> missing?

Sounds like you need a cast. Might as well set a variable to the data table
as well instead of continually referencing ViewState:

            // Find the index of the record to update
            DataTable dt = ViewState["dtTempUserTable"] as DataTable;
            rowIndex =
dt.Rows.IndexOf(ViewState["dtTempUserTable"].Rows.Find(userId));

            // Update record
            dt.Rows[rowIndex].Item["firstName"] = firstName;
            dt.Rows[rowIndex].Item["lastName"] = lastName;
Are all your drivers up to date? click for free checkup

Author
9 Dec 2008 8:03 PM
adiel_g
Thanks Jeff. So you cant do it "On the fly" in C# like you can in
VB.NET?

Thanks,
Adiel
Author
9 Dec 2008 8:48 PM
Jeff Johnson
<adie***@hotmail.com> wrote in message
news:984e8ce9-5cbd-4be8-b880-c357f90f33b8@r34g2000vbp.googlegroups.com...

> Thanks Jeff. So you cant do it "On the fly" in C# like you can in
> VB.NET?

More like VB is being very lax. Sounds like you have Option Strict Off or
something like that. This can lead to things akin to Evil Type Coercion in
the VB6 days. Like the Dark Side, relying on that is the quick and easy
path. I recommend you try to wean yourself from this habit. (And if you're
going to be using C#, you have little choice!)
Author
9 Dec 2008 9:43 PM
adiel_g
I see...another limitation in C#.  So much of the strengths of the IL
is missed when you only code in C#...

Adiel
Author
9 Dec 2008 9:57 PM
Jeff Johnson
<adie***@hotmail.com> wrote in message
news:63afa3ee-60d8-428e-9d66-c0edc2e273e1@h16g2000yqj.googlegroups.com...

>I see...another limitation in C#.  So much of the strengths of the IL
> is missed when you only code in C#...

I'm a long-time VB6 developer, and I actually LIKE this "limitation" of C#.
I don't consider "making me do it the way I SHOULD be doing it" to be a
limitation, but that's just my opinion. I realize that some folks prefer to
let the language "figure it out for them."
Author
10 Dec 2008 2:29 PM
adiel_g
On Dec 9, 4:57 pm, "Jeff Johnson" <i....@enough.spam> wrote:
> <adie***@hotmail.com> wrote in message
>
> news:63afa3ee-60d8-428e-9d66-c0edc2e273e1@h16g2000yqj.googlegroups.com...
>
> >I see...another limitation in C#.  So much of the strengths of the IL
> > is missed when you only code in C#...
>
> I'm a long-time VB6 developer, and I actually LIKE this "limitation" of C#.
> I don't consider "making me do it the way I SHOULD be doing it" to be a
> limitation, but that's just my opinion. I realize that some folks prefer to
> let the language "figure it out for them."

Thanks Jeff, I will take your advice.  I did find a way to modify the
viewstate data "On the fly" without copying it over to another
object.  I used Ignacio's example.  Here is the working code.
(Basically the answer is you have to use the cast operation (ex
(DataTable) in front of the object to access it's methods and
properties, etc):

        public bool updateUser(string userId, string firstName, string
lastName)
        {

            int rowIndex = 0;

            // Find the index of the record to update
            rowIndex = ((DataTable) ViewState
["dtTempUserTable"]).Rows.IndexOf(ViewState
["dtTempUserTable"].Rows.Find(userId));

            // Update record
            ((DataTable) ViewState["dtTempUserTable"]).Rows
[rowIndex].Item["firstName"] = firstName;
            ((DataTable) ViewState["dtTempUserTable"]).Rows
[rowIndex].Item["lastName"] = lastName;

            // Normal Completion
            return true;

        }
Author
10 Dec 2008 4:51 PM
Jeff Johnson
<adie***@hotmail.com> wrote in message
Show quoteHide quote
news:c84d80fd-f779-4bcf-a247-1b420540c5a5@w1g2000prm.googlegroups.com...

>>>I see...another limitation in C#. So much of the strengths of the IL
>>> is missed when you only code in C#...

>> I'm a long-time VB6 developer, and I actually LIKE this "limitation" of
>> C#.
>> I don't consider "making me do it the way I SHOULD be doing it" to be a
>> limitation, but that's just my opinion. I realize that some folks prefer
>> to
>> let the language "figure it out for them."

> Thanks Jeff, I will take your advice.  I did find a way to modify the
> viewstate data "On the fly" without copying it over to another
> object.  I used Ignacio's example.  Here is the working code.
> (Basically the answer is you have to use the cast operation (ex
> (DataTable) in front of the object to access it's methods and
> properties, etc):

I told you that in my first reply. I also said I think it's far easier to
set a variable to the table ONCE and then just reference the variable
instead of having to put the cast in front of EVERY OCCURRENCE of
ViewState["dtTempUserTable"]. That clutters up the code to my eyes.

Plus, assuming that's your real code, you forgot to cast a second time here:

rowIndex = ((DataTable) ViewState
["dtTempUserTable"]).Rows.IndexOf(ViewState
["dtTempUserTable"].Rows.Find(userId));

To be honest, I missed that in my first example as well, so here's the
revised code:

            // Find the index of the record to update
            DataTable dt = ViewState["dtTempUserTable"] as DataTable;
            rowIndex = dt.Rows.IndexOf(dt.Rows.Find(userId));

            // Update record
            dt.Rows[rowIndex].Item["firstName"] = firstName;
            dt.Rows[rowIndex].Item["lastName"] = lastName;
Author
9 Dec 2008 9:59 PM
Ignacio Machin ( .NET/ C# MVP )
On Dec 9, 3:03 pm, adie***@hotmail.com wrote:
> Thanks Jeff. So you cant do it "On the fly" in C# like you can in
> VB.NET?
>
> Thanks,
> Adiel


You are doing it "on the fly". you just need to cast it to the correct
type.
Personally I think it's clearer to assign it to a local var, at the
end  you are only consuming 4 more bytes :)

DataTable table =ViewState["dtTempUserTable"] as DataTable;

if ( table!=null){
table.Rows.IndexOf ....
}
Author
9 Dec 2008 9:50 PM
Ignacio Machin ( .NET/ C# MVP )
On Dec 9, 1:49 pm, adie***@hotmail.com wrote:
Show quoteHide quote
> I have the following function working correctly in VB.NET which
> updates a table ON THE FLY in viewstate. (without copying it to a
> datatable object and then back to viewstate again after editing it):
>
> Public Function updateUser(ByVal userId As String, _
>                                          ByVal firstName As String, _
>                                          ByVal lastName As String) As
> Boolean
>
>         Dim rowIndex As Integer
>
>             ' Find the index of the record to update
>             rowIndex = ViewState("dtTempUserTable").Rows.IndexOf
> (ViewState("dtTempUserTable").Rows.Find(userId))
>
>             ' Update record
>             ViewState("dtTempUserTable").Rows(rowIndex).Item
> ("firstName") = firstName
>             ViewState("dtTempUserTable").Rows(rowIndex).Item
> ("lastName") = lastName
>
>             ' Normal Completion
>             Return True
>
>     End Function
>
> I need to use the same function in C# so I created the following code:
>
>         public bool updateUser(string userId, string firstName, string
> lastName)
>         {
>
>             int rowIndex = 0;
>
>             // Find the index of the record to update
>             rowIndex = ViewState["dtTempUserTable"].Rows.IndexOf
> (ViewState["dtTempUserTable"].Rows.Find(userId));

You have to cast it:
( (DataTable) ViewState["dtTempUserTable"] ) .Rows.IndexOf

Note the two set of parenthesis

Bookmark and Share