"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 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
}
}
}
Stack Trace :
PHP Code:
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
Could you please guide me ? Thanks.
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 + "\""; )
Re: "Illegal characters in path" Exception ?
Also consider using System.IO.Path.Combine(...);
1 Attachment(s)
Re: "Illegal characters in path" Exception ?
Quote:
Originally Posted by
Arjay
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 :
Attachment 25588
But the Exception has occurred again !!! :confused:
Could you please guide me more ?
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\"";
Re: "Illegal characters in path" Exception ?
Thanks Arjay.
I fixed the code :
PHP Code:
private void MainForm_Load(object sender, EventArgs 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 ?
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:
= 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).
Re: "Illegal characters in path" Exception ?
I modified the code :
PHP Code:
private void MainForm_Load(object sender, EventArgs 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.
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.
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 sender, EventArgs 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.
Re: "Illegal characters in path" Exception ?
I asked you to restart the SQL service. Did you do that?
Re: "Illegal characters in path" Exception ?
Yes, I've done it.
I restarted SQL Express Service and tested the code several times.