John500
asked on
How to insert one record from a DataGridView to a MSSQL database table
Greetings,
I am trying to understand how to perform an insert to a table and I'm using the AdventurWorks database with a C# DataGridView. The picture below shows what I have so far. That is, I am able to load the table into my DataGridView.
I am new to this stuff so I don't know what the particulars are from here. I could read umptine articles on this subject from the Internet but I'm hoping somebody could provide the code that will work with what I have.
The code I have so far is seen in the snippet below. What code (a method to call) is necessary to add one person with these fields:
[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
For right now, I'm not worried about cell validation. I can open another question for that later.
Thanks in advance
I am trying to understand how to perform an insert to a table and I'm using the AdventurWorks database with a C# DataGridView. The picture below shows what I have so far. That is, I am able to load the table into my DataGridView.
I am new to this stuff so I don't know what the particulars are from here. I could read umptine articles on this subject from the Internet but I'm hoping somebody could provide the code that will work with what I have.
The code I have so far is seen in the snippet below. What code (a method to call) is necessary to add one person with these fields:
[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
For right now, I'm not worried about cell validation. I can open another question for that later.
Thanks in advance
using System;
using System.Configuration.Assemblies;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace AdvWorksFormApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'adventureWorksDataSet.Contact' table. You can move, or remove it, as needed.
this.contactTableAdapter.Fill(this.adventureWorksDataSet.Contact);
}
}
}
table-loaded.JPG
so you want the insert update delete page for your table in adventure database ?
ASKER
gmailjini - I just want to be able to insert a new contact at the last row of the Form application above. That picture shows I am able to load the table.... I should have taken a shot of the last row where it provides a blank row for a new entry. I'm not worried about updates or deletes, only inserts.
puru1981 - I looked at that article but it says:
"The GridView was not designed to insert new rows, but there is a way to accomplish this with very little code."
That article doesn't apply because I'm using a DataGridView and I know this is possible. I'm guessing the code would look something like this:
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 ();
puru1981 - I looked at that article but it says:
"The GridView was not designed to insert new rows, but there is a way to accomplish this with very little code."
That article doesn't apply because I'm using a DataGridView and I know this is possible. I'm guessing the code would look something like this:
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 ();
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
vs00saini,
Thanks but sorry, that code don't work...
Thanks but sorry, that code don't work...