last day (22 days later) » 

12:58 PM
Hi! working?
 
yes
i say we should continue here and in the other room :)
please tell Morski also to join this room
 
ok, lets continue here :)) i want to add the file to the listbox
 
the listbox.items.add(new ListItem(fileName)); is what you want I guess
 
ok, let me see first
 
1:00 PM
i'm using a fileupload, i don't need to code something with it? like, if(fileupload1.postedfile != null && fileupload1.hasfile) ?
 
Norbert the problem is the pictures removes from the listbox but when saving it dont remove the picture from the save button
from the list *
 
ok, i took a look on that question. when you press the button, you want to remove the items from the listbox and also delete the attachments, right?
 
Right i only get it work to remove from listbox
but not from my SpList :)
 
yes Morski, that's exactly what I understood from the question
 
you can start helping him out, so you don't get confused :)
 
1:04 PM
ok, thanks Kriistiian :) I had the feeling that the two of you are talking about different things. so one at a time :)
 
SPWeb web = SPContext.Current.Web;
SPList myList = web.Lists["MyList"];
string listUrl = web.Url + "/" + myList.RootFolder.Url;

string attachmentUrl;
if (ListBox1.Attachments.Count > 0)
{
string filename = ListBox1.Attachments[0];
attachmentUrl = listUrl + "/attachments/" + item.ID + "/" + filename;
}
ListBox1.Attachments.Delete(filename);
when writing this code filename get red
this is code for deleting in my list
 
Morski, is this line ok? "ListBox1.Attachments"? I didn't know that the listbox control has attachments...
 
I am first saving the attachment to my list , then i search it up and what to be able do update the image of the attachment
and then save it back
:)
 
wait wait. let's delete it first. What is the type of your ListBox1 object?
 
.Attachments get red on hole the code...
i dont understand what that means .:D
 
1:10 PM
you probably have some add-on in your visual studio, which marks with red parts of the code which is not correct.
 
yes
resharper
 
Like I was saying: ListBox1 is a ListBox control, and that control doesn't have Attachments property. The SPListItem class has one
 
"Cannot Resolve Symbol"
 
ok, clear for me
let me try to correct your code
so, you need to work together with Kriistiian here, who is populating the Listbox with the filenames, if I understood correctly. You, Morski, if you want to delete an attachment, you need the current SPListItem you are working with.
 
try
{
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;

SPList myList = web.Lists["MyList"];
int listItemId = Convert.ToInt32(HiddenField1.Value);

//Considering you have the ID of the List Item
SPListItem itemToUpdate = myList.GetItemById(listItemId);


if (itemToUpdate != null)

{
if (TextBox_Name.Text == string.Empty && TextBox_ProdNum.Text == string.Empty &&
TextBox_ListPrice.Text == string.Empty && TextBox_Color.Text == string.Empty &&
TextBox_MoreInfo.Text == string.Empty)
i also need to add to update attachment there...
i am already using SPlistItem
this is my code when i save
 
1:15 PM
Lets say like this. Morski is trying to remove the attachment from the listbox and from there update the list. While i'm trying to add a new attachment and update the listitem, get it? :)
 
ok, let's suppose itemToUpdate is the SPListItem from where you want to delete the attachments, right?
 
from mig list
 
yes, Kriistiian. I am just trying to make up the parts of the code that I am not seeing :)
ok, Morski, try this:
 
delete it i mean
 
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected){
ListBox1.Items.Remove(ListBox1.Items[i]);
no, sorry. i pressed enter too soon
wait a little more
 
1:18 PM
        for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            if (ListBox1.Items[i].Selected)
                ListBox1.Items.Remove(ListBox1.Items[i]);
        }
i am already using this..
 
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected){
ListBox1.Items.Remove(ListBox1.Items[i]);
itemToUpdate.Attachments.Delete(ListBox1.Items[i]);
}
}
 
okey will try now :)
 
ok, I hope you got the idea
it will work, it you have exactly the filenames you want to delete in the listbox
oh, one more thing
you have to call after the for cycle: itemToUpdate.Update()
to push the changes to the database
 
buy the way my deletebutton dont uses splisitem but me save button does
 
are the two buttons on the same class, on the same webpart?
never mind
on your method for your delete button, just do this:
SPListItem listItem = web.Lists["YourListName"].GetItemById(id);
and store the id somewhere, pass it as a parameter to your delete method, something like this
 
1:24 PM
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;

SPList myList = web.Lists["MyList"];
int listItemId = Convert.ToInt32(HiddenField1.Value);

//Considering you have the ID of the List Item
SPListItem itemToUpdate = myList.GetItemById(listItemId);
like this
 
yes, i already saw that. do that again in your delete method
 
i think i should do like this in delete
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;

SPList myList = web.Lists["MyList"];
int listItemId = Convert.ToInt32(HiddenField1.Value);

//Considering you have the ID of the List Item
SPListItem itemToUpdate = myList.GetItemById(listItemId);

