Click here to Skip to main content
15,909,091 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,and user wont enter \\ when entering a path,how to solve this prob,


<pre lang="cs">private string filepath(string FilePath)
        {
            char[] splitpath=FilePath.ToCharArray();;
            for(int i=0;i<splitpath.Length;i++)
            {
                char s=splitpath[i];
                if (c == '\')//error at this line
                   {
                 string path= s.Replace('\' , '\\');//error at this line also
                   }
            }
        return path;
        }




even this code is giving error

errors are
too many character in character literal(CS1012)
new line in constant(CS1010)
unexpected character(CS1056)

thanks in advance,
Posted
Updated 5-May-11 19:31pm
v5

user wont enter \\ when entering a path
Yes true. But you can handle it internally in the code! What say? :)

Try:
C#
private string FormattedFilepath(string FilePath)
{
   string path= FilePath.Replace('\' , '\\');
   return path;
}
 
Share this answer
 
v3
Comments
girish sp 6-May-11 0:33am    
i know that,that's where i am facing prob,
Sandeep Mewara 6-May-11 1:03am    
Which part of your question speaks of that? If you are facing problem with that then you should share what you were trying and found not working for you.

Tried '@' before the string?
girish sp 6-May-11 1:20am    
i have shared the code,pls check out and let me know
Sandeep Mewara 6-May-11 1:25am    
You forgot to add what error!
girish sp 6-May-11 1:32am    
sorry,,i have added what errors i got!
Hi there,

Please, help me understand your question. You are trying to write a file path to the database right?

I just tested, the textbox returns an escaped string instead of the value you just entered. For instance if you entered "C:\Data.txt", the value returned is "C:\\Data.txt".

Next thing is, if you used "DbParameter" class to supply the file path parameter instead of constructing your query with string concatenation, it will automatically add required escape characters :) I.e. it would transform the value "C:\\Data.txt" to "C:\\\\Data.txt" and send it to the database.

Let me know if I am missing anything. Sadly, I don't have any database server installed in this PC so I could do a simple sample for you and post :\

Hope this helps :) Regards
 
Share this answer
 
Comments
girish sp 6-May-11 3:12am    
i am using the mysql database ,but unfortunately if you enter C:\data.txt it will be considered as C:data.txt
as merwara said i tried to insert@before the string it enters as @C:data.txt
i know @ works but i dont know the way to use with the string
CodeHawkz 6-May-11 3:29am    
Wow. I have done many applications which involved storing file paths in the database, but a similar problem never occurred to me. May be this is a MySQL issue, I know not.

I'd surely go home, try it out myself, and post here.

On a final note, can you post the code in which you get the file path and pass it on to MySQL (database code part?). If it's a glitch in your code, I'd be able to help you sooner.

Regards
girish sp 6-May-11 4:13am    
public int Insert(int printerid,int id,string FilePath,string FileName)
{
MySqlCommand cmd=null;
try
{
con.Open();
string str=string.Format("INSERT INTO tbl_Driver(tbl_PrinterName_id_fk,tbl_id_fk,tbl_filePath,tbl_fileName)VALUES('{0}','{1}','{2}','{3}')",printerid,id,FilePath,FileName);
cmd=new MySqlCommand(str,con);
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if(con!=null)
{
con.Close();
con.Dispose();
}
if(cmd!=null)
{
cmd.Dispose();
}
}
}

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