I want how to upload file in a folder of server.
and how to define its path.
Please check.Below line is working for localhost but not working for web
SaveLocation=Server.MapPath("~/Kamna Group Website/"+fn);
Is it ok ?
Is this path is relevent to access image in Image1.ImageUrl
i am giving my coading
void page_Load(Object sender, EventArgs e)
{
con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/kamnagroup.mdb") + "";
con.Open();
ds = new DataSet();
string sql = "select photo,agent,point,aname,branch from joining WHERE point=(SELECT MAX(point) FROM joining)";
da = new OleDbDataAdapter(sql, con);
da.Fill(ds, "joining");
DataRow dRow = ds.Tables["joining"].Rows[0];
System.Web.UI.WebControls.Label lbl2 = new System.Web.UI.WebControls.Label();
lbl2.Text = dRow.ItemArray.GetValue(0).ToString();
lb1.Text = dRow.ItemArray.GetValue(1).ToString();
lb2.Text = dRow.ItemArray.GetValue(2).ToString();
lb3.Text = dRow.ItemArray.GetValue(3).ToString();
lb4.Text = dRow.ItemArray.GetValue(4).ToString();
fn = System.IO.Path.GetFileName(lbl2.Text.Trim()); Image1.ImageUrl = fn;
con.Close();
}
void sub(Object s, EventArgs e)
{
con.Open();
Int32 x = 0; string y = GetRandomString(x);
int y1 = 0;
string y2 = "Code";
string s1 = t1.Text.Trim();
string m;
int r = 0;
DataRow[] rs;
rs = ds.Tables["joining"].Select("aname='" + s1 + "'");
r = rs.Length;
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
{
fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
fn = y + fn;
SaveLocation = Server.MapPath("~/Kamna Group Website/" + fn);
photo = SaveLocation;
com = con.CreateCommand();
com.CommandText = "Insert into joining(aname,fathname,dob,pob,add1,mob,city,state,pcode,panno,nominee,relation,photo,bank,branch,account,acctype,intby,codno,doi,agent,point,refno) values('" + t1.Text.Trim() + "','" + t2.Text.Trim() + "','" + t3.Text.Trim() + "','" + t4.Text.Trim() + "','" + texta1.Value + "','" + t7.Text.Trim() + "','" + t5.Text.Trim() + "','" + t6.Text.Trim() + "','" + t8.Text.Trim() + "','" + t11.Text.Trim() + "','" + t9.Text.Trim() + "','" + t10.Text.Trim() + "','" + photo + "','" + t12.Text.Trim() + "','" + t13.Text.Trim() + "','" + t14.Text.Trim() + "','" + d1.SelectedItem.Text + "','" + t15.Text.Trim() + "','" + t16.Text.Trim() + "','" + t17.Text.Trim() + "','" + y2 + "','" + y1.ToString() + "','" + y + "')";
try
{
File1.PostedFile.SaveAs(SaveLocation);
com.ExecuteNonQuery();
System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
lbl1.ForeColor = System.Drawing.Color.Blue;
lbl1.BackColor = System.Drawing.Color.Yellow;
lbl1.Text = "You have joined us.We will contect you soon!";
ph1.Controls.Add(lbl1);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
else
{
Response.Write("Please a file to upload");
}
con.Close();
}
private string GetRandomString(int seed)
{
const string alphabet = "abcdefghijklmnopqrstuvwxyz";
Random rnd = new Random((seed + DateTime.Now.Millisecond));
string result = rnd.Next(1000000, 9999999).ToString();
string alphaChar = alphabet.Substring(rnd.Next(0, alphabet.Length - 1), 1);
int replacementIndex = rnd.Next(0, (result.Length - 1));
result = result.Remove(replacementIndex, 1).Insert(replacementIndex, alphaChar);
return result;
}
[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]