'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

LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
 
GeorgeGerguesConnect With a Mentor Commented:
SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value)));

It is

Glad it worked.
0
 
GeorgeGerguesCommented:
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
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Now I get this error:

 error CS1502: The best overloaded method match for 'int.Parse(string)' has some invalid arguments  
 
 
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
GeorgeGerguesCommented:
replace
SPListItem item = sourceList.GetItemById(hdID);

with

SPListItem item = sourceList.GetItemById(int.Parse(hdID.toString()));
0
 
IsaacSharePoint Client Side DeveloperAuthor 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

 
0
 
GeorgeGerguesCommented:
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.
0
 
IsaacSharePoint Client Side DeveloperAuthor 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"?
0
 
GeorgeGerguesCommented:

replace
SPListItem item = sourceList.GetItemById(hdID);

with

SPListItem item = sourceList.GetItemById(int.Parse(hdID.Value.toString()));
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
That did not work but this did,

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

Thanks for leading me in the right direction.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.