Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to make a Program which user Could Attach Pictures,And All Is Saved In Database Under One Id ,The Below Code Is Running Correctly But I Need To Let The User Have The Availability To add more than one pic in the same window , and when he attach a photo a label link is created with labellink.text = openfileDialog.Filename;

here is the code

private void add_photos_Click(object sender, EventArgs e)
        {   
            DBClass db = DBClass.GetInstance(); //this is a class which contains all commands of database and the connection

            add_new_content aa = (add_new_content)this.Owner;
            string id = @"select cont_id from dbo.contents where cont_name='" + aa.txt_name.Text + "'";
            object obj = db.ExecuteScalre(id,CommandType.Text);


            string str = @"INSERT INTO [Picture_upload].[dbo].[Pictures]
           ([cont_id]
           ,[path])
            VALUES
           (" + obj + ",'" +
              openFileDialog1.FileName + "')";
            

            db.ExecuteNonQuery(str, CommandType.Text); // i want to loop it

            linkLabel1.Text = openFileDialog1.FileName;  //i want to loop this too
            txt_link.Text = " ";



     }
Posted
Updated 12-Jan-14 2:08am
v5
Comments
OriginalGriff 12-Jan-14 7:28am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So what do you want in the loop, and what does that have to do with your button click event?
Use the "Improve question" widget to edit your question and provide better information.
Member 10518995 12-Jan-14 7:39am    
Thanks soo much for your quick replay and i'll try harder to improve my questions more
OriginalGriff 12-Jan-14 7:44am    
Just do us both a favour: edit it again, and move the question out of the subject and into the body - where it isn't truncated and unreadable!
The subject should be a short description (ten words or so, max) which tells us enough to know if we have experience with the area. The body can be long enough to fully describe the problem.
Andreas Gieriet 12-Jan-14 7:54am    
So what is the problem?
Some (DB-) design or implementation issue?
Compilation or functional problem?
Is this a homework assignment you ask us to do for you?
...?

The Title of your question is so unspecific that you risk nobody will answer - the question must attract those CP members who potentially know an answer. If you don't manage to provide a concise question title, then at least add the relevant terms in the subject, e.g. "How to add multiple pictures to database?"

Cheers
Andi
Member 10518995 12-Jan-14 7:57am    
i just don't know how to make a loop for btn_click
while(btn_click)
{
do code..
}

1 solution

Looking at your comments on your question, it looks like you want each time he presses the button to add a file to the db - in which case you don't want a loop at all: you will get a Click event each time he presses the button, so do one each time he presses it.

Then, the only complications are:
1) You need to activate your OpenFileDialog in the click handler to get a new file:
C#
if (openFileDialog1.ShowDialog() == DialogResult.OK)
   {
   DBClass db = DBClass.GetInstance();
   ...
   }
Though I would probably create the OpenFileDialog instance as part of the event handler, rather than embedding one in my form.
2) You need to look at adding a new LinkLabel each time he clicks - if you just recycle the existing one then you can only show the last file link. However, you need to work out where you are going to put them - I can't do that for you - but the general code is:
C#
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
    {
    LinkLabel ll = new LinkLabel();
    ll.Text = ofd.FileName;
    ll.Location = new Point(100, 100);
    Controls.Add(ll);
    }
You will need to set the location to your point (which shouldn't be the same each time...) and you probably need to hook up some events as well.
3) You should look at your database code as well: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
Member 10518995 12-Jan-14 8:59am    
Thanks for your help really i appreciate ur efforts can i ask u how to i can change the location when it comes to the next loop which shouldn't be the same each time
but i can't
OriginalGriff 12-Jan-14 9:11am    
Forget the word "loop" - you aren't looping (and shouldn't in a click event, most likely).

Declare a class level integer and start it with the value of the highest point you want a LinkLabel (try 100 for the moment, it'll give you the idea of what I mean). Call it "yLoc"
Then change the code above to:
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
LinkLabel ll = new LinkLabel();
ll.Text = ofd.FileName;
ll.Location = new Point(100, yLoc);
Controls.Add(ll);
yLoc += 25;
}
That will create each one 25 pixels lower on the form than the previous one.
I can't tell you exactly what numbers you need, because I can't see your form! (laugh)
Member 10518995 15-Jan-14 9:03am    
Thanks soooooooooooooooooooooooooooooooo much :)
OriginalGriff 15-Jan-14 9:32am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900