Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

'Microsoft.SharePoint.SPList.GetItemById(int)' has some invalid arguments

The error below points to this line in my code:

SPListItem item = sourceList.GetItemById(hdID);

error CS1502: The best overloaded method match for 'Microsoft.SharePoint.SPList.GetItemById(int)' has some invalid arguments

Any ideas.  See a piece of code below.


foreach (GridViewRow row in GridView1.Rows)
            {                         
                HiddenField hdID = (HiddenField)row.FindControl("hdID");
                CheckBox complete = (CheckBox)row.FindControl("complete");

                if (complete.Checked)
                {
                    msg.Text += "GRIDVIEW LOOP -- hdID #: " + hdID.Value + "<br />";

                        SPListItem item = sourceList.GetItemById(hdID);
                        byte[] fileBytes = item.File.OpenBinary();
                        string destUrl = destList.RootFolder.Url + "/" + item.File.Name;
                        SPFile destFile = destList.RootFolder.Files.Add(destUrl, fileBytes, true /*overwrite*/);
                    
  
                        SPListItem destItem = destFile.Item;
                        destItem["STATUS"] = "REROUTE";
                        destItem["CASE NUMBER"] = "CASE NUMBER";
                        destItem["PRIORITY"] = "Y";
                        destItem["PRIORITY CODE"] = "PRIORITY CODE";
                        destItem["COMPLETE"] = "YES";

                        destItem.Update();
                }
        }

Open in new window

Avatar of GeorgeGergues
GeorgeGergues

hdID.Value
is acutaly a String

So you need to cast to an int

replace
SPListItem item = sourceList.GetItemById(hdID);

with

SPListItem item = sourceList.GetItemById(int.Parse(hdID));

Best of luck
Avatar of Isaac

ASKER

Now I get this error:

 error CS1502: The best overloaded method match for 'int.Parse(string)' has some invalid arguments  
 
 
replace
SPListItem item = sourceList.GetItemById(hdID);

with

SPListItem item = sourceList.GetItemById(int.Parse(hdID.toString()));
Avatar of Isaac

ASKER

Now I get this error:

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.FormatException: Input string was not in a correct format.

Stack Trace:


[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +7471335
   System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
   System.Int32.Parse(String s) +23
   ASP._controltemplates_webusercontrol_ascx.Button1_Click(Object sender, EventArgs e) in c:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\CONTROLTEMPLATES\WebUserControl.ascx:60
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

 
This means that your Grid does  not pass the value in that cell hdID


that could be a java scripting limiation .

I suggest you trace at this line and check the values at run time

Best of luck.
Avatar of Isaac

ASKER

GeorgeGerques,

I did a view source on the page before clicking submit and copied this piece of source code on one of the controls:

<input type="hidden" name="ctl00$m$g_8198334b_f124_40b9_a51e_787dd5b05a8d$ctl00$GridView1$ctl02$hdID" id="ctl00_m_g_8198334b_f124_40b9_a51e_787dd5b05a8d_ctl00_GridView1_ctl02_hdID" value="2" />

Am I writing the right code to retrieve "hdID"?

replace
SPListItem item = sourceList.GetItemById(hdID);

with

SPListItem item = sourceList.GetItemById(int.Parse(hdID.Value.toString()));
Avatar of Isaac

ASKER

That did not work but this did,

SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value)));

Thanks for leading me in the right direction.
ASKER CERTIFIED SOLUTION
Avatar of GeorgeGergues
GeorgeGergues

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial