get empty dataset result in vb.net - plsql

writing procedure in pl/sql . calling this procedure in vb.net. I want to fill the dataset with the result of pl/sql procedure. but am getting empty dataset. In pl/sql am able to see the output.in my application am using oracle.dataaccess.client dll.Help me to get the result in dataset.
using this dll
Imports Oracle.DataAccess.Client
my code
Dim orc1 As Oracle.DataAccess.Client.OracleConnection
orc1 = New OracleConnection("Data Source=xxx;User Id=qqq;Password=123;")
orc1.open()
Dim objcommand3 As New Oracle.DataAccess.Client.OracleCommand
objcommand3.Connection = orc1
objcommand3.CommandText = "SMSDND.pro_mod934"
objcommand3.CommandType = CommandType.StoredProcedure
Dim oraada As New OracleDataAdapter(objcommand3)
'oraada .SelectCommand =objcommand3
Dim ods As New DataSet
oraada.Fill(ods)
procedure in pl/sql(using oracle toad)
CREATE procedure SMSDND.pro_mod934
as
cursor c78 is
SELECT smsdnd.phonenumbers_tbl.FLD_PHONENUMBER
FROM smsdnd.phonenumbers_tbl
WHERE not exists (select smsdnd.smsdnd_tbl.FLD_PHONENUMBERS from smsdnd.smsdnd_tbl
where smsdnd.smsdnd_tbl.FLD_PHONENUMBERS = smsdnd.phonenumbers_tbl.FLD_PHONENUMBER and smsdnd.smsdnd_tbl.FLD_OPSTYPE='A');
m smsdnd.phonenumbers_tbl.FLD_PHONENUMBER %type;
BEGIN
OPEN c78;
LOOP
FETCH c78 INTO m;
EXIT when c78%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(''|| m);
END LOOP;
CLOSE c78;
EXCEPTION WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
end;
/

Related

Execute PL/SQL procedure from vb6 on Oracle 11g

I am trying to call a PL/SQL procedure which has an output value from a Visual Basic 6 function, but it does not work.
Here my code.
PL/SQL code (so far, it is just a mock):
create or replace
PROCEDURE IS_SINISTRO_ABS_MOCK
( numPol in VARCHAR2,
codGaranzia in VARCHAR2,
res out BOOLEAN
) AS
BEGIN
res := TRUE;
END IS_SINISTRO_ABS_MOCK;
VB6 code:
Private Function IsSinistroInABS(NumPol As String, CodGaranzia As String) As Boolean
Dim dbConn As New ADODB.Connection
With dbConn
.Provider = "OraOLEDB.Oracle"
.Properties("Data Source") = "*********"
.Properties("User Id") = "ROUTING"
.Properties("Password") = "***********"
.Open
End With
Dim dbCmd As ADODB.Command
Dim result As Boolean
Set dbCmd = New ADODB.Command
dbCmd.ActiveConnection = dbConn
dbCmd.CommandTimeout = 300
dbCmd.CommandType = adCmdStoredProc
dbCmd.CommandText = "{CALL ROUTING.IS_SINISTRO_ABS_MOCK(?,?,?)}"
dbCmd.Parameters.Append dbCmd.CreateParameter(, adLongVarChar, adParamInput, _
Len(NumPol), NumPol)
dbCmd.Parameters.Append dbCmd.CreateParameter(, adLongVarChar, adParamInput, _
Len(CodGaranzia), CodGaranzia)
dbCmd.Parameters.Append dbCmd.CreateParameter(, adBoolean, adParamOutput, , _
result)
dbCmd.Prepared = True
dbCmd.Execute
IsSinistroInABS = dbCmd.Parameters("res").value
dbConn.Close
End Function
The DB connection works properly, indeed I succeeded in executing a standard SQL query, but I get an unspecified error when I try to run the procedure. I succeeded also in launching a procedure without any parameters. As a result, the problem is supposed to be in the use of them.
Note that the procedure is a standalone one. In other words, it is not included in any package.
Maybe I'm a bit late (you posted your question 2.5 years ago), but I got the same problem.
After a lot of digging and frustration, I found out the error occurs when the stored procedure has numeric output parameters (VARCHAR is OK as well as any input parameter).
I finally found out that everything works correctly when you use the ancient DB-provider MSDAORA.1.

Stored Procedure Returns zero results

