473,804 Members | 3,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clr trigger for insert

Hi
I need to use Clr trigger for insert command
My code is as below
I am using SQL server 2005 and VS 2008.... but after running this code
i didnt get the result as i expexted it shows the result as no row is
effected ...Please help me guys
using System;
using System.Data;
using System.Data.Sql ;
using System.Data.Sql Types;
using System.Data.Sql Client;
using Microsoft.SqlSe rver.Server;
using System.Xml;
using System.IO;
//using System.Transact ions;

public partial class Triggers
{
// Enter existing table or view for the target and uncomment the
attribute line
[Microsoft.SqlSe rver.Server.Sql Trigger(Name = @"firstTrigger" ,
Target = "dbo.CrossSell" , Event = "FOR Insert")]
public static void firstTrigger()
{
Guid CrossSellId;
int int_id;
Guid ProductId;
Guid CrossSellingId;
SqlCommand command;
SqlTriggerConte xt triggContext = SqlContext.Trig gerContext;
//string st = triggContext.Ev entData.Value;
// XmlDocument xmlDoc = new XmlDocument();
//xmlDoc.LoadXml( st);

SqlPipe pipe = SqlContext.Pipe ;
SqlDataReader reader;
// DataTable dt = new DataTable();
//SqlDataAdapter da = new SqlDataAdapter( );
switch (triggContext.T riggerAction)
{

case TriggerAction.I nsert:
// Retrieve the connection that the trigger is using
using (SqlConnection connection
= new SqlConnection(@ "context connection=true "))
{
connection.Open ();
command = new SqlCommand(@"SE LECT * FROM
INSERTED;",
connection);
SqlDataAdapter da = new
SqlDataAdapter( "select*fro m inserted ",connectio n);
DataTable dt = new DataTable();
da.Fill(dt);
StringWriter writer = new StringWriter();

dt.WriteXml(wri ter,XmlWriteMod e.WriteSchema,f alse);
string xmlFromDataTabl e = writer.ToString ();

reader = command.Execute Reader();
reader.Read();
CrossSellId = (Guid)reader[0];

int_id = (int)reader[1];
ProductId = (Guid)reader[2];
CrossSellingId = (Guid)reader[3];

reader.Close();
////// command = new SqlCommand(
//////"INSERT into CrossSell (CrossSellId,
int_id,ProductI d,CrossSellingI d) " +
//////"VALUES (@CrossSellId,
@int_id,@Produc tId,@CrossSelli ngId)", connection);
command = new SqlCommand(
@"INSERT [dbo].[CrossSell] VALUES ("
+ CrossSellId + @", " + int_id + @"," + ProductId +
@"," + CrossSellingId + @");",
connection);

pipe.Send(comma nd.CommandText) ;
command.Execute NonQuery();
pipe.Send(xmlFr omDataTable);

//pipe.Send("Cros sSell inserted!");
//connection.Open ();
//da.Fill(dt);
//
connection.Clos e();

}
break;
}


After this i need to update my ProductBase table

DECLARE @ProductBase TABLE (ID int IDENTITY(1,1), CrossSell xml)

INSERT INTO @ProductBase
DEFAULT VALUES

SELECT * FROM @ProductBase

Update @ProductBase
set CrossSell = ' '
where CrossSell IS NULL

SELECT * FROM @ProductBase
then it shows the updated result...
This what i did ....
but after debugging the clr trigger it shows no rows are effected i
dont kknw what is the problem with it..i am new to this...thanks in
advance for your help

Nov 10 '08 #1
1 2660
On Nov 10, 12:22*pm, anu b <anupam...@gmai l.comwrote:
Hi
I need to use Clr trigger for insert command
My code is as below
I am using SQL server 2005 and VS 2008.... but after running this code
i *didnt get the result as i expexted it shows the result as no row is
effected ...Please help me guys

