 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 22nd, 2007, 01:34 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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#")
|
|

April 23rd, 2007, 11:16 AM
|
|
Authorized User
|
|
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 23rd, 2007, 04:12 PM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

April 23rd, 2007, 04:13 PM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 24th, 2007, 07:09 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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
================================================== =========
|
|

April 25th, 2007, 06:07 PM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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."
|
|

April 26th, 2007, 07:04 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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
================================================== =========
|
|

April 26th, 2007, 09:47 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

April 26th, 2007, 10:00 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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
================================================== =========
|
|

April 29th, 2007, 11:52 PM
|
|
Authorized User
|
|
Join Date: Aug 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |