I assume that your code is not quite like this:
string filename = Server.MapPath("~/photo") +flbload.PostedFile.FileName;
As This should not generate a "unrecognised escape sequence" error. It would if you had a "\" instead of the "/" character though...
Try putting an "@" in front of the string - that turns off escape character processing, and may make it work better for you:
string filename = Server.MapPath(@"~/photo") + flbload.PostedFile.FileName;
"hi
the page run but i have use local host
Ex string filename = Server.MapPath(@"~/D:\photo") + flbload.PostedFile.FileName;
but error path is in valid .i have use file upload control"
No, that won't work.
The whole idea of Server.MapPath is to convert an internal URL based path to a PC based file structure path. The '~' bit is the URL based bit for the root directory of your website.
So what you are asking for is the path:
"C:\websites\users\UID123456\root/D:\photo" which is seriously not going to work as it will complain about the colon in the middle of the path...
In addition, you need to separate the path from the filename with a further slash or backslash.
The way I would do it:
if (flbload.HasFile)
{
string filename = Server.MapPath(@"~/photo") + "/" + flbload.FileName;
flbload.SaveAs(filename);
}
"can you post edit in my code i have totally confused i am begineer"
I could, but then the context to other answers is lost. Instead, here is your code modified by mine:
public void onflbload(object sender, EventArgs e)
{
if (flbload.HasFile)
{
string filename = Server.MapPath(@"~/Resources/Images") + "/" + flbload.FileName;
flbload.SaveAs(filename);
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = @"Data Source=DEVI\SQLEXPRESS; Initial Catalog =cat; Integrated Security=SSPI";
try
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("insert into tblphotosettings " +
"(BillNo,CustomerName,Address,StartDate,EndDate,Systemurl,Numberofcopies,Amount,Total) " +
"values (@BillNo,@CustomerName,@Address,@StartDate,@EndDate,@Systemurl,@Numberofcopies,@Amount,@Total)",
connection))
{
cmd.Parameters.AddWithValue("@BillNo", TextBox1.Text);
cmd.Parameters.AddWithValue("@CustomerName", TextBox2.Text);
cmd.Parameters.AddWithValue("@Address", TextBox3.Text);
cmd.Parameters.AddWithValue("@StartDate", Rdbsdate.SelectedDate);
cmd.Parameters.AddWithValue("@EndDate", Rdbddate.SelectedDate);
cmd.Parameters.AddWithValue("@Systemurl", filename);
cmd.Parameters.AddWithValue("@Numberofcopies", TextBox7.Text);
cmd.Parameters.AddWithValue("@Amount", TextBox8.Text);
cmd.Parameters.AddWithValue("@Total", TextBox9.Text);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
LogError(ex.Message);
}
finally
{
connection.Close();
}
}
}
}
You will see I have made a number of changes!
1) There is the mod I discussed above.
2) I have used
using
blocks to ensure that the SqlConnection and SqlCommand are properly closed and disposed.
3) I have changed your Parameters.Add to Paramaters.AddWithValue: the former is depreciated and should be replaced with the latter - it is easier to read!
4) I have included a
catch
block: this way I will find out that an error occurred, even if the user doesn't! Please, try very hard not to omit catches, as they can help you find a problem early, rather than hidding it until it is a catastrophe later!
Two other things:
1) My LogEntry class does not exist in your code: replace the call with your appropriate action: add it to a text log file, or something so you can review it later and work out what error has occurred!
2) Please, please, please, change the names of your controls! TextBox8 may hold your "amount" but it is a lot more obvious if you write
cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = tbAmount.Text;
Rather than
cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = TextBox8.Text;
It just makes it easier to see that you are using the correct text box in the correct place - and you don't have to look back in your design to find out which text box holds the number of copies... :laugh:
"sorry i dont know text log file"
Try this:
using (StreamWriter sw = File.AppendText(Server.MapPath(@"~") + @"/MyLog.Txt"))
{
sw.WriteLine(ex.Message);
}
It's not ideal (it puts it in a generally available location) but it'll do...
(I use a database for my logs, which is a bit more complex to set up than you want at the moment)