using System;
using System.Data;
using System.Data.Sql ;
using System.Data.Sql Types;
using System.Data.Sql Client;
using Microsoft.SqlSe rver.Server;
using System.Xml;
using System.IO;
//using System.Transact ions;

public partial class Triggers
{
* * // Enter existing table or view for the target and uncomment the
attribute line
* * [Microsoft.SqlSe rver.Server.Sql Trigger(Name = @"firstTrigger" ,
Target = "dbo.CrossSell" , Event = "FOR Insert")]
* * public static void firstTrigger()
* * {
* * * * Guid CrossSellId;
* * * * int int_id;
* * * * Guid ProductId;
* * * * Guid CrossSellingId;

* * * * *SqlCommand command;
* * * * SqlTriggerConte xt triggContext = SqlContext.Trig gerContext;
* * * * //string st = triggContext.Ev entData.Value;
* * * *// XmlDocument xmlDoc = new XmlDocument();
* * * * *//xmlDoc.LoadXml( st);

* * * * SqlPipe pipe = SqlContext.Pipe ;
* * * * SqlDataReader reader;
* * * *// DataTable dt = new DataTable();
* * * * //SqlDataAdapter da = new SqlDataAdapter( );
* * * * switch (triggContext.T riggerAction)
* * * * {

* * * * * * case TriggerAction.I nsert:
* * * * * * * * // Retrieve the connection that the trigger is using
* * * * * * * * using (SqlConnection connection
* * * * * * * * * *= new SqlConnection(@ "context connection=true "))
* * * * * * * * {
* * * * * * * * * * connection.Open ();
* * * * * * * * * * command = new SqlCommand(@"SE LECT * FROM
INSERTED;",
* * * * * * * * * * * *connection);
* * * * * * * * * * SqlDataAdapter da = new
SqlDataAdapter( "select*fro m inserted ",connectio n);
* * * * * * * * * * DataTable dt = new DataTable();
* * * * * * * * * * da.Fill(dt);
* * * * * * * * * * StringWriter writer = new StringWriter();

dt.WriteXml(wri ter,XmlWriteMod e.WriteSchema,f alse);
* * * * * * * * * * string xmlFromDataTabl e = writer.ToString ();

* * * * * * * * * * reader = command.Execute Reader();
* * * * * * * * * * reader.Read();
* * * * * * * * * * CrossSellId = (Guid)reader[0];

* * * * * * * * * * int_id = (int)reader[1];
* * * * * * * * * * ProductId = (Guid)reader[2];
* * * * * * * * * * CrossSellingId = (Guid)reader[3];

* * * * * * * * * * * reader.Close();
* * * * ////// * * * * * *command = new SqlCommand(
* * * * //////"INSERT into CrossSell (CrossSellId,
int_id,ProductI d,CrossSellingI d) " +
* * * * //////"VALUES (@CrossSellId,
@int_id,@Produc tId,@CrossSelli ngId)", connection);

* * * * * * * * * * command = new SqlCommand(
* * * * * * * * * @"INSERT [dbo].[CrossSell] VALUES ("
* * * * * * * * * + CrossSellId + @", " + int_id + @","+ ProductId +
@"," + CrossSellingId + @");",
* * * * * * * * * connection);

* * * * * * * * * * pipe.Send(comma nd.CommandText) ;
* * * * * * * * * * command.Execute NonQuery();
* * * * * * * * * * pipe.Send(xmlFr omDataTable);

* * * * * * * * * * //pipe.Send("Cros sSell inserted!");
* * * * * * * * * * //connection.Open ();
* * * * * * * * * * //da.Fill(dt);
* * * * * * * * * *//
connection.Clos e();

* * * * * * *}
* * * * * * * * break;

* * * * }

After this i need to update my ProductBase table

DECLARE @ProductBase TABLE (ID int IDENTITY(1,1), CrossSell xml)

INSERT INTO @ProductBase
DEFAULT VALUES

SELECT * FROM @ProductBase

