Solved

Inserting data into a SQL table from a C# program

Posted on 2010-08-27
9
208 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
  • 6
  • 3
9 Comments
 
LVL 30

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 30

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 30

Accepted Solution

by:
MlandaT earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
defining NULL or 0 10 44
parsing JSON help 1 20
Convert Ctime to date time in textfile? 7 34
VB/ASP.Net "Value of type 'Date' cannot be converted to 'Integer'" 12 26
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this article I will describe the Copy Database Wizard 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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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