Ishtiaq Marwat
Ishtiaq Marwat

Reputation: 25

How to Save File Path in the MySQL Database Using string in C#

I have a picturebox control and also a imagepath textbox control.When I upload an image into the picturebox control then the full path of image in my computer is also pasted in the imagepath textbox control as

E:\Engineer\picture.jpg

but when I save this image and also its path in my database, then the path is saved without backslashes such as like this:

E:Engineerpicture.jpg

I am using string type for the path. This is the main problem which had stuck my developing process of application. If any other explaination is needed then kindly let me know. Thank You

Upvotes: 2

Views: 7207

Answers (2)

SamGhatak
SamGhatak

Reputation: 1493

I did a bit research...but could not find escaping a string after it is initialized as suggested in the other answer. So it leaves us with this option:

string newFileName = fileName.Replace("\\","\\\\");

Doing this before the DB insert should help you, but I am not sure if it is the best way to do this.

Upvotes: 2

user1666620
user1666620

Reputation: 4808

Backslashes are escaped characters.

You can instead escape the backslashes as below:

string filename = "E:\\Engineer\\picture.jpg";

Or use a string literal:

string filename = @"E:\Engineer\picture.jpg";

Further reading from MSDN: https://msdn.microsoft.com/en-us/library/h21280bw.aspx

Upvotes: 2

Related Questions