Double backslash problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Double backslash problem

  1. #1
    Join Date
    May 2011
    Posts
    3

    Double backslash problem

    This should be a simple one for you guys to sort out.

    I have a simple app that prompts the user for a file path and then queries a database for records with that text in a column.

    It all works well - except for the old double slash issue.

    The values in the database only have single slashes. ("\")
    The text returned from the textbox has double slashes ("\\")

    I've tried doing a txtPath.Text.Replace(@"\\", @"\") but the value injected into the sql still has the \\ there.

    I know I can use the @ when hardcoding the literal string, but how do I do this when the value is entered into a textbox?

    fwi: I'm using VS2008 C#

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Can you post a few lines of code that demonstrate the issue?
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    May 2011
    Posts
    3
    Here's a cut down code snippet:

    Code:
    string sql = "select docnumber, docname, path, from documentlist where path= '[1]'"; 
    
    sql = sql.Replace("[1]", txtPath.Text.Replace(@"\\", @"\"));
    
    SqlDataAdapter da = new SqlDataAdapter(sql, conn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    txtPath is a textbox on the form.

  4. #4
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I think the best solution is to use a parameter in your query. This will also protect you against SQL injection attacks:
    Code:
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = new SqlCommand("select docnumber, docname, path, from documentlist where path = @Path", conn);
    da.SelectCommand.Parameters.AddWithValue("@Path", txtPath.Text);
    DataTable dt = new DataTable();
    da.Fill(dt);
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  5. #5
    Join Date
    May 2011
    Posts
    3
    Thanks Phil. I'll give it a go.
    I don't think any attacks are likely as I'm the only one with access to the app. Nevertheless, I should still follow best practice.

  6. #6
    Join Date
    Oct 2008
    Posts
    142

    escape character

    hi there,

    in dotnet, backslash character is used to escape other characters, like single and double quotes. therefore, you need to escape the backslash, so that you can tell dotnet you actually mean backslash character itself. it looks like you need to send four backslashes to represent backslash itself.

    here is info about escaping backslash in dotnet --> http://www.google.com/#sclient=psy&h...w=1280&bih=899

    good luck,

    tonci korsano

  7. #7
    Join Date
    Mar 2011
    Location
    New York
    Posts
    5

    Double backslash problem

    You can use the @ to not have to double up the backslashes or just double them up. I think he had the file in the wrong place. Bad backslashes usually mean syntax errors but his Message Box showed the right path.

Similar Threads

  1. Replies: 2
    Last Post: 02-11-2008, 01:19 PM
  2. Class vs structs design issues
    By rssmps in forum C++
    Replies: 4
    Last Post: 06-12-2006, 04:31 AM
  3. Replies: 8
    Last Post: 01-08-2006, 11:55 PM
  4. Need Help With Multi Function Program
    By WackoWolf in forum C++
    Replies: 4
    Last Post: 10-17-2005, 04:22 PM
  5. Replies: 3
    Last Post: 10-02-2005, 11:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center