{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
{
oSPWeb.AllowUnsafeUpdates = true;
// Fetch the List
SPList list = oSPWeb.Lists["MyList"];
// Get the List item
SPListItem listItem = list.GetItemById(1);
// Get the Attachment collection
SPAttachmentCollection attachmentCollection = listItem.Attachments;
Stream attachmentStream;
Byte[] attachmentContent;
// Get the file from the file upload control
if (this.fileDocAttach.HasFile)
{
attachmentStream = this.fileDocAttach.PostedFile.InputStream;
attachmentContent = new Byte[attachmentStream.Length];
attachmentStream.Read(attachmentContent, 0, (int)attachmentStream.Length);
attachmentStream.Close();
attachmentStream.Dispose();
// Add the file to the attachment collection
attachmentCollection.Add(this.fileDocAttach.FileName, attachmentContent);
}
// Update th list item
listItem.Update();
oSPWeb.AllowUnsafeUpdates = false;
}
}