Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
I'm trying to upload an image file into MySql many times, but I did't get it right. So finally I got working code from CP. It runs, but I want to understand the process step by step. Can anyone please explain the following code line by line?
C++
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.Files.Count != 0)
        {
            HttpPostedFile httpFile = Request.Files[0];
            string extension = this.GetFileExtension(httpFile.ContentType);
            if(extension == null )
            {
                Response.Write("Mime type not Supported");
                return;
            }
            BufferedStream bf = new BufferedStream(httpFile.InputStream);
            byte[] buffer = new byte[bf.Length];
            bf.Read(buffer,0,buffer.Length);
            DataAccess data = new DataAccess();
            data.addImage(buffer,extension);
            Response.Write("Image Added!");
        }
    }

    private string GetFileExtension(string p)
    {
        return p;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    }
    
    public class DataAccess
    {
        MySqlConnection con = new MySqlConnection("user id=root; password=admin; database=qms; server=localhost");
        public string addImage(byte[] buffer, string extension)
        {
            string strSql = "SELECT * FROM image";
            DataSet ds = new DataSet("Image");
            MySqlDataAdapter tempAP = new MySqlDataAdapter(strSql, this.con);
            MySqlCommandBuilder objCommand = new MySqlCommandBuilder(tempAP);
            tempAP.Fill(ds, "Table");

            try
            {
                this.con.Open();
                DataRow objNewRow = ds.Tables["Table"].NewRow();
                objNewRow["Extension"] = extension;
                objNewRow["Data"] = buffer;
                ds.Tables["Table"].Rows.Add(objNewRow);
                // trying to update the table to add the image
                tempAP.Update(ds, "Table");
            }
            catch (Exception e) { return e.Message; }
            finally { this.con.Close(); }
            return null;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    }

}


Thanks for your help!
Posted
Updated 9-Mar-11 3:47am
v2
Comments
Toli Cuturicu 9-Mar-11 14:15pm    
> please explain this code line by line
You are joking, right?
Sergey Alexandrovich Kryukov 9-Mar-11 17:14pm    
Sorry, this resource is for software people.
--SA
Sergey Alexandrovich Kryukov 9-Mar-11 17:21pm    
Also, you should write politely. You text is simply rude!
You should not use textspeak, should use full correct capitalization, punctuation and spelling. Do you think we have more time than you do?! Everyone write properly, and remember you're asking for help. Why will want to deal with you? Think about it.

Bad texting is serious enough. It can even be a reason for removing your post.
--SA

If you can't analyze existing code that you didn't write, how do you ever expect to develop anything resembling "skill" with regards to being a programmer?
 
Share this answer
 
Comments
shanthikalai 9-Mar-11 7:35am    
hmm ok if u knw the answer,then just explain,otherwise dont insult,bcoz nobody did't gt knowledge from their own thinking...even u...sory to say tis bcoz i'm a begginner,don't discourage me.....
#realJSOP 9-Mar-11 7:37am    
I don't care if you're a beginner or not. If you can't analyze someone else's code to determine what it's doing, you're doomed to a career change, such as sweeping standing water off sidewalks.
William Winner 9-Mar-11 13:06pm    
just an FYI, but people around here don't like textspeak. You'll lose a lot of respect writing like that.
Sergey Alexandrovich Kryukov 9-Mar-11 17:16pm    
No, you are not the beginner!
The beginners are those who begin, but you're the one who resist beginning.
Do begin or stop using this resource.
--SA
shanthikalai 9-Mar-11 7:53am    
hmm hello don't gt ovr ideas no one is talent by birth even u...
Line by Line? Do you have any idea how much work that involves?

It means writing a minimum of one sentence per line of code, and some of those lines will require a paragraph or so to explain them fully.

Try describing how to make a cup of tea in that much detail, and it will have gone cold (and probably mouldy) by the time you are finished!

Try and explain what your problem areas are instead, and we may be able to help.

"yes my ques is y we put dataaccess and if(Request.Files.Count != 0) this line plz explain...."

C#
if(Request.Files.Count != 0)

If you don't understand something and it has '.' characters in it, then break it down and look at MSDN with google:
Request is an object which retrieves the values that the client browser passed to the server: MSDN[^]
So Request.Files is an HttpRequest (which is what Request is an instance of) property : MSDN[^]
And Count is a standard property of all Collections, which tells you how many items it contains.

So Request.Files.Count tells you how many files were uploaded.

If this number is not zero, then execute the code block immediately below.

(Do you see now, how much work is involved in explaining an entire program, even one that small? :)
 
Share this answer
 
v2
Comments
Albin Abel 9-Mar-11 7:54am    
my 5
shanthikalai 9-Mar-11 7:55am    
Yes my question is why we put dataaccess and if(Request.Files.Count != 0). Please explain this line to me.
OriginalGriff 9-Mar-11 8:39am    
Answer updated
Albin Abel 9-Mar-11 8:02am    
That is just to check if any files were uploaded or not.
Sergey Alexandrovich Kryukov 9-Mar-11 17:17pm    
Agree, my 5.
See also my comment to John's answer about beginning.
This is not beginning!
--SA

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