Problems editing datatable stored in viewstate

A

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
 
J

Jeff Johnson

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;
 
A

adiel_g

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

Thanks,
Adiel
 
J

Jeff Johnson

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!)
 
A

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
 
I

Ignacio Machin ( .NET/ C# MVP )

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
 
J

Jeff Johnson

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."
 
I

Ignacio Machin ( .NET/ C# MVP )

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 ....
}
 
A

adiel_g

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;

}
 
J

Jeff Johnson

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top