|
|
ASP.NET C#
Started by hanusoft at 09-21-2007 12:39 AM. Topic has 3 replies.
 
 
|
|
Sort Posts:
|
|
|
|
09-21-2007, 12:39 AM
|
hanusoft
Joined on 09-17-2007
Posts 5
|
|
|
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
Joined on 06-10-2008
Posts 4
|
Re: Example of Image Upload
|
|
|
|
|
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
Joined on 08-09-2008
U.S.A.
Posts 16
|
Re: Example of Image Upload
|
|
|
|
|
<BLOCKQUOTE><table width="85%"><tr><td class="txt4"><img src="/Themes/default/images/icon-quote.gif"> <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
Joined on 09-22-2008
Posts 1
|
Re: Example of Image Upload
|
|
|
|
|
|
|
|
|
Webhost4life Fo... » General Discuss... » ASP.NET C# » Re: Example of Image Upload
|
|
|
|