Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Database connection problem in vb.net

Database connection problem in vb.net

Scheduled Pinned Locked Moved Visual Basic
csharphelpdatabasevisual-studio
14 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P P P Vilsad

    change the path to Application.startuppath & "\databasename" and try

    Vilsad P P MCTS (Windows Applications) .Net 2.0

    H Offline
    H Offline
    hbk_leo
    wrote on last edited by
    #3

    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:)

    P 1 Reply Last reply
    0
    • H hbk_leo

      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:)

      P Offline
      P Offline
      P P Vilsad
      wrote on last edited by
      #4

      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 again

      Vilsad P P MCTS (Windows Applications) .Net 2.0

      H 1 Reply Last reply
      0
      • H hbk_leo

        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.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #5

        The |DataDirectory| portion of the AttachDbFilename 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

        H 1 Reply Last reply
        0
        • D Dave Kreskowiak

          The |DataDirectory| portion of the AttachDbFilename 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

          H Offline
          H Offline
          hbk_leo
          wrote on last edited by
          #6

          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

          D 1 Reply Last reply
          0
          • P P P Vilsad

            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 again

            Vilsad P P MCTS (Windows Applications) .Net 2.0

            H Offline
            H Offline
            hbk_leo
            wrote on last edited by
            #7

            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.

            D P 2 Replies Last reply
            0
            • H hbk_leo

              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

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #8

              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

              H 1 Reply Last reply
              0
              • H hbk_leo

                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.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #9

                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

                H 1 Reply Last reply
                0
                • H hbk_leo

                  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.

                  P Offline
                  P Offline
                  P P Vilsad
                  wrote on last edited by
                  #10

                  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

                  H 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    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

                    H Offline
                    H Offline
                    hbk_leo
                    wrote on last edited by
                    #11

                    Hi thx for reply. as u suggested i serched it in sql server mgmt studio under databases but it is not listed there.

                    1 Reply Last reply
                    0
                    • P P P Vilsad

                      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

                      H Offline
                      H Offline
                      hbk_leo
                      wrote on last edited by
                      #12

                      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.

                      P 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        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

                        H Offline
                        H Offline
                        hbk_leo
                        wrote on last edited by
                        #13

                        Hi 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.

                        1 Reply Last reply
                        0
                        • H hbk_leo

                          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.

                          P Offline
                          P Offline
                          P P Vilsad
                          wrote on last edited by
                          #14

                          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

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups