Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have designed a windows forms which has a textbox in it,in that textbox i will enter path and on click of button i will bind that data to database(mysql) and i am done with it.but if the entered path contains '\'(escape character) it will be ignored while binding for e.g, if E:\folder is path it will be entered as E:folder into database,i know using @ would solve the problem,but still i am not able to do that,i am using property to read n write the file path,can i append @ in the property itself??or is there any other way to do that??

C#
public stringFilePath
        {
            get {return this.FilePathTextBox.Text.Trim();}
            set {this.FilePathTextBox.Text = value;}
        }
Posted

Hi,

According to the following web site: MySql Strings you can make use of: '\\' -> A backslash ('\') character.'\\'

I think that would solve your problem.

Kind regards,
 
Share this answer
 
v2
Comments
girish sp 13-May-11 3:38am    
thanks very much for the link..
i will revert back if i find any prob,
thanks again,
girish sp 13-May-11 3:39am    
my 5
Programm3r 13-May-11 3:53am    
great stuff... :)
Unfortunately, @ is for constant strings.
You need to escape the string yourself...
 
Share this answer
 
Comments
girish sp 13-May-11 3:36am    
ok,i got..as my string varies for each entry i need to escape it every time..that is where i am struck..

any help would be appreciated.
girish sp 13-May-11 3:39am    
my 5
You can just use Replace to convert "\" to "\\" like this

C#
public string FilePath
        {
            get{return textBox1.Text.Trim();}
            set { this.textBox1.Text = value.Replace(@"\", @"\\"); }
        }


Hope this Helps

Here is the code for the whole form I used for testing :

C#
public string FilePath
        {
            get{return textBox1.Text.Trim();}
            set { this.textBox1.Text = value.Replace(@"\", @"\\"); }
        }
        public Form1()
        {
            InitializeComponent();
        }
        public void Save()
        {
            using (MySqlConnection con = new MySqlConnection("server=localhost;User Id=xxxx;password=xxxx;database=xxxx"))
            {
                MySqlCommand cmdSave = new MySqlCommand("UPDATE"
                                                          + " myTable"
                                                  + " SET"
                                                         + " FilePath = @FilePath"
                                                   + " WHERE"
                                                         + " myClassID = @myClassID", con);
                cmdSave.Parameters.AddWithValue("@myClassID", 1);
                cmdSave.Parameters.AddWithValue("@FilePath", FilePath);
                con.Open();
                int rowsAffected = cmdSave.ExecuteNonQuery();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Save();
        }


and it all works as expected.

Good Luck!!
 
Share this answer
 
v2
Comments
girish sp 13-May-11 4:27am    
if i enter E:\dir\another,my sql would take it as E:\dirnother,don know why??
Wayne Gaylard 13-May-11 4:34am    
I tested it with c:\folder1\folder2\folder3\folder4\test.txt and it works fine on MySQL 5.1 using .Net connector 6.3.3.0. I think you may have entered the info incorrectly maybe?
girish sp 13-May-11 5:15am    
when i see the value at run time before passing it to a method ,it would still be E:\folder1\folder2 ,i don think its replacing the char??
girish sp 13-May-11 5:16am    
if i pass E:\\folder1\\folder2,in textbox,in mysql it would display E:\folder1\folder2
girish sp 13-May-11 5:15am    
i m using sharpdevelop and mysql workbench
Check out static string MySqlHelper.EscapeString(string value) it takes care of must escaping on a string. But better is to use MySqlParameter.
 
Share this answer
 
Comments
girish sp 13-May-11 5:21am    
where should i use this??i am passing path to a method,before passing as an argument it should consider \ in the path?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900