-
October 25th, 2009, 12:05 PM
#1
"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.
-
October 25th, 2009, 04:01 PM
#2
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 + "\""; )
-
October 25th, 2009, 10:44 PM
#3
Re: "Illegal characters in path" Exception ?
Also consider using System.IO.Path.Combine(...);
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
-
October 27th, 2009, 11:23 AM
#4
Re: "Illegal characters in path" Exception ?
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 :
But the Exception has occurred again !!!
Could you please guide me more ?
-
October 27th, 2009, 11:41 AM
#5
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\"";
-
October 27th, 2009, 11:57 AM
#6
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 ?
-
October 27th, 2009, 12:46 PM
#7
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).
-
October 27th, 2009, 01:54 PM
#8
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.
-
October 27th, 2009, 01:57 PM
#9
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.
-
October 27th, 2009, 02:05 PM
#10
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.
-
October 27th, 2009, 02:07 PM
#11
Re: "Illegal characters in path" Exception ?
I asked you to restart the SQL service. Did you do that?
-
October 27th, 2009, 02:09 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|