?
Solved

Inserting data into a SQL table from a C# program

Posted on 2010-08-27
9
Medium Priority
?
211 Views
Last Modified: 2013-12-17
Greetings,

As the question states, I'd like to insert data into a table using the AdventureWorks database.  I would like to do this using a grid on a Windows Form application.

I have created the application and I'm ready to drag a grid to the Form.  These fields would be inserted into the Persons.Contact table:

      [FirstName] [dbo].[Name] NOT NULL,
      [LastName] [dbo].[Name] NOT NULL,
      [EmailPromotion] [int] NOT NULL CONSTRAINT
      [PasswordHash] [varchar](40) NOT NULL,
      [PasswordSalt] [varchar](10) NOT NULL,
      [rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT
       [ModifiedDate] [datetime] NOT NULL CONSTRAINT

I've chosen these fields because I have no choice given they are not NULL.  The entire table definition is located in the code below.

I've put this question in the C# area hoping to get the necessary answers for both the C# code and the database.   Please don't assume I know anything in regard to the grid or how to configure the grid to accept data when the user inputs the data.

Thank you very much  in advance for this help!



[ContactID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
	[NameStyle] [dbo].[NameStyle] NOT NULL CONSTRAINT [DF_Contact_NameStyle]  DEFAULT ((0)),
	[Title] [nvarchar](8) NULL,
	[FirstName] [dbo].[Name] NOT NULL,
	[MiddleName] [dbo].[Name] NULL,
	[LastName] [dbo].[Name] NOT NULL,
	[Suffix] [nvarchar](10) NULL,
	[EmailAddress] [nvarchar](50) NULL,
	[EmailPromotion] [int] NOT NULL CONSTRAINT [DF_Contact_EmailPromotion]  DEFAULT ((0)),
	[Phone] [dbo].[Phone] NULL,
	[PasswordHash] [varchar](40) NOT NULL,
	[PasswordSalt] [varchar](10) NOT NULL,
	[AdditionalContactInfo] [xml](CONTENT [Person].[AdditionalContactInfoSchemaCollection]) NULL,
	[rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_Contact_rowguid]  DEFAULT (newid()),
	[ModifiedDate] [datetime] NOT NULL CONSTRAINT [DF_Contact_ModifiedDate]  DEFAULT (getdate()),

Open in new window

0
Comment
Question by:John500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 31

Expert Comment

by:MlandaT
ID: 33546847
0
 

Author Comment

by:John500
ID: 33546957
MlandaT,

Appreciate this article.  It starts off with a good subject which is the connection string.  Since I'm dealing with MSSQL and the AdventureWorks database, can you tell me what that connection string would look like to you ?

Thanks!
0
 
LVL 31

Expert Comment

by:MlandaT
ID: 33547017
Definitive guide for connection strings: http://www.connectionstrings.com/sql-server-2005

Samples:

Data Source=dataserver;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Data Source=dataserver\sqlexpress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Data Source=192.168.0.1;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

Data Source=192.168.0.1\sqlexpress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:John500
ID: 33547626
Here's what I have so far.  The picture below shows my Form application and the fact that I have a Data Source defined for the AdventureWorks database.

Upon launching the program, I'd like to fill it with every contact in the Person.Contact table.  

The only other thing I need is to be able to insert a person at the end of the list.  I'll take another look at the article you referenced.  Is there anything that comes to mind that I should be doing in order to populate the Window?

Thanks
0
 

Author Comment

by:John500
ID: 33547637
forgot the picture ...

Although the datasource says 'None', it is now set to AdventureWorks
AW-DataGridView.JPG
0
 
LVL 31

Accepted Solution

by:
MlandaT earned 2000 total points
ID: 33548405
Select the "Contact" table so that the DataSource is set to the Contacts table.
0
 

Author Comment

by:John500
ID: 33549476
Thanks for you help on this.  I opened another question that gets further into the details of inserting a record.  The first article you provided is good for binding but not for the particulars of adding a row.

If you know how to handle the particulars, here's a link to that question:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_26436224.html

Thanks
0
 

Author Comment

by:John500
ID: 33549490
I forgot to mention that for this question I was looking for an answer that resembles this:

DataRow row = dataTable.Rows [currRec];
 
row.BeginInsert ();
row ["Title"]        = txtTitle.Text;
row ["FirstName"]    = txtFirstName.Text;
row ["MiddleName"]   = txtMiddleName.Text;
row ["LastName"]     = txtLastName.Text;
row ["Suffix"]       = txtSuffix.Text;
row ["Phone"]        = txtPhone.Text;
row ["EmailAddress"] = txtEmailAddress.Text;
row.EndEdit ();

You were able to get the data to load when you directed me to set the data source to the actual table.  But as I say, that first article didn't come close to addressing inserts.  I'm just sayin ....

Thanks
0
 

Author Closing Comment

by:John500
ID: 33549498
I jumped the gun by asking about inserts when I wasn't even able to load the data from the table.

I should have started with loading and then moved to inserts.

My bad
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question