Welcome to Webhost4life Forum Sign in | Join | Faq

ASP.NET C#

Started by hanusoft at 09-21-2007 12:39 AM. Topic has 3 replies.

Print Search
Sort Posts:    
   09-21-2007, 12:39 AM
hanusoft is not online. Last active: 12/20/2007 7:38:34 PM hanusoft

Top 50 Posts
Joined on 09-17-2007
Posts 5
Example of Image Upload
Reply Quote
    Here we are uploading images in File System and storing path in the database.
 
http://www.hanusoftware.com

Code (ImageUpload.aspx.cs) :-
private void Button1_Click(object sender, System.EventArgs e)
        {
    //    Here I am uploading images in Images folder of C drive.
int intResult=0;
string strPath = @"c:\Images\"+Path.GetFileName(File1.PostedFile.FileName);
SqlConnection con = new SqlConnection("server=.;uid=sa;database=pubs;pwd=");
SqlCommand com = new SqlCommand("Insert into Category(name,imagepath) values(@name,@imagepath)",con);
            com.Parameters.Add("@name",TextBox1.Text);
            com.Parameters.Add("@imagepath",strPath);
            con.Open();
            intResult =  Convert.ToInt32(com.ExecuteNonQuery());
            if(intResult != 0)
            {
                File1.PostedFile.SaveAs(strPath);
                Response.Write("Record Inserted.");
            }
        }




Software Development Company



Offshore Software


   Report 
   06-10-2008, 12:35 AM
Milo is not online. Last active: 6/10/2008 3:24:29 PM Milo

Top 75 Posts
Joined on 06-10-2008
Posts 4
Stick out tongue [:P] Re: Example of Image Upload
Reply Quote
Hope you don't mind...

Here's the same as the above, but this one won't memory leak.

private void Button1_Click(object sender, System.EventArgs e)
{
// Here I am uploading images in Images folder of C drive.
int intResult=0;
string strPath = @"c:\Images\"+Path.GetFileName(File1.PostedFile.FileName);
using (SqlConnection con = new SqlConnection("server=.;uid=sa;database=pubs;pwd="))
{
using (SqlCommand com = new SqlCommand("Insert into Category(name,imagepath) values(@name,@imagepath)",con))
{
com.Parameters.Add("@name",TextBox1.Text);
com.Parameters.Add("@imagepath",strPath);
con.Open();
intResult = Convert.ToInt32(com.ExecuteNonQuery());
if(intResult != 0)
{
File1.PostedFile.SaveAs(strPath);
Response.Write("Record Inserted.");
}
}
}
}


(Excuse the formatting, this textbox is too damn small.)
   Report 
   08-08-2008, 6:56 PM
Kevinul is not online. Last active: 8/20/2008 5:41:02 PM Kevinul

Top 10 Posts
Joined on 08-09-2008
U.S.A.
Posts 16
Re: Example of Image Upload
Reply Quote
<BLOCKQUOTE><table width="85%"><tr><td class="txt4"><img src="/Themes/default/images/icon-quote.gif">&nbsp;<strong>Milo wrote:</strong></td></tr><tr><td class="quoteTable"><table width="100%"><tr><td width="100%" valign="top" class="txt4">Hope you don't mind...

Here's the same as the above, but this one won't memory leak.

private void Button1_Click(object sender, System.EventArgs e)
{
// Here I am uploading images in Images folder of C drive.
int intResult=0;
string strPath = @"c:\Images\"+Path.GetFileName(File1.PostedFile.FileName);
using (SqlConnection con = new SqlConnection("server=.;uid=sa;database=pubs;pwd="))
{
using (SqlCommand com = new SqlCommand("Insert into Category(name,imagepath) values(@name,@imagepath)",con))
{
com.Parameters.Add("@name",TextBox1.Text);
com.Parameters.Add("@imagepath",strPath);
con.Open();
intResult = Convert.ToInt32(com.ExecuteNonQuery());
if(intResult != 0)
{
File1.PostedFile.SaveAs(strPath);
Response.Write("Record Inserted.");
}
}
}
}


(Excuse the formatting, this textbox is too damn small.)</td></tr></table></td></tr></table></BLOCKQUOTE>

So all you did was add "using" and "()" around the SqlCommand method (method right)? I'm new to C# (and also ASP.NET), and so what causes a memory leak and how did a memory (or does) it accure during it's initial run?
Regards,
Kevin K.
   Report 
   09-22-2008, 4:22 AM
jim123 is not online. Last active: 9/22/2008 7:19:08 PM jim123

Not Ranked
Joined on 09-22-2008
Posts 1
Re: Example of Image Upload
Reply Quote
Hi,
 
  quiet good.  Thanks for this code really helpful for all.........

http://www.infysolutions.com

Outsourcing software development company

   Report 
Webhost4life Fo... » General Discuss... » ASP.NET C# » Re: Example of Image Upload

Powered by Community Server, by Telligent Systems