473,652 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Updating a SQL Server Table using a datatable and stored procedure

62 New Member
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 updateMatrixDat a(System.Data.D ataTable dt)
{
DataAccessor.cm DABOMatrix insMatrix = new DataAccessor.cm DABOMatrix();
DataAccessor.Da taClass dataC = new DataAccessor.Da taClass();
this.sqlConnect ionString = dataC.sqlConnec tionString;
SqlConnection scnn = new SqlConnection(t his.sqlConnecti onString);
SqlCommand scmd = new SqlCommand();
try
{
scnn.Open();
scmd.Connection = scnn;
scmd.CommandTyp e = CommandType.Tex t;
scmd.CommandTex t = "BEGIN TRAN";
scmd.ExecuteNon Query();

insMatrix.sqlCo nn = scnn;
insMatrix.updat eMatrixData(dt) ;

scmd.CommandTyp e = CommandType.Tex t;
scmd.CommandTex t = "COMMIT TRAN";
scmd.ExecuteNon Query();

}
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 updateMatrixDat a(System.Data.D ataTable dt)
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sDA = new SqlDataAdapter( );
DataTable dti = dt.GetChanges(D ataRowState.Add ed);
DataTable dtc = dt.GetChanges(D ataRowState.Mod ified);
DataTable dtd = dt.GetChanges(D ataRowState.Del eted);

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.Sto redProcedure;
cmd.Parameters. Add(sDA);
cmd.CommandText = "spUpdateMatrix Data";
cmd.ExecuteNonQ uery();
}
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@gma il.com
Apr 2 '08 #1
0 1223

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

Similar topics

0
2614
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 thumbnail. My code is: CREATE OR REPLACE PROCEDURE "MDEMO"."PUT_PHOTO_THUMB" ( image_file_directory in varchar2, image_file_name in varchar2, image_file_mime_type in varchar2, image_http_path in varchar2,
3
2470
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 satified with the table structure. I've moved on to building some front ends for our users. I'm running into situations where I want subreports to be built from queries that are dependent on the values in other controls. I've played with stored...
4
1873
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 have only records updated, but no result come out. how can i debug, and what's the error? thanks!
4
2012
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 datagrid isn't going to work). On this page, people can update a variet of records. On submit, I want to then go in and update all of the records. Normally, I'd make each form element include a runat: server and then declare it in my codebhind so I...
10
13300
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 was hoping to use a DataAdapter. But I think the data adapter only uses select statements. I could write the sp in my vb.net app, but the sp references UDF's I wrote in the Sql Sever. I will guess that I will need to stick with the ADODB recordset...
3
1715
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 to those who are due and update the table that this had been done, so that it is only done once. If session 1 is doing just this, session 2 (and 3 and n) could be doing the same thing.
6
13991
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 = conn da.UpdateCommand.CommandText = "Update tbl1 set fld1 = 'test' where ID = 1" da.Update(tblx) '--tblx/tbl1 not getting updated here.
3
7417
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". Mixed mode authentication specified and sa password specified. Used server manager to create user login with sysadmin role. Login using windows authentication. Run my application and database is created and populated with default records.
1
2395
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 with the old name and code! I deleted the DTS and built it from scratch, and the same thing happened. I ran SELECT * FROM sys.objects where type = 'P' on the source server and the names were correct
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5619
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.