niraj bagde

niraj bagde

  • NA
  • 5
  • 0

Error getting while adding one by one files into list using file uploader

Sep 19 2008 4:51 AM
hello.
 I m using the asp.net File uploader in sharepoint to uplaod a file.
and the following code i use for First time when the file is to be uploaded...............
const string ListName = "ChangeManagementList";
SPWeb site = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);//siteCollection.AllWebs[siteName];//
SPListItemCollection listItems = site.Lists[ListName].Items;
SPListItem item = listItems.Add();
SPList List = site.Lists[ListName];
SPWeb web = List.ParentWeb;
web.AllowUnsafeUpdates =
true;
try
{
if (FileUpload1.HasFile)
{
#region
Upload File and Attach it to ListItem
using (Stream fStream = FileUpload1.PostedFile.InputStream)
{
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (
int)fStream.Length);
SPAttachmentCollection attachments = attachments = item.Attachments;
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
attachments.Add(fileName, contents);
item[
"Title"] = "Niraj";
item.Update();
}
#endregion
id = getNewIdGenerated();
}
}
catch (Exception ex)
{
lblErrorOnPage.Text =
"Error with Upload " + ex.Message.ToString();
}
finally
{
web.AllowUnsafeUpdates =
false;
}
 
 
Now the problem is i want to  upload 1 more file using the same uploader for the same id generated in the list.for that im using the following code..
but i m getting the error.....Error with Upload Value does not fall within the expected range.
 
SPWeb website = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context);
string listName = "ChangeManagementList";
SPListItemCollection listItems = website.Lists[listName].Items;
SPList list = website.Lists[listName];
SPQuery nquery = new SPQuery();
string[] stringArray = { "UniqueId" };
nquery.ViewFields =
"UniqueId";
nquery.RowLimit = 1;
System.Text.
StringBuilder oSb = new System.Text.StringBuilder(255);
oSb.Append(
"<Where><Eq><FieldRef Name='ID'/><Value Type='Number'>");
oSb.Append(id);
oSb.Append(
"</Value></Eq></Where>");
string sResult = oSb.ToString();
nquery.Query = sResult;
SPListItemCollection collection = list.GetItems(nquery);
SPListItem item = collection.GetItemById(Convert.ToInt32(id));
 
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs;
foreach (SPWeb oWebsite in collWebsites)
{
SPList oList = oWebsite.Lists["ChangeManagementList"];
SPListItemCollection collListItems = oList.Items;
foreach (SPListItem oListItem in collListItems)
{
SPAttachmentCollection collAttachments = oListItem.Attachments;
for (int i = 0; i < collAttachments.Count; i++)
{
lblReasonForChange.Text += collAttachments[i];
}
}
}
// SiteData srvSiteData = new SiteData();
// //srvSiteData.Credentials = System.Net.CredentialCache.DefaultCredentials;
// string[] strAttach;
// srvSiteData.GetAttachments("ChangeManagementList", id, out strAttach);
// foreach (string str in strAttach)
// {
// lblReasonForChange.Text = str;
// }
 
try
{
item[
"Status"] = "Pending";
if (FileUpload1.HasFile)
{
#region
Upload File and Attach it to ListItem
if (list != null)
{
using (Stream fStream = FileUpload1.PostedFile.InputStream)
{
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (
int)fStream.Length);
SPAttachmentCollection attachments = item.Attachments;
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
attachments.Add(fileName, contents);
item.Update();
}
}
#endregion
}
}
catch (Exception ex)
{
lblErrorOnPage.Text =
"Error with Upload " + ex.Message.ToString();
}
finally
{
website.AllowUnsafeUpdates =
true;
}