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. I added a column to a table, but, I can't see it.

I added a column to a table, but, I can't see it.

Scheduled Pinned Locked Moved Visual Basic
databasesysadminsecurity
7 Posts 3 Posters 0 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.
  • F Offline
    F Offline
    furjaw
    wrote on last edited by
    #1

    I ran the code below that adds a column to an SQL table. I refreshed Data Sources, but, the new column does not appear. I refreshed Server Explorer and the new column does not appear. I tried running the code again and I got: "Column names in each table must be unique. Column name 'CaseNbr' in table 'Patient' is specified more than once." so the column did actually get added, but, I can't see it. Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sqlConnection As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Patient.mdf;Integrated Security=True;User Instance=True") Dim str As String = "ALTER TABLE Patient ADD CaseNbr Varchar(50)" Dim command As New System.Data.SqlClient.SqlCommand(str, sqlConnection) sqlConnection.Open() command.ExecuteNonQuery() sqlConnection.Close() Close() End Sub End Class

    C D 2 Replies Last reply
    0
    • F furjaw

      I ran the code below that adds a column to an SQL table. I refreshed Data Sources, but, the new column does not appear. I refreshed Server Explorer and the new column does not appear. I tried running the code again and I got: "Column names in each table must be unique. Column name 'CaseNbr' in table 'Patient' is specified more than once." so the column did actually get added, but, I can't see it. Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sqlConnection As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Patient.mdf;Integrated Security=True;User Instance=True") Dim str As String = "ALTER TABLE Patient ADD CaseNbr Varchar(50)" Dim command As New System.Data.SqlClient.SqlCommand(str, sqlConnection) sqlConnection.Open() command.ExecuteNonQuery() sqlConnection.Close() Close() End Sub End Class

      C Offline
      C Offline
      Ch_Shahzad iqbal
      wrote on last edited by
      #2

      I Think Ur Code is Correct if u have any confusion then please review this Code thanks Dim cn As New SqlClient.SqlConnection cn.ConnectionString = "Data Source=ssi-SIQBAL;Initial Catalog=Dummy;integrated Security=SSPI" cn.Open() Dim cmd As New SqlClient.SqlCommand Dim str As String str = "ALTER TABLE TBL1 ADD HOME varchar(500)" cmd.Connection = cn cmd.CommandText = str cmd.ExecuteNonQuery()

      1 Reply Last reply
      0
      • F furjaw

        I ran the code below that adds a column to an SQL table. I refreshed Data Sources, but, the new column does not appear. I refreshed Server Explorer and the new column does not appear. I tried running the code again and I got: "Column names in each table must be unique. Column name 'CaseNbr' in table 'Patient' is specified more than once." so the column did actually get added, but, I can't see it. Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sqlConnection As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Patient.mdf;Integrated Security=True;User Instance=True") Dim str As String = "ALTER TABLE Patient ADD CaseNbr Varchar(50)" Dim command As New System.Data.SqlClient.SqlCommand(str, sqlConnection) sqlConnection.Open() command.ExecuteNonQuery() sqlConnection.Close() Close() End Sub End Class

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

        Is the MDF file listed in your project in Solution Explorer? The problem usually occurs because the .NDF file is being copied to your Release or Debug folders along with your app's .EXE. The Server Explorer is looking at one version of the database (in your project folder) and your code is looking at the other copy of your database in the bin\Release or bin\Debug folder. Wanna see? Just open your project folder and search all the subfolders for any files with the extension MDF.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        F 1 Reply Last reply
        0
        • D Dave Kreskowiak

          Is the MDF file listed in your project in Solution Explorer? The problem usually occurs because the .NDF file is being copied to your Release or Debug folders along with your app's .EXE. The Server Explorer is looking at one version of the database (in your project folder) and your code is looking at the other copy of your database in the bin\Release or bin\Debug folder. Wanna see? Just open your project folder and search all the subfolders for any files with the extension MDF.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          F Offline
          F Offline
          furjaw
          wrote on last edited by
          #4

          That was the problem. Another question: Why is it that in some projects, the .mdf file is in bin\Release and in other projects, the .mdf file is in the bin\Debug folder?

          D 1 Reply Last reply
          0
          • F furjaw

            That was the problem. Another question: Why is it that in some projects, the .mdf file is in bin\Release and in other projects, the .mdf file is in the bin\Debug folder?

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

            It'll be in either depending on which build configuration you use when you hit, well, Build.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            F 1 Reply Last reply
            0
            • D Dave Kreskowiak

              It'll be in either depending on which build configuration you use when you hit, well, Build.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              F Offline
              F Offline
              furjaw
              wrote on last edited by
              #6

              OK, I deleted the Patient.mdf in Server Explorer. Then I opened a New Connection and browsed to the .mdf file under the output directory (\bin\debug). Now I can see the new column in Server Explorer. But, how do I get it to appear in Data Sources of Solution Explorer?

              D 1 Reply Last reply
              0
              • F furjaw

                OK, I deleted the Patient.mdf in Server Explorer. Then I opened a New Connection and browsed to the .mdf file under the output directory (\bin\debug). Now I can see the new column in Server Explorer. But, how do I get it to appear in Data Sources of Solution Explorer?

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

                You don't want to do that. You just have to understand that the Server Explorer will look at the copy of the database in the Project folder and your running code will look at the copy in the bin\ folders. Why? Because when you build the project, by default, the database in the project folder will be copied to the bin\ folders every time you build the project. This will overwrite the bin\ folder copy, and any changes your code makes to it. If this is a permanent change you want to make to the project folder copy, either use the SQL Server Management Studio to make the change, or the Server Explorer, or make the change with your code, then copy the bin\folder version up to the project folder.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007

                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