CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2008
    Posts
    57

    "Illegal characters in path" Exception ?

    Hi. I'm using LINQ to SQL.
    I wanna create a database with LINQ but the following exception has occurred : Illegal characters in path.

    My snippet code :
    PHP Code:
    public static string DBFolder Application.StartupPath "\\SQL\\";   
    //the path is equal with "D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\bin\Release\SQL"
    private void MainForm_Load(object senderEventArgs e)
    {
        if (!
    Directory.Exists(StaticVariables.DBFolder))
            
    Directory.CreateDirectory(StaticVariables.DBFolder);
        
    using (RezaRestaurant.SQL.DataClasses1DataContext dbc = new RezaRestaurant.SQL.DataClasses1DataContext())
        {
            if (!
    File.Exists(StaticVariables.DBFolder dbc.Mapping.DatabaseName ".mdf") && !dbc.DatabaseExists())
            {
                
    RezaRestaurant.SQL.DataClasses1DataContext db = new RezaRestaurant.SQL.DataClasses1DataContext(StaticVariables.DBFolder dbc.Mapping.DatabaseName ".mdf"dbc.Mapping.MappingSource);
                
    db.CreateDatabase();//Exception
            
    }
        }

    Stack Trace :

    PHP Code:
    at System.IO.Path.CheckInvalidPathChars(String path)
       
    at System.IO.Path.NormalizePathFast(String pathBoolean fullCheck)
       
    at System.IO.Path.NormalizePath(String pathBoolean fullCheck)
       
    at System.IO.Path.GetFullPathInternal(String path)
       
    at System.IO.Path.GetFullPath(String path)
       
    at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.CreateDatabase()
       
    at System.Data.Linq.DataContext.CreateDatabase()
       
    at RezaRestaurant.MainForm.MainForm_Load(Object senderEventArgs ein D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\Forms\MainForm.cs:line 364 
    Could you please guide me ? Thanks.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: "Illegal characters in path" Exception ?

    Take the path and put it into a string variable and then look at it in the debugger. Then use the same path in all subsequent method calls.

    Code:
     
    string path = StaticVariables.DBFolder + dbc.Mapping.DatabaseName + ".mdf";
    When you look at it in a debugger, make sure it's properly formed and has the appropriate backspace characters.

    Also since the path contains spaces, you may need to double quote the string. (e.g. path = "\"" + path + "\""; )

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: "Illegal characters in path" Exception ?

    Also consider using System.IO.Path.Combine(...);
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  4. #4
    Join Date
    Jun 2008
    Posts
    57

    Re: "Illegal characters in path" Exception ?

    Quote Originally Posted by Arjay View Post
    Take the path and put it into a string variable and then look at it in the debugger. Then use the same path in all subsequent method calls.

    Code:
     
    string path = StaticVariables.DBFolder + dbc.Mapping.DatabaseName + ".mdf";
    When you look at it in a debugger, make sure it's properly formed and has the appropriate backspace characters.

    Also since the path contains spaces, you may need to double quote the string. (e.g. path = "\"" + path + "\""; )
    Hi and thanks.
    I've done it :

    Name:  pic1.png
Views: 3770
Size:  14.0 KB
    But the Exception has occurred again !!!
    Could you please guide me more ?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: "Illegal characters in path" Exception ?

    In your code snippet, looks like you're missing some code. Note that you aren't using the path anywhere with respect to the database code.

    Code:
     
    if (!Directory.Exists(StaticVariables.DBFolder))
            Directory.CreateDirectory(StaticVariables.DBFolder);
    
    using (RezaRestaurant.SQL.DataClasses1DataContext dbc = new RezaRestaurant.SQL.DataClasses1DataContext())
    {
      string path = StaticVariables.DBFolder + dbc.Mapping.DatabaseName + ".mdf";
      if (!File.Exists( path ))
      {
        dbc.CreateDatabase();  // Path never was passed in to DataClasses1DataContext
      }
    }
    If this still gives you trouble, then double quote the string.

    Code:
     
    string path = "\"" + StaticVariables.DBFolder + dbc.Mapping.DatabaseName + ".mdf\"";

  6. #6
    Join Date
    Jun 2008
    Posts
    57

    Re: "Illegal characters in path" Exception ?

    Thanks Arjay.
    I fixed the code :

    PHP Code:
    private void MainForm_Load(object senderEventArgs e)
    {
        
    RezaRestaurant.SQL.DataClasses1DataContext dbc = new RezaRestaurant.SQL.DataClasses1DataContext();
        if (!
    Directory.Exists(StaticVariables.DBFolder))
            
    Directory.CreateDirectory(StaticVariables.DBFolder);
        
    string path StaticVariables.DBFolder dbc.Mapping.DatabaseName ".mdf";
        
    dbc = new RezaRestaurant.SQL.DataClasses1DataContext(path);
        if (!
    File.Exists(path))
        {
            
    dbc.CreateDatabase();//Exception
        
    }

    But now, this Exception occurred :
    Code:
    Database 'D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\bin\Release\SQL\RRDB.mdf' already exists. Choose a different database name.
    I'm dead sure the database isn't exist in the path.
    What's wrong with it ?

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: "Illegal characters in path" Exception ?

    To help track it down, try prepending some characters to the .mdf extension to ensure the db name is unique.

    Code:
    string path
    Code:
    = StaticVariables.DBFolder + dbc.Mapping.DatabaseName + "_1.mdf";


    I wonder if a database with the same name exists in SQL server. If it does, you may have check SQL server to find out if it exists (and not just check the file path).

  8. #8
    Join Date
    Jun 2008
    Posts
    57

    Re: "Illegal characters in path" Exception ?

    I modified the code :

    PHP Code:
    private void MainForm_Load(object senderEventArgs e)
    {
        
    RezaRestaurant.SQL.DataClasses1DataContext dbc = new RezaRestaurant.SQL.DataClasses1DataContext();
        if (!
    Directory.Exists(StaticVariables.DBFolder))
            
    Directory.CreateDirectory(StaticVariables.DBFolder);
        
    string path StaticVariables.DBFolder dbc.Mapping.DatabaseName "_1.mdf";
        if (!
    File.Exists(path))
        {
            
    dbc = new RezaRestaurant.SQL.DataClasses1DataContext(path);
            
    dbc.CreateDatabase();//Exception
        
    }

    The first one after modifying the code runs without any exceptions and the DB built successfully. after that I deleted the Data Base again and ran the project. The same Exception has occurred again !
    BTW: I'm using SQL Express 2008.

    Code:
    Database 'D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\bin\Release\SQL\RRDB_1.mdf' already exists. Choose a different database name.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: "Illegal characters in path" Exception ?

    Try restarting the SQL Express service (from the services control panel applet) and try again. If this works, let me know.

  10. #10
    Join Date
    Jun 2008
    Posts
    57

    Re: "Illegal characters in path" Exception ?

    I've done it. Unfortunately the same Exception has occurred.
    I modified the code again :

    PHP Code:
    private void MainForm_Load(object senderEventArgs e)
    {
        
    RezaRestaurant.SQL.DataClasses1DataContext dbc = new RezaRestaurant.SQL.DataClasses1DataContext();
        if (!
    Directory.Exists(StaticVariables.DBFolder))
            
    Directory.CreateDirectory(StaticVariables.DBFolder);
        
    string path StaticVariables.DBFolder dbc.Mapping.DatabaseName ".mdf";
        if (!
    File.Exists(path))
        {
            
    dbc = new RezaRestaurant.SQL.DataClasses1DataContext(path);
            
    dbc.DeleteDatabase();//Exception
            
    dbc.CreateDatabase();
        }

    This time the Exception is :
    Code:
    An attempt to attach an auto-named database for file D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\bin\Release\SQL\RRDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: "Illegal characters in path" Exception ?

    I asked you to restart the SQL service. Did you do that?

  12. #12
    Join Date
    Jun 2008
    Posts
    57

    Re: "Illegal characters in path" Exception ?

    Yes, I've done it.
    I restarted SQL Express Service and tested the code several times.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured