473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Datagrid Insert Statement

I AM GETTING A SYSTEM.DATA.SQLCLIENT.SQLEXCEPTION ERROR WHEN ATTEMPTING TO
INSERT DATA INTO A SINGLE TABLE THROUGH A GRID
//If this is due to a spelling error, i will
//inflict a ritual beating upon myself !!!!!!!!!!!!

TABLE SCRIPT

/* 04 September 2004 03:15:04 User: Server: (local) Database:
Payslip Application: MS SQLEM - Data Tools*/BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
CREATE TABLE dbo.Tmp_Allowances
(
AllowanceID int NOT NULL IDENTITY (1, 1),
AllowanceDesc nvarchar(50) NULL,
AllowanceAmt decimal(19, 4) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT dbo.Tmp_Allowances ON
GO
IF EXISTS(SELECT * FROM dbo.Allowances)
EXEC('INSERT INTO dbo.Tmp_Allowances (AllowanceID, AllowanceDesc,
AllowanceAmt)
SELECT AllowanceID, AllowanceDesc, AllowanceAmt FROM dbo.Allowances
TABLOCKX')
GO
SET IDENTITY_INSERT dbo.Tmp_Allowances OFF
GO
DROP TABLE dbo.Allowances
GO
EXECUTE sp_rename N'dbo.Tmp_Allowances', N'Allowances', 'OBJECT'
GO
ALTER TABLE dbo.Allowances ADD CONSTRAINT
PK_Allowances PRIMARY KEY CLUSTERED
(
AllowanceID
) ON [PRIMARY]

GO
GRANT SELECT ON dbo.Allowances TO public AS dbo
GRANT UPDATE ON dbo.Allowances TO public AS dbo
GRANT INSERT ON dbo.Allowances TO public AS dbo
GRANT DELETE ON dbo.Allowances TO public AS dbo
COMMIT


THIS IS THE CODE USED TO PERFORM THE INSERT, THE UPDATE,DELETE AND SELECT
WORKS FINE


//Populate allowances datagrid
// ****************************** ALLOWANCES
********************************

SqlConnection connGetAllowances = new SqlConnection(
"data source=chrisnote;initial catalog=Payslip;integrated
security=SSPI;persist security info=False;workstation id=CHRISNOTE;");
dsModify = new DataSet();
daModify = new SqlDataAdapter();
//Get the data
SqlCommand cmdSelect = connGetAllowances.CreateCommand();
cmdSelect.CommandType = CommandType.Text;
cmdSelect.CommandText = "SELECT AllowanceID," +
" AllowanceDesc,AllowanceAmt FROM Allowances";

// ******************************** Insert data
SqlCommand cmdInsert = connGetAllowances.CreateCommand();
cmdInsert.CommandType = CommandType.Text;
cmdInsert.CommandText = "INSERT INTO Allowances " +
"(AllowanceID, AllowanceAmt, AllowanceDesc)" +
"VALUES (@AllowanceID, @AllowanceAmt,@AllowanceDesc)";

//Create insert parameters
cmdInsert.Parameters.Add("@AllowanceID",SqlDbType. Int,4,"AllowanceID");
cmdInsert.Parameters.Add("@AllowanceAmt",SqlDbType .Decimal, 9,"AllowanceAmt");
cmdInsert.Parameters.Add("@AllowanceDesc",SqlDbTyp e.NVarChar,
50,"AllowanceDesc");
cmdInsert.Parameters["@AllowanceID"].SourceVersion = DataRowVersion.Original;

//Set the dataadapter and fill the dataset

daModify.SelectCommand = cmdSelect;
daModify.UpdateCommand = cmdUpdate;
daModify.InsertCommand = cmdInsert;
daModify.DeleteCommand = cmdDelete;

daModify.Fill(dsModify, "Allowances");
//Bind the datasources to the datagrid
dgAllowances.DataSource = dsModify;
dgAllowances.DataMember = "Allowances";

THE FOLLOWING CODE IS MEANT TO UPDATE THE TABLE
private void btnAcceptManageChanges_Click(object sender, System.EventArgs e)
{

daModify.Update(dsModify, "Allowances");
}


Is there anywhere that i can get sample code of how to
populate, edit, add new and delete data from a datagrid ?
Thanks
Christopher

Nov 16 '05 #1
0 2179

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: adove | last post by:
I am having problems with the following code. I am trying to update my db table Categories in Northwind. I have borrowed this code from www.dotnetjohn.com/articles/articleid27.aspx Although it...
10
by: GML | last post by:
I have a bound datagrid in C# based on an an SQL query. The results are displayed in a Sharepoint 2003 Webpart. I would like to add a new column based on the results of two columns in the...
7
by: Pierluigi Terzoli | last post by:
Hi everybody, I need help for this problem: I'm using a DataGrid to Insert/Modify/Cancel data from a single table. At the moment, every cell modification of a pre-existing row is correctly update...
8
by: Brent Burkart | last post by:
My datagrid OnCancelCommand event is not firing. HTML OnCancelCommand="AdminGrid_Cancel" 'Code Behind Sub AdminGrid_Cancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)...
9
by: tshad | last post by:
How do I find (and set) a couple of labels in the Footer after a DataGrid is filled? I have a bunch of DataGrids that get displayed nested inside a DataList. The datagrid looks like: ...
2
by: Channa | last post by:
I have a datagrid that is bound to an MSDataSetGenerator that is then bound to an access database though a OleDbDataAdapter and an OleDbConnection. I am able to open the form and its datagrid...
4
by: jaYPee | last post by:
I have 1 dataset called "dataset1" that contains 2 tables called "course" and "courseload". in my form i have a datagrid. the datasource of this datagrid is "dataset1" and the datamember is...
12
by: JMO | last post by:
I can import a csv file with no problem. I can also add columns to the datagrid upon import. I want to be able to start importing at the 3rd row. This will pick up the headers necessary for the...
1
by: sandeep.damodar | last post by:
Hello, I need help.. I am just trying to insert data from the datagrid to data source, using insert sql statement.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.