Microsoft
Software
Hardware
Network
Question : SQL in C# code .. Reading Output Parameters.
I got it so close I can almost taste victory. But alas, foiled again.
I put in code to read a SQL Stored procedure with and output parameter.
The output parameter is set ok in the Stored Procedure.
I know I am getting the info to return because I get the first character of the entry in the field. If the message is empty it returns the field empty if it is set to something like "Can't Add..." I get back "C"... That's it.. One lousy character! What the ????????
I added an image of the results of running the Stored Procedure in SQL..
here is my C# code
cmd = new SqlCommand("ULInsideRepMai
nt", SQLServer_Conn.Connection)
;
//if want to read output parm when return, make sure you intialize it in SP!
//Then set the direction to output.
cmd.Parameters.AddWithValu
e("@SPMess
age", "");
cmd.Parameters["@SPMessage
"].Directi
on = ParameterDirection.Output;
cmd.Parameters.AddWithValu
e("@Proces
s", pbl.pblStringVar1);
cmd.Parameters.AddWithValu
e("@RepNo"
, pbl.pblstrRepNo);
cmd.CommandType = CommandType.StoredProcedur
e;
try
{
SQLServer_Conn.Open();
cmd.ExecuteNonQuery();
SQLServer_Conn.Close();
//See if we have an error message or if it is empty..
lblMessage.Text = (string)cmd.Parameters["@S
pMessage"]
.Value;
}
catch (SqlException exc)
{
lblMessage.Text = exc.Message;
}
finally
{
pbl.pblStringVar1 = string.Empty;
}
}
here is my SqL Code..
ALTER Procedure [dbo].[ULInsideRepMaint]
@SPMessage CHAR (100) OUTPUT,
@Process Char (1),
@RepNo Char(5)
AS
declare @reccount2 as int;
--since reading this in C# code, need to set it.
set @SpMessage = '';
--- Check if valid process (can not add if already added or if not in VD SalesRep to add, can't delete if not there)
set @reccount2 = (select count(Salesperson) from SSIS_VD.SSIS.ULarSalespers
on where @repno = Salesperson)
-- Cannot add Rep if not in VD SalesRep Table to begin with!
If @Process = 'A' and @reccount2 = 0
Begin
set @SpMessage = 'Cannot Add Rep, Rep # is not in VD SalesRep Table';
Return
end;
Attachments:
SQLInCSharp.jpg
(30 KB)
(File Type Details)
Results Running in SQL
Answer : SQL in C# code .. Reading Output Parameters.
hi
when u write
cmd.Parameters.AddWithValu
e("@SPMess
age", "");
then length of @SPMessage set to char(1) but then sp parameter is char(100) in casting (in C# code in return) the the @SPMessage is truncated
Random Solutions
How do you stop a Network printer from changing it's IP address
FastCGI Error Displaying PHP Page via IIS6 on Windows Server2003
Adding Javascript to a button
Best way to duplicate Windows environment in ESX lab?
Allow user to navigate to a folder in windows directory and select the PATH to the file in a .aspx page. Is this possible and how?
How Can I Disable Calendar Event Notifications in Entourage 2008?
LanManserver service on server 2008 sp2 stuck in stopping state
ASP Menu control with css working in Firefox, and IE, but not in Chrome
Delete Old Exchange Emails
How do you mirror a hard drive