473,386 Members | 1,997 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,386 software developers and data experts.

Updating a SQL Server Table using a datatable and stored procedure

62
Hey guys need some info here.

I am adding, updating and deleting data on a datagrid. I pass the datagrid's datasource (datatable dt) to my data layer.

Assuming I have already created and opened a connection in a business layer and passed it to my datalayer I am now stuck here:

My business layer code is as follows:

//UPDATE MATRIX STOCK DATA TO SQL DATABASE
public void updateMatrixData(System.Data.DataTable dt)
{
DataAccessor.cmDABOMatrix insMatrix = new DataAccessor.cmDABOMatrix();
DataAccessor.DataClass dataC = new DataAccessor.DataClass();
this.sqlConnectionString = dataC.sqlConnectionString;
SqlConnection scnn = new SqlConnection(this.sqlConnectionString);
SqlCommand scmd = new SqlCommand();
try
{
scnn.Open();
scmd.Connection = scnn;
scmd.CommandType = CommandType.Text;
scmd.CommandText = "BEGIN TRAN";
scmd.ExecuteNonQuery();

insMatrix.sqlConn = scnn;
insMatrix.updateMatrixData(dt);

scmd.CommandType = CommandType.Text;
scmd.CommandText = "COMMIT TRAN";
scmd.ExecuteNonQuery();

}
catch (SqlException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
finally
{
scnn.Close();
scnn.Dispose();
insMatrix = null;
}
}


My data layer code is as follows:

public void updateMatrixData(System.Data.DataTable dt)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sDA = new SqlDataAdapter();
DataTable dti = dt.GetChanges(DataRowState.Added);
DataTable dtc = dt.GetChanges(DataRowState.Modified);
DataTable dtd = dt.GetChanges(DataRowState.Deleted);

string col1, col2, col3, col4, col5, col6, col7;
char mode;
int id;

try
{

// DOnt know how to do this
sDA.Update(dti);



cmd.Connection = this.sqlConn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(sDA);
cmd.CommandText = "spUpdateMatrixData";
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}


I know the data layer is incorrect. any idea how I can update, add and delete?

The stored procedure accepts a mode char I = Insert, D = Delete and U = update.

it also accepts a parameter for each field heading @col1 through to @ col7

Please help!!!1

Thanks,

Lóan
loan.burger@gmail.com
Apr 2 '08 #1
0 1199

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Vladimir Kanovnik | last post by:
I have table with columns id(number), photo(blob) and thumbnail(blob). I would like to insert image (using stored procedure) from file to column photo and in same time copy reduced image to column...
3
by: Ryan.Chowdhury | last post by:
This is a general question regarding the use of view and stored procedures. I'm fairly new to databases and SQL. I've created a SQL database using an Access Data Project ("ADP") and I'm...
4
by: Mullin Yu | last post by:
i have a stored procedure at sql server 2k. which will update records and select result from temp table. if i use SqlConnection class, and i do both. but, if i use OleDbConnection class, i can...
4
by: Darrel | last post by:
I'm creating a table that contains multiple records pulled out of the database. I'm building the table myself and passing it to the page since the table needs to be fairly customized (ie, a...
10
by: Rich | last post by:
I have a stored procedure on Sql Server2k. I can fill a data table which I can append to a dataset using an ADODB recordset object which gets populated from a command object that runs the sp. I...
3
by: Roger Withnell | last post by:
I have a framed website. I plan to include in default.asp a routine to check if the annual subscription is due from the records in the People table. If so, then the routine would send emails...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
3
by: ianforgroupuse | last post by:
I'm running Vista Business edition on 2005 Virtual PC. Installed SQL Server 2005 Express latest download, with instance of MSSQLSERVER and service accounts running under "NT AUTHORITY\SYSTEM"....
1
by: jshunter | last post by:
I've got a weird one here. I'm running a DTS package on SQL Server 2005. It copies a bunch of stored procedures. I renamed them on the originating server and ran the DTS again. The came over...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.