Click here to Skip to main content
15,917,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I created a SQL Server Compact 3.5 database file (sword.sdf) I defined the connection string in VS 2008 as follows:
C#
Dim AppPath As String = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim ConnStr As String = "Data Source='" + AppPath + "\sword.sdf';Persist Security Info=True;password='12111964';"

I get an error message saying that "The file name is not a valid file name".
Nothing is wrong with the file name. I did test the database connection in server explorer. It connects to the database. But at runtime, I get the error message.

Do you know what could be causing the problem?

Thanks.
Posted
Updated 21-Feb-11 3:56am
v2
Comments
Sandeep Mewara 21-Feb-11 9:58am    
What is your ConnStr when you DEBUG it? Did you check that the string formed correctly?
JUNEYT 21-Feb-11 10:46am    
Connection string is okay as it can been seen in code section in my posted message. I think the error relates to something else rather than filename.
OriginalGriff 21-Feb-11 11:01am    
No, it can't: all we can see is the code to generate it. See my answer for details.

1 solution

I think if you check, that System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
returns a path preceded by "file:" which will confuse the normal processing.
Try using:
Dim AppPath As String = Directory.GetCurrentDirectory()
Use it at startup, and store your connection string in a class level variable so you don't have to re-generate it (CurrentDirectory can change while your App is running.)
 
Share this answer
 
Comments
JUNEYT 21-Feb-11 12:01pm    
Thank you very much. Absolutely you are right about it. The problem has been solved. :)
OriginalGriff 21-Feb-11 12:01pm    
Welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900