in dault.cs part have i did this correctly ,seem to have alot od red underscores
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
private void StartUpLoad()
{
//get the file name of the posted image
string imgName = FileUpload1.FileName.ToString();
//sets the image path
string imgPath = "ImageStorage/" + imgName;
//then save it to the Folder
FileUpload1.SaveAs(Server.MapPath(imgPath));
//get the size in bytes that
int imgSize = FileUpload1.PostedFile.ContentLength;
//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
}
else
{
//save the file
//Call the method to execute Insertion of data to the Database
ExecuteInsert(imgName, imgSize, imgPath);
Response.Write("Save Successfully!");
}
}
}
private string GetConnectionString()
{
//sets the connection string from your web config file. "DBConnection" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString;
}
private void ExecuteInsert(string name, int size, string path)
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void StartUpLoad()
{
//get the file name of the posted image
string imgName = FileUpload1.FileName.ToString();
//sets the image path
string imgPath = "ImageStorage/" + imgName;
//then save it to the Folder
FileUpload1.SaveAs(Server.MapPath(imgPath));
//get the size in bytes that
int imgSize = FileUpload1.PostedFile.ContentLength;
//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
}
else
{
//save the file
//Call the method to execute Insertion of data to the Database
ExecuteInsert(imgName, imgSize, imgPath);
Response.Write("Save Successfully!");
}
}
}
private string GetConnectionString()
{
//sets the connection string from your web config file. "DBConnection" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString;
}
private void ExecuteInsert(string name, int size, string path)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO ImageInfo (ImageName, ImageSize, ImagePath) VALUES "
+ " (@ImgName,@ImgSize,@ImgPath)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter("@ImgName", SqlDbType.NVarChar, 50);
param[1] = new SqlParameter("@ImgSize", SqlDbType.BigInt, 9999);
param[2] = new SqlParameter("@ImgPath", SqlDbType.VarChar, 50);
param[0].Value = name;
param[1].Value = size;
param[2].Value = path;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
StartUpLoad();
}
}
richkyrs
Member
436 Points
500 Posts
can anyone tell me if this upload is correct ?
Apr 09, 2012 04:57 PM|LINK
i have followed this tuturial
http://geekswithblogs.net/dotNETvinz/archive/2009/08/02/uploading-and-storing-image-path-todatabase-and--image-to.aspx
in dault.cs part have i did this correctly ,seem to have alot od red underscores
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
private void StartUpLoad()
{
//get the file name of the posted image
string imgName = FileUpload1.FileName.ToString();
//sets the image path
string imgPath = "ImageStorage/" + imgName;
//then save it to the Folder
FileUpload1.SaveAs(Server.MapPath(imgPath));
//get the size in bytes that
int imgSize = FileUpload1.PostedFile.ContentLength;
//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
}
else
{
//save the file
//Call the method to execute Insertion of data to the Database
ExecuteInsert(imgName, imgSize, imgPath);
Response.Write("Save Successfully!");
}
}
}
private string GetConnectionString()
{
//sets the connection string from your web config file. "DBConnection" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString;
}
private void ExecuteInsert(string name, int size, string path)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO ImageInfo (ImageName, ImageSize, ImagePath) VALUES "
+ " (@ImgName,@ImgSize,@ImgPath)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter("@ImgName", SqlDbType.NVarChar, 50);
param[1] = new SqlParameter("@ImgSize", SqlDbType.BigInt, 9999);
param[2] = new SqlParameter("@ImgPath", SqlDbType.VarChar, 50);
param[0].Value = name;
param[1].Value = size;
param[2].Value = path;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
StartUpLoad();
}
}
Justin Dwyer
Participant
1107 Points
195 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 04:58 PM|LINK
You put your StartUpLoad() method inside your page load method which will not work, those need to be seperated.
richkyrs
Member
436 Points
500 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 05:01 PM|LINK
hi thanks justin can you show me how to seperate them just the tuturial doesnt show it
Justin Dwyer
Participant
1107 Points
195 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 05:18 PM|LINK
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private void StartUpLoad() { //get the file name of the posted image string imgName = FileUpload1.FileName.ToString(); //sets the image path string imgPath = "ImageStorage/" + imgName; //then save it to the Folder FileUpload1.SaveAs(Server.MapPath(imgPath)); //get the size in bytes that int imgSize = FileUpload1.PostedFile.ContentLength; //validates the posted file before saving if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") { if (FileUpload1.PostedFile.ContentLength > 5120) // 5120 KB means 5MB { Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true); } else { //save the file //Call the method to execute Insertion of data to the Database ExecuteInsert(imgName, imgSize, imgPath); Response.Write("Save Successfully!"); } } } private string GetConnectionString() { //sets the connection string from your web config file. "DBConnection" is the name of your Connection String return System.Configuration.ConfigurationManager.ConnectionStrings["RegConnectionString"].ConnectionString; } private void ExecuteInsert(string name, int size, string path) { SqlConnection conn = new SqlConnection(GetConnectionString()); string sql = "INSERT INTO ImageInfo (ImageName, ImageSize, ImagePath) VALUES " + " (@ImgName,@ImgSize,@ImgPath)"; try { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter[] param = new SqlParameter[3]; param[0] = new SqlParameter("@ImgName", SqlDbType.NVarChar, 50); param[1] = new SqlParameter("@ImgSize", SqlDbType.BigInt, 9999); param[2] = new SqlParameter("@ImgPath", SqlDbType.VarChar, 50); param[0].Value = name; param[1].Value = size; param[2].Value = path; for (int i = 0; i < param.Length; i++) { cmd.Parameters.Add(param[i]); } cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } } protected void Button1_Click(object sender, EventArgs e) { StartUpLoad(); } }richkyrs
Member
436 Points
500 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 05:30 PM|LINK
thanks justin no errors there , but i have red underscores under the name
FileUpload1 how can i solve that?
Justin Dwyer
Participant
1107 Points
195 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 05:54 PM|LINK
Make sure you have a file upload object on your asp page, from the link you posted you should have.
<asp:FileUpload ID="FileUpload1" runat="server" />
evello880
Member
226 Points
75 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 05:58 PM|LINK
see here what r u missing.
http://aspalliance.com/150_how_to_upload_files_in_asp_net
richkyrs
Member
436 Points
500 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 06:10 PM|LINK
yip justin i have that in my souce code
<asp:FileUpload ID="FileUpload1" runat="server" />
richkyrs
Member
436 Points
500 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 06:17 PM|LINK
thanks evello but that tuturial you gave me is vb im using c# webforms
Justin Dwyer
Participant
1107 Points
195 Posts
Re: can anyone tell me if this upload is correct ?
Apr 09, 2012 07:28 PM|LINK
If you have it i see no reason for it to be giving you an error., When you try to run it what does it do/say?