Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 22nd, 2007, 01:34 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding a column to an existing SQL database table

Visual Basic 2005
I used DataSet Designer to add a column called "Case#" to an existing SQL table.
The table is used by a DataGridView in a VB 2005 program.
I got it working on my development computer in debug mode.
Then, when I installed it, I got an exception "Case# is an invalid column" and none of my data records appeared.

Adding the following statement did not help:
PatientDataSet.Patient.Columns.Add("Case#")
Reply With Quote
  #2 (permalink)  
Old April 23rd, 2007, 11:16 AM
Authorized User
 
Join Date: Oct 2005
Location: , , .
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If I understand your question correctly, as far as I know, when you added the column in DataSet Designer, the column was only added to that DataSet and not to the actual SQL Server table. Check your database if the "Case#" column is there.

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
Reply With Quote
  #3 (permalink)  
Old April 23rd, 2007, 04:12 PM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have a number of users running this application in remote locations.
Of course, they cannot lose their existing data.
It was requested that I include a column called "CaseNbr" in the table.
The file that I need to add the column to is an SQL table that is being updated via a DataGridView.
I think that it was a mistake to add the column via DataSet Designer, so, I restored my project from a backup eliminating that step.
So, now the column does not exist in the database and I am back to square one.
I would like to add the column to the SQL tables via my app so that my various users can have it added to their databases. Every user has his own database.
There is only one program that accesses the table and it is very simple.
The user selects (or adds) a row (Patient record) then the program simply passes that Patient's info to the main form of the application.
I know nothing about SQL. There are no SQL statements in my app. Everything is done by TableAdapter and BindingSource statements like:
Me.PatientTableAdapter.Fill(Me.PatientDataSet.Pati ent)
PatientBindingSource.Position = Form1.DefaultPatient
That's why I don't know how to add a column to the SQL table.

Reply With Quote
  #4 (permalink)  
Old April 23rd, 2007, 04:13 PM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I added the following statement to my program:

Me.PatientDataSet.Patient.Columns.Add("CaseNbr", Type.GetType("System.String"))

But it did not do anything.

I added the statement as the first command in the Form Load routine:

Public Class Patient
Public Sub Patient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.PatientDataSet.Patient.Columns.Add("CaseNbr", Type.GetType("System.String"))
Me.PatientTableAdapter.Fill(Me.PatientDataSet.Pati ent)
PatientBindingSource.Position = Form1.DefaultPatient
End Sub
Reply With Quote
  #5 (permalink)  
Old April 24th, 2007, 07:09 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

you will have to use SQL.. and this is a good time to start learning about it..

try w3schools for a nice tutorial: http://www.w3schools.com/sql/default.asp

anyway you will have to open a connection, create a command and do something like:
Code:
ALTER TABLE (TABLE_NAME) ADD COLUMN_NAME(TYPE)
the first time you start your program, but not the rest of the time. I recommend you to make another table in your DB that holds something like the version of the DB and start versioning it, so the next time if you find that the version is something (anything you like!) then you don't do this proccess...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
Reply With Quote
  #6 (permalink)  
Old April 25th, 2007, 06:07 PM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is how MSDN tells you to do it (I just added these statements to my Form Load routine):

Dim columns As DataColumnCollection = Me.PatientDataSet.Patient.Columns
If columns.Contains("CaseNbr") Then
MsgBox("'CaseNbr' column already exists" )
Else
Me.PatientDataSet.Patient.Columns.Add("CaseNbr", Type.GetType("System.String"))
End If

It bombed out with this message:
"An attempt to attach an auto-named database for file
C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\ProjectsOrthoLabRx\OrthoLabRx\bin\Debug\Patie nt.mdf failed.
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
Reply With Quote
  #7 (permalink)  
Old April 26th, 2007, 07:04 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

that error is solved closing and re opening the GUI...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
Reply With Quote
  #8 (permalink)  
Old April 26th, 2007, 09:47 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Somebody informed me that adding the column to the DataSet would not add it to the DataBase, so I wrote another small program (see below) as a new project. When I executed it, I got the exact same error message!

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("DataSource=.\ SQLEXPRESS;AttachDbFilename=|DataDirectory|\patien t.mdf;Integrated Security=True;User Instance=True")
      Dim command As New System.Data.SqlClient.SqlCommand
      command.CommandText = "ALTER TABLE Patient ADD CaseNbr Varchar(50)')"
      sqlConnection.Open()
      command.ExecuteNonQuery()
      sqlConnection.Close()
   End Sub
End Class

Here is the message:

An attempt to attach an auto-named database for file C:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\Add Column\Add Column\bin\Debug\patient.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.


Reply With Quote
  #9 (permalink)  
Old April 26th, 2007, 10:00 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi again... that error ocurrs when you are already using that database in another program (or you have it open elsewhere)...

close every instance and try again (be aware that if you open it in the IDE you will have to close the IDE to close the DB)...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
Reply With Quote
  #10 (permalink)  
Old April 29th, 2007, 11:52 PM
Authorized User
 
Join Date: Aug 2006
Location: chennai, tamilnadu, India.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to thillaiarasu
Default

hi for adding a column use this command
for example

CREATE TABLE Appointment
(AppointmentID Int,Description VarChar(50),StartDateTime DateTime ,EndDateTime DateTime,Resource VarChar(50) Null )
GO

 ALTER TABLE Appointment
 ADD LeadTime SmallInt Null GO





n.thillaiarasu
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding new field to a database table in run time dotnet cat ASP.NET 1.0 and 1.1 Basics 1 October 6th, 2005 01:36 PM
Adding rows, AND column(s) to existing datatable cliffd64 VB.NET 2002/2003 Basics 1 August 18th, 2005 06:50 AM
Adding existing and non-existing attributes spencer.clark XSLT 5 July 27th, 2005 04:02 PM
How can I update 2 rows in one column in SQL table babou Classic ASP Databases 6 March 9th, 2005 11:13 AM
Adding a New Table to a Database with ADO.NET Aaron Edwards ADO.NET 4 December 26th, 2004 03:04 AM



All times are GMT -4. The time now is 04:57 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.