Stored Procedure with fatal error
tempPK has one column with datatype = int
==============================================
CREATE PROCEDURE [dbo].[spError]
AS
BEGIN
Begin Tran
--Select 1/0
--execute('insert into tempPK select ''s''')
execute('insert into tempPK select ''fg''')
execute('insert into tempPK select 4')
Commit Tran
print 'Success'
END
======================================================
Dot Net Calling Method
try
{
SqlConnection sCon = new SqlConnection();
sCon.ConnectionString = "Database=xyz;Server=10.62;User Id=sa;Password=#12#";
sCon.Open();
SqlCommand sCom = new SqlCommand();
sCom.Connection = sCon;
sCom.CommandText = "Exec spError";
sCom.ExecuteNonQuery();
}
catch (Exception ex)
{
if(ex.Message.Substring(ex.Message.Length-7) == "Success")
MessageBox.Show("No Fatal Error");
else
MessageBox.Show("Fatal Error");
}
======================================================
SQL Trick And Tips, Interview questions,Queries, examples to make developer's life easier than ever before.. My aim to create this blog is to write all such tricks and tips in SQL queries which will be helpful to my friends all over the world. I want to share my experience about the SQL and hence i am too excited to start with.... Hope you all take me further over the web with my genuine intention to help you out with enjoyment of the technology......
Friday, 13 August 2010
Get Non Fatal Errors from SQL in DOT NET CATCH block
Stored Procedure having errors within
=======================
CREATE PROCEDURE spError
AS
BEGIN
Begin Tran
Select 1/0
execute('insert into te mpPK select 1')
execute('insert into tempPK select 3')
Commit Tran
END
GO
=======================
Application Code
try
{
SqlConnection sCon = new SqlConnection();
sCon.ConnectionString = "Database=xyz;Server=11.62;User Id=sa;Password=klk";
sCon.Open();
SqlCommand sCom = new SqlCommand();
sCom.Connection = sCon;
sCom.CommandText = "Exec spError";
sCom.ExecuteNonQuery();
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
=======================
Errors captured
Divide by zero error encountered.
Incorrect syntax near 'mpPK'.
=======================
CREATE PROCEDURE spError
AS
BEGIN
Begin Tran
Select 1/0
execute('insert into te mpPK select 1')
execute('insert into tempPK select 3')
Commit Tran
END
GO
=======================
Application Code
try
{
SqlConnection sCon = new SqlConnection();
sCon.ConnectionString = "Database=xyz;Server=11.62;User Id=sa;Password=klk";
sCon.Open();
SqlCommand sCom = new SqlCommand();
sCom.Connection = sCon;
sCom.CommandText = "Exec spError";
sCom.ExecuteNonQuery();
}
catch (Exception ex)
{
textBox1.Text = ex.Message;
}
=======================
Errors captured
Divide by zero error encountered.
Incorrect syntax near 'mpPK'.
Subscribe to:
Posts (Atom)