Update @ProductBase
set CrossSell = ' *'
where CrossSell IS NULL

SELECT * FROM @ProductBase
then it shows the updated result...
This what i did ....
but after debugging the clr trigger it shows no rows are effected i
dont kknw what is the problem with it..i am new to this...thanks in
advance for your help
As you're asking about SQL and C#, I'd suggest you go to one of those
newsgroups, and not the VB.NET group.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Nov 11 '08 #2

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

Similar topics

10
2742
by: Anton.Nikiforov | last post by:
Dear all, i have a problem with insertion data and running post insert trigger on it. Preambula: there is a table named raw: ipsrc | cidr ipdst | cidr bytes | bigint time | timestamp Triggers:
2
6436
by: 73blazer | last post by:
Perhaps my thinking is wrong but this is what I have: 1 table (Tab1) with 1 attribute (Attr1) Attr1 char(16) for bit data ----------------------------------------------- create trigger check no cascade before insert on Tab1 referencing new as N
0
2481
by: JohnO | last post by:
Thanks to Serge and MarkB for recent tips and suggestions. Ive rolled together a few stored procedures to assist with creating audit triggers automagically. Hope someone finds this as useful as I've found it educational. Note: - I build this for use in a JDEdwards OneWorld environment. I'm not sure how generic others find it but it should be fairly generic. - I use a C stored procedure GETJOBNAME to get some extra audit data,
8
2640
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or something entirely different? The docs only mention regular triggers being executed alfabetically.
2
3343
by: Net Virtual Mailing Lists | last post by:
Hello, If I have a rule like this: CREATE OR REPLACE RULE sometable_update AS ON UPDATE TO table2 DO UPDATE cache SET updated_dt=NULL WHERE tablename='sometable'; CREATE OR REPLACE RULE sometable_insert AS ON INSERT TO table2 DO UPDATE cache SET updated_dt=NULL WHERE tablename='sometable'; CREATE OR REPLACE RULE sometable_delete AS ON DELETE TO table2 DO UPDATE
2
3791
by: mghale | last post by:
Hello, I have to create a trigger to accomplish the following: Before the insert into table A occurs, the trigger must check to see if the combination of two columns (from the insert statement) exist in another domain table. If the combination does not exist the trigger must insert a record into the domain table so that the original insert statement will succeed. I know what you're thinking, why not use RI. Well not my choice so I'm...
1
6212
by: abhi81 | last post by:
Hello All, I have a table on which I have created a insert,Update and a Delete trigger. All these triggers write a entry to another audit table with the unique key for each table and the timestamp. Insert and Update trigger work fine when i have only one of them defined. However when I have all the 3 triggers in place and when i try to fire a insert query on the statement. It triggers both insert and update trigger at the same time and...
2
3371
by: wugon.net | last post by:
Problem: after inser trigger encounter error sql0348 Env:db2 v8 + fp 13 + win xp Description: we build two after insert triggers DB2.TRG1, DB2.TRG2 on base table DB2.TEST1, insert data into DB2.TEST1 encounter error sql0348 but we re-create trigger DB2.TRG2 before DB2.TRG1 and re-insert data
1
2282
by: veasnamuch | last post by:
I have a problem while I create a trigger to my table. My objective is getting any change made to my table and record it in to another table . My have thousands records before I add new trigger to this table, everything work fine means, when I manual insert data into table the last row of inserting is what I am doing but after I add trigger on inserting to this table and I do next manual insert data into the last row in Enterprise Manager, I...
2
3755
by: lenygold via DBMonster.com | last post by:
Hi Everebody: I have a table: CREATE TABLE CROSS_REFERENCE (ROW# INTEGER NOT NULL ,KEY_WORD CHAR(16) NOT NULL ,QUERY_DESCR VARCHAR(330) NOT NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a cross reference table to my CATALOG Table based on key words.
0
9705
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
9576
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
10311
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
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
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
6847
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();...
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
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.