Database connection problem in vb.net
-
Hi to all I have a problem with database in vb.net using sqlexpress. My problem is: I have created a.mdf database file and tables in it, and written code for inserting data to this table through command object. But my real prob is when i supply the connection string which is created by the "data configuration wizard" to this cmd object then on runtime insert executes and also datais visible in datagridview but when i check the data in the table it is not there and all fields are NULL. given connection string. ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LIB.mdf;Integrated Security=True;User Instance=True") But on the otherhand if i give connection string as :: "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\LIB\LIB\LIB.mdf;Integrated Security=True;User Instance=True" then data is successfully inserted in the database and i m also able to view it in my database table. So if Any one can help me it will be highly appreciable. Thanks.
-
Hi to all I have a problem with database in vb.net using sqlexpress. My problem is: I have created a.mdf database file and tables in it, and written code for inserting data to this table through command object. But my real prob is when i supply the connection string which is created by the "data configuration wizard" to this cmd object then on runtime insert executes and also datais visible in datagridview but when i check the data in the table it is not there and all fields are NULL. given connection string. ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LIB.mdf;Integrated Security=True;User Instance=True") But on the otherhand if i give connection string as :: "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\LIB\LIB\LIB.mdf;Integrated Security=True;User Instance=True" then data is successfully inserted in the database and i m also able to view it in my database table. So if Any one can help me it will be highly appreciable. Thanks.
change the path to Application.startuppath & "\databasename" and try
Vilsad P P MCTS (Windows Applications) .Net 2.0
-
change the path to Application.startuppath & "\databasename" and try
Vilsad P P MCTS (Windows Applications) .Net 2.0
-
hi thks for your reply. I ttied it but it did not worked. Please can u give it with an example o how to declare it. Thanks:)
try this
dim constring as string = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & application.startuppath & "\LIB.mdf;Integrated Security=True;User Instance=True"
connect with this string Note : after exiting application you need to click on server explorer and right click lib.mdf database and click on close connection and copy lib.mdf and lib.log files from debug folder to back to lib folder. otherwise you will not get the changes and the file will be overwritten with the old one when you are running your application againVilsad P P MCTS (Windows Applications) .Net 2.0
-
Hi to all I have a problem with database in vb.net using sqlexpress. My problem is: I have created a.mdf database file and tables in it, and written code for inserting data to this table through command object. But my real prob is when i supply the connection string which is created by the "data configuration wizard" to this cmd object then on runtime insert executes and also datais visible in datagridview but when i check the data in the table it is not there and all fields are NULL. given connection string. ("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\LIB.mdf;Integrated Security=True;User Instance=True") But on the otherhand if i give connection string as :: "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\LIB\LIB\LIB.mdf;Integrated Security=True;User Instance=True" then data is successfully inserted in the database and i m also able to view it in my database table. So if Any one can help me it will be highly appreciable. Thanks.
The
|DataDirectory|
portion of theAttachDbFilename
path is only usable if your database file is in either the Data folder of the SQL Server Program Files folder or when using ClickOnce Deployment - I can't remember which. You have to replace this with the full path to your database .MDF file for the connection string to work. If your database is in your applications .EXE folder, you can do something like this to get it work:Dim dbPath As String = Path.Combine(Application.StartupPath, "mydatabase.mdf") Dim connString As String = String.Format("Data Source=.\\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;User Instance=True", dbPath)
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
The
|DataDirectory|
portion of theAttachDbFilename
path is only usable if your database file is in either the Data folder of the SQL Server Program Files folder or when using ClickOnce Deployment - I can't remember which. You have to replace this with the full path to your database .MDF file for the connection string to work. If your database is in your applications .EXE folder, you can do something like this to get it work:Dim dbPath As String = Path.Combine(Application.StartupPath, "mydatabase.mdf") Dim connString As String = String.Format("Data Source=.\\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;User Instance=True", dbPath)
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
try this
dim constring as string = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & application.startuppath & "\LIB.mdf;Integrated Security=True;User Instance=True"
connect with this string Note : after exiting application you need to click on server explorer and right click lib.mdf database and click on close connection and copy lib.mdf and lib.log files from debug folder to back to lib folder. otherwise you will not get the changes and the file will be overwritten with the old one when you are running your application againVilsad P P MCTS (Windows Applications) .Net 2.0
Hi When i try the constring u suggested i am getting another error at the point of cn.open, the error is as : DriveLetter:\Project Folder\bin\Debug\LIB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. what is this error about i cant understand.
-
hi Thxs for u r reply. Can u plz tell what is the key word "Path.combine" because when i m writing the string u told about a message is displayed "Path" is not declared . So let me know ,do i have to import some namespace or what should i do. Regards
The Path class is defined in the System.Io namespace. Stick this at the top of your code:
Imports System.Io
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Hi When i try the constring u suggested i am getting another error at the point of cn.open, the error is as : DriveLetter:\Project Folder\bin\Debug\LIB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. what is this error about i cant understand.
Either that path doesn't exist or Sql Server already has that database attached. Use the SQL Server Management Studio to see if it's listed under the Databases folder in the Object Explorer.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Hi When i try the constring u suggested i am getting another error at the point of cn.open, the error is as : DriveLetter:\Project Folder\bin\Debug\LIB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. what is this error about i cant understand.
just add msgbox(constring) in my previous code after constructing constring. and run the application. varify that the message you are getting is pointing to the exact location of your database
Vilsad P P MCTS (Windows Applications) .Net 2.0
-
Either that path doesn't exist or Sql Server already has that database attached. Use the SQL Server Management Studio to see if it's listed under the Databases folder in the Object Explorer.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
just add msgbox(constring) in my previous code after constructing constring. and run the application. varify that the message you are getting is pointing to the exact location of your database
Vilsad P P MCTS (Windows Applications) .Net 2.0
-
The Path class is defined in the System.Io namespace. Stick this at the top of your code:
Imports System.Io
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Hi by including IO class the error was removed but now i m getting this error at the point of cn.open, the error is as : DriveLetter:\Project Folder\bin\Debug\LIB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. what is this error about i cant understand.
-
I tried this and also got the database path correct but still i m getting the same error msg about auto-named and UNC share.
what is exactly you are getting about autonamed and unc shares ? the error you are getting about the database name already exist is because it is already attached to sqlserver. if you have connected your database in server explorer, try to detach it and run this program error, i am not getting this error when i tried here. if this problem still exist after you detach db try after installing visual studio sp1
Vilsad P P MCTS (Windows Applications) .Net 2.0