if (itemToUpdate == null) return;
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected)
ListBox1.Items.Remove(ListBox1.Items[i]);
}
 
yes, also include after ListBox1.Items.Remove(ListBox1.Items[i]); itemToUpdate.Attachments.Delete(ListBox1.Items[i]);
and don't forget to set back allowunsafeupdates to false
 
alright will try now
when pressing delete i got
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
 
yes, i know why
please modify the for cycle like this
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected){
string filename = ListBox1.Items[i];
ListBox1.Items.Remove(ListBox1.Items[i]);
itemToUpdate.Attachments.Delete(filename);
}
}
15 minutes break. i will be back
 
1:32 PM
Alright :)
i works thanks :)
 
Norbert, right now i'm using this code when i want to add a new file to the listbox:

if (FileUpload1.PostedFile != null && FileUpload1.HasFile)
{
SPAttachmentCollection collAttachments = listItem.Attachments;
for (int i = 0; i < collAttachments.Count; i++)
{
ListBox_Attachment.Items.Add(new ListItem(collAttachments[i]));
}
}

But nothin happens, the site refreshes only.
 
I will be with you in around 10 minutes.
 
SPSite mySite = SPContext.Current.Site;
SPList myList = mySite.RootWeb.Lists.TryGetList("Extern products");
SPListItem listItem = myList.Items.Add();


if (FileUpload1.PostedFile != null && FileUpload1.HasFile)
{
SPAttachmentCollection collAttachments = listItem.Attachments;
for (int i = 0; i < collAttachments.Count; i++)
{
ListBox_Attachment.Items.Add(new ListItem(collAttachments[i]));
}
}
Thats the whole code
No problem. :)
 
2:02 PM
Forgot me? :(
Like i said, i have a fileupload control and a add button. When i click on the add button i want to add the uploaded file to the listbox. There after i want to update the list with the new attachments!
 
nono, i will be right with you. I am working here, you know :)
 
2:22 PM
ok, i am back
let me check out how this fileupload thing works, it's been a while i didn't use it
 
np
if (FileUpload_Pic.PostedFile != null && FileUpload_Pic.HasFile)
{
SPAttachmentCollection attachments = listItem.Attachments;

Stream fStream = FileUpload_Pic.PostedFile.InputStream;

byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
fStream.Dispose();


string fileName = Path.GetFileName(FileUpload_Pic.PostedFile.FileName);
attachments.Add(fileName, contents);

}

This code ive been using to store attachments to my list
 
ok
first, you should call listItem.Update() after you added the attachment
or adding the attachments is working fine even without listItem.Update() ?
 
Well i think the whole code is wrong. I dont want it to update in the list in this button, this button will only add the attachments do the listbox
And then i got a button called "Update list". when i click there the attachments should be added to the list
 
aha
so you first upload files one by one and you want them to appear in the listbox
and then when you call update list, you want to add them also to the listbox
sorry
 
1. upload attachments do a listbox.
2. save them to the list :))
 
2:31 PM
when you call update list, you want to add the attachments to the list
 
exactly
 
yes, sorry, i typed the wrong word
question regarding this:
what if you have already two files attached to the list item. do you show them in this listbox?
 
Yep, they are showing,and Morski got the delete button to work, so its only add left =)
 
actually, in this case it's a little more complicated
because the way morski is doing the delete, he asumes that every file which is in the listbox, is already attached to the listitem
 
Hm, lets start from the beginning, ill explain what we are working with
 
2:35 PM
what if, i have 2 files attached, and they appear in the listbox. then i come and i attach one more, like you said. i upload it, but i don't press yet the update list button so it's not attached to the list. it's just in the listbox. then i select all the three files and i press delete?
read my question first
morski's delete code doesn't make any difference on items "virtually" in the listbox and items phisically attached
do you see my concern here or not?
 
Thats what i told him, i want to delete the attachments from the listbox, but not update the list untill i click on the "update list" button
 
i see. and when you click the update list button, how will you know which items should be actually deleted from the listitem and which items from the listbox should be added?
how are you keeping track of that?
 
We got it working, everything is updating, name, listprice, productnumber etc.. but we are stuck on the attachment part
 
ok, if you say so
 
and it is updating =) we have tried with 10-15 products, and the specified priductID is updating, not everything in the list
 
2:38 PM
ok, so you say, you managed to upload the file and it appears in the listbox, right?
 
i've managed to upload the file directly to my list, but i want to upload it to the listbox, so i can attach more than one file at a time, and then update the listitem with all attachments at once
 
ok, when you do this:
string fileName = Path.GetFileName(FileUpload_Pic.PostedFile.FileName);
you should take the filename, and do this:
 
in my AddButton? (add to listbox)
 
ListBox1.Add(new ListItem(filename));
yes, after you uploaded the file
why, is not ok there?
 
        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        ListBox_Attachment.Items.Add(fileName);
 
