Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Inserting data into a SQL table from a C# program

Posted on 2010-08-27
9
Medium Priority
?
213 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

610 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