We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

Isaac
Isaac asked
on
Medium Priority
1,950 Views
Last Modified: 2012-08-14
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

Comment
Watch Question

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
IsaacSharePoint Client Side Developer

Author

Commented:
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()));
IsaacSharePoint Client Side Developer

Author

Commented:
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.
IsaacSharePoint Client Side Developer

Author

Commented:
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()));
IsaacSharePoint Client Side Developer

Author

Commented:
That did not work but this did,

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

Thanks for leading me in the right direction.
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.