|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problems editing datatable stored in viewstateupdates 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 <adie***@hotmail.com> wrote in message
Show quoteHide quote news:ffe73d51-d0dc-4613-abeb-cc91b3526c46@r40g2000yqj.googlegroups.com... Sounds like you need a cast. Might as well set a variable to the data table >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? 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; Thanks Jeff. So you cant do it "On the fly" in C# like you can in
VB.NET? Thanks, Adiel <adie***@hotmail.com> wrote in message
news:984e8ce9-5cbd-4be8-b880-c357f90f33b8@r34g2000vbp.googlegroups.com... More like VB is being very lax. Sounds like you have Option Strict Off or > Thanks Jeff. So you cant do it "On the fly" in C# like you can in > VB.NET? 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!) I see...another limitation in C#. So much of the strengths of the IL
is missed when you only code in C#... Adiel <adie***@hotmail.com> wrote in message
news:63afa3ee-60d8-428e-9d66-c0edc2e273e1@h16g2000yqj.googlegroups.com... I'm a long-time VB6 developer, and I actually LIKE this "limitation" of C#. >I see...another limitation in C#. So much of the strengths of the IL > is missed when you only code in 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." On Dec 9, 4:57 pm, "Jeff Johnson" <i....@enough.spam> wrote: Thanks Jeff, I will take your advice. I did find a way to modify the> <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." 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; } <adie***@hotmail.com> wrote in message
Show quoteHide quote news:c84d80fd-f779-4bcf-a247-1b420540c5a5@w1g2000prm.googlegroups.com... I told you that in my first reply. I also said I think it's far easier to >>>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): 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; 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 You are doing it "on the fly". you just need to cast it to the correct> VB.NET? > > Thanks, > Adiel 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 .... } On Dec 9, 1:49 pm, adie***@hotmail.com wrote:
Show quoteHide quote > I have the following function working correctly in VB.NET which You have to cast it:> 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)); ( (DataTable) ViewState["dtTempUserTable"] ) .Rows.IndexOf Note the two set of parenthesis
Other interesting topics
reflection, GetType()
How to create what VB6 calls an 'ActiveX EXE' how to dispose of a streamwriter without closing the underlying stream Sanity check on call to imported DLL function RegEx: Remove all characters except [0-9]+ Convert float to double and back - easiest way without a loss of precision? Getting the count of tags in a XML-file I need help with writing || operator Help splitting up and processing Textbox data .. for each and unknown numbe of tokens ?? Getting tags OTHER than blobb |
|||||||||||||||||||||||