I developed a search page containing a textbox control for entering a number and button to display the respective results in a Gridview. The page functions off a stored procedure. The sql query returns the expected results when ran via SQL Server Manager when I manually type in the number, but when used in my stored procedure, I get zero results.
This is the code behind the button-event handler:
Dim ds As New DataSet()
Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("ShipperNotificationConnectionString1").ToString())
Using command As New SqlCommand()
command.CommandType = CommandType.StoredProcedure
command.CommandText = "getPON"
command.Connection = connection
command.Parameters.AddWithValue("#PON", txtPON.Text)
connection.Open()
Dim a As New SqlDataAdapter(command)
a.Fill(ds)
End Using
End Using
gvPON.DataSource = ds
gvPON.DataBind()
...The following is the stored procedure:
ALTER PROCEDURE [dbo].[getPON]
(
#PON varchar
)
AS
BEGIN
SELECT SupplierCompany.company_name, SupplierCompany.Address1, SupplierCompany.Address2, SupplierCompany.City, SupplierCompany.State,
SupplierCompany.Zip, Shipment_Po.PONumber, Shipment.TotalWeight, Shipment.NoOfPallets, Shipment.PalletIdentical
FROM SupplierCompany INNER JOIN
Shipment ON SupplierCompany.Company_guid = Shipment.Company_Guid INNER JOIN
Shipment_Po ON Shipment.Shipment_Guid = Shipment_Po.Shipment_guid
WHERE Shipment_Po.PONumber = '''+ #PON +'''
END
...Could someone please provide some direction?
The problem is the stored procedure. The expression:
WHERE Shipment_Po.PONumber = '''+ #PON +'''
Is not doing what you think. It is doing the following comparison:
WHERE Shipment_Po.PONumber = '+#PON+'
Or something like that. In other words, you are mixing dynamic SQL expressions with regular SQL. Try doing:
WHERE Shipment_Po.PONumber = #PON
If you are concerned about the cast to the right type:
WHERE Shipment_Po.PONumber = (case when isnumeric(#PON) = 1 then cast(#PON as int) end)

SQL Stored Procedures failing to return values

I am working on a Tag system for a news page designed in ASP.NET. For the system I require a TagExists method to check for tags within the database. The stored procedure I have written is below.
ALTER PROCEDURE [dbo].[Tags_TagExists](
#Tag varchar(50))
AS
BEGIN
If (EXISTS(SELECT * FROM dbo.Tags WHERE LOWER(#Tag) = LOWER(Tag)))
RETURN 1
ELSE
RETURN 0
END
When I call this method however 0 is always returned. I am using the following code to call the method
Public Shared Function TagExists(ByVal name As String) As Boolean
Dim result As Boolean
Using conn As SqlConnection = New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand("Tags_TagExists", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#Tag", name)
conn.Open()
result = Convert.ToBoolean(cmd.ExecuteScalar())
conn.Close()
End Using
Return result
End Function
I have tried switching the procedure to return 0 if the tag exists and 1 if it does not and it still returns 0 despite the exact same testing conditions. I have also returned the actual select query and it has complained of the Tag "news" (my test item) not being an int on execution showing the select itself is definitely properly formed.
If anyone can shed some light on this, Thanks
Michael
It should probably be a function, but here is the stored proc code:
ALTER PROCEDURE [dbo].[Tags_TagExists](
#Tag varchar(50))
AS
BEGIN
If EXISTS(SELECT 1 FROM dbo.Tags WHERE LOWER(#Tag) = LOWER(Tag))
BEGIN
SELECT 1
END
ELSE
BEGIN
SELECT 0
END
END
You're returning from a Stored Procedure, not getting a single scalar value from a SQL statement.
I'm assuming this is a simple example and you have other processing you want to handle inside the Stored Procedure. In that case, using the Stored Procedure and return value is the right way to go. You need to handle the return value from the Stored Procedure in your C# code (Please excuse any syntax errors, my VB.NET is a bit rusty):
Public Shared Function TagExists(ByVal name As String) As Boolean
Dim result As Boolean
Using conn As SqlConnection = New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand("Tags_TagExists", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#Tag", name).
Dim retVal As SqlParameter = _
cmd.Parameters.Add("return_value", SqlDbType.Int)
retval.Direction = ParameterDirection.ReturnValue
conn.Open()
cmd.ExecuteNonQuery()
result = System.Convert.ToBoolean(retval.Value)
conn.Close()
End Using
Return result
End Function
If you're strictly interested in the return value and your Stored Procedure isn't performing any other use, then convert it to a simple select statement (or function). Your use of ExecuteScalar would work in that case.
Please try using SELECT 1 and SELECT 0 instead of RETURN statement
Hope that helps,

IF EXISTS IN STORED PROCEDURE

I am using following storedprocedure
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
Alter PROCEDURE [dbo].[GetUserDetailsByUserID]
(
#UserId varchar(100)
)
AS
BEGIN
IF EXISTS(Select * from Registration where UserId=#UserID)
BEGIN
Select
[Name],
[UserId],
[PermanentAddress],
[TemporaryAddress],
[OfficePhoneNo],
[ResidentialPhoneNo],
[MobileNo],
[Email],
[ContactPerson],
[C_OfficePhoneNo],
[C_ResidentialPhoneNo],
[C_MobileNo],
[C_Email],
[Project],
[TotalAmount]
From
Registration
Where
UserId=#UserId
END
END
I am using following code in vb
Public Function GetUserDetailsByUserID(ByVal strUserId As String) As DataTable
Try
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim DbCommand As DbCommand = _
db.GetStoredProcCommand("GetUserDetailsByUserID")
db.AddInParameter(DbCommand, "#UserId", DbType.String, strUserId)
Return db.ExecuteDataSet(DbCommand).Tables(0)
Catch ex As Exception
Return New DataTable()
End Try
End Function
If details corresponding to userid does not exist in registration table, db.ExecuteDataSet(DbCommand).Tables(0) shows one error as cannot find Table(0). What modification in stoted procedure i hve to make to get rid of this error?
You can simply get rid of the IF EXISTS. When the record doesn't exist, you will get an empty table (which I think is what you want, looking at your sample code)
The procedure will not always return a record set. If there is no record set then Tables will be empty and Tables(0) will fail and return an error. You should just return the selection rather than only selecting if the record exists. Your code can then check for an empty returned record set.
In the VB code, change
Return db.ExecuteDataSet(DbCommand).Tables(0)
to
Dim Ds as DataSet = db.ExecuteDataSet(DbCommand)
If Ds.Tables.Count = 1 Then
Return Ds.Tables(0)
Else
Return New DataTable()
End If
Also, remove the If Exists from the Stored Procedure, since if the row exists, you will force the database to search twice in the table for the record where UserId=#UserID.

How to get stored procedure's returning value?

I have made stored procedures in oracle.
I'm calling it through my asp.net code.
The procedure is :
PROCEDURE prc_GetNewQuestionNo(iNextQuestionNo IN OUT NUMBER)
IS
iQuestionNo NUMBER ;
BEGIN
Select MAX(QUESTIONNO)+ 1 INTO iQuestionNo
from tblIFFCOQUESTIONMASTER;
iNextQuestionNo:=iQuestionNo;
END prc_GetNewQuestionNo;
and I'm calling it in asp.net:
<Connection>
com.CommandType = CommandType.StoredProcedure;
com.CommandText = StoredProcedures.GET_NEW_QUESTION_NO;
com.Parameters.Add(new OracleParameter("iNextQuestionNo", OracleType.Number)).Direction = ParameterDirection.InputOutput;
adp = new OracleDataAdapter(com);
ds = new DataSet();
adp.Fill(ds);
How to get its return value?
Isn't it better to use function? Just like:
create function prc_GetNewQuestionNo(iNextQuestionNo IN NUMBER)
return number AS
iQuestionNo NUMBER ;
BEGIN
Select MAX(QUESTIONNO)+ 1 INTO iQuestionNo from tblIFFCOQUESTIONMASTER;
return iQuestionNo;
END prc_GetNewQuestionNo;
I wanted to add a comment/question to your reply there Paul, but I couldnt. Apologize for my ignorance, but if you are using a SQL Server stored procedured with isolation level serializable, arent supposed all the sql tables to be locked for the time the transaction/stored procedure last, giving no problems of concurrency? is this a bad practice?
I guess the problem is here
adp = new OracleDataAdapter(com);
ds = new DataSet();
adp.Fill(ds);
You want a scalar value and not an entire record set.. right? So instead try like this
//some code snippet
db.ExecuteNonQuery(cmd);
iNextQuestionNo= (decimal?)cmd.Parameters[0].Value;
Hope this helps

Resources