tags:

views:

151

answers:

2

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

This is my code :

public static string DBFolder = Application.StartupPath + "\\SQL\\";
private void MainForm_Load(object sender, EventArgs 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
            }
        }
    }

BTW : I was using UFT8 characters in database's tables, like this : اقلام_فروخته_شده

Could you please guide me ? Thanks.

+2  A: 

StaticVariables.DBFolder contains one or more of the illegal Win32 path characters.

you can use

System.IO.Path.GetInvalidPathChars()
System.IO.Path.GetInvalidFileNameChars()

to check which characters are not allowed, and then remove those from your StaticVariables.DBFolder variable

also make sure that dbc.Mapping.DatabaseName doesn't contain any of these characters

John Leidegren
Thanks John, but I don't think so the problem is what you wrote !
Mohammad
Can you provide the stack trace then? That's would help us identify exactly where the problem is? The stack trace can be found by inspecting the StrackTrace property of the Exception thrown.
John Leidegren
I've added the StackTrace in next post.
Mohammad
A: 

Thanks. StackTrace of the exception :

   at System.IO.Path.CheckInvalidPathChars(String path)
   at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
   at System.IO.Path.NormalizePath(String path, Boolean 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 sender, EventArgs e) in D:\My Works\C#\Win Form\Reza Restaurant\RezaRestaurant\Forms\MainForm.cs:line 364
Mohammad