2:42 PM
yes. that should work. it is not working?
 
why not ListBox_Attachment.Items.Add(new ListItem(fileName));?
just deployed, trying now
sec
 
i think the add method has multiple overload
it accepts also string also ListItem as paramater
 
shouldn't i check first if the fileupload has files?
 
the advantage with ListItem, that then you can specify an id also
 
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
ListBox_Attachment.Items.Add(fileName);

worked, it saved to the listbox
 
2:43 PM
you sent me this piece of code:
if (FileUpload_Pic.PostedFile != null && FileUpload_Pic.HasFile)
{
SPAttachmentCollection attachments = listItem.Attachments;

Stream fStream = FileUpload_Pic.PostedFile.InputStream;

byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
fStream.Dispose();


string fileName = Path.GetFileName(FileUpload_Pic.PostedFile.FileName);
attachments.Add(fileName, contents);

}
i was saying, that here, in this piece, you should put after string filename = ..... the line i have given you
 
yea, thats the one we are using when we are saving to the list (from the first webpart). i dont know if morski told you the whole project?
 
instead of attachments.add(...)
 
let me explain abit fast, maybe will be easier
 
no, he didn't. but it's not needed now
ok, if you want :)
 
first webpart: search in database, get product, and then if we want we can upload attachments, then we are using that code. Saving all to a List.

second webpart: search through a list, getting productid, when we are clicking on (Edit product), we get all information in a third webpart, (the one we are working with now).
 
2:46 PM
ok
 
not we want to add attachment to a listbox: we just made it, next step is to update the specific listitem with the new attachments
now*
 
ok
 
same with the delete button (delete from listbox). i just want to delete if from the listbox, and not from the list untill i click on (update list) button
 
so, i don't know if you are typing right now :) let me just say, that in order to achieve what you want, regarding the upload, i think you need one more additional thing
you showed me the code above, which uploads a file to the list directly
there, I say, you should replace attachments.add(...) part with ListBox.Items.Add(...)
BUT
you have to store somewhere the contents of the file too
 
like a picture library/document library?
 
2:51 PM
no, in memory
like a System.Generic.List
and you should create a new class
something like
class UploadedFile{ string fileName; byte[] contents;}
and whenever you upload a file in that code you showed me, you also add to your System.Generic.List<UploadedFile> instance a new member
are you following me?
 
yea, but thats like abit complicated for me, hehe quite new to sharepoint.. but that is needed when you are uploading attachments? the class
 
no, that is needed, because you want to upload multiple files to the listbox and press the update list button for all of them
and this not a sharepoint thing, this is a general .net thing :)
and i can tell you, that it will not be simple
 
so when i update the list i need to call the uploadedfile class?
 
no, when you upload a file, you need to add an instance of the uploaded file class to the instance of the List<UploadedFile> list :)
ok, to be honest with you
i don't think i can help you to do all that
 
haha its ok my friend, i'm really glad that you took your time to help us out, i think we will go that way morski did for now
 
2:57 PM
i could easier do the whole thing by myself, but i see that giving you ideas is not enoght
 
and then read more about generic lists
 
and also about page lifecycle
because there you will also have a problem
when your page reloads, everything you have put in that list instance, will be lost
unless you save it to the viewstate
but maybe viewstate is not even enabled on your sharepoint page
you see, how many problems you will have to deal with?
 
right now my friend, i'm confused.. but im glad that you are telling me this
 
my advice is: just add the file to the listitem when you upload it, one by one.
 
the i see how much there is to learn :)
 
2:59 PM
yes, and i have to tell you, even i don't know everything :)
i am faaaar :)
 
the thing about me and morski is that he is going the easier way, while i'm the person that always get problems.. and this seems so heavy that my head right now cant take all this at once..
 
i believe you
 
ofcourse, we are students tho, but right now out on a project so we can learn more, really fun, but there is so much
 
how old are you?
 
we are both 22
 
3:00 PM
just starting programing, right?
 
:) only been studying .net for like.. hm 1 year
going a sharepoint class , 2 years in school, this is the second year
 
yes, you could really learn from joining a company, where more senior developers could teach you
this is also interesting, what you are doing, but you will not learn things the right way this way, trust me
i bet, you have no idea what AllowUnsafeUpdates does, and you have to use it :)
you just saw it somewhere, that it helps if you have some nasty error, and you put it in your code :)
 
exactly, sharepoint blog helped us out alot hehe
this site
 
i don't want to criticise you
i am saying, that i learned when i started exactly the way you are doing it now
trial-error
 
i know, we are new to this, loong way to go, but i will make it :D never give up
 
3:03 PM
i kept doing it, until it worked
 
i g2g now, back to programming, cya my friend, and thanks for the help
 
if you want to really understand how things work, you have to take them in a specific order. that's why it's better if you have some seniors around you, who can explain you well. on forums many times, even if the answer is correct, the explanations is not always
ok, see you. good luck!
 

  last day (22 days later) »