Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Why only one character is returned?
Message
 
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01544467
Message ID:
01544509
Views:
21
>>>>Hi,
>>>>
>>>>This is a continuation of the thread where I am trying to get the value of OUTPUT parameter when calling a stored procedure. The stored procedure is very simple and has one OUTPUT parameter where the value is set to something like 'ABCD' (I tried, '1234', and so on).
>>>>
>>>>The problem is that the code below, when gets the value of parameter only shows 1 character. E.g. 'A' or '1', and so on.
>>>>
>>>>Here is the summary of the code (basically copy of what Viv suggested in the other thread):
>>>>
>>>> System.Data.Common.DbCommand command = new SqlCommand();
>>>> System.Data.IDbDataParameter p = command.CreateParameter();
>>>> p.ParameterName = "@Param3";
>>>> p.Direction = ParameterDirection.Output;
>>>> p.Value = cVal3;
>>>> command.Parameters.Add(p);
>>>>int RetValue = command.ExecuteNonQuery()
>>>> var v = command.Parameters["@Param3"].Value;
>>>>// the value 'v' has 'A' or '1' but never the entire string
>>>>
>>>>
>>>>Do you see where could be the problem? TIA.
>>>
>>>You missed out the most important line. Snip:
>>>p.Direction = ParameterDirection.Output;
>>>//etc  <------- This one :-}
>>>p.Value = cVal3;
I think you have this working now anyway but here's a complete example. Given
CREATE PROCEDURE [dbo].[MyProc]
>>>   @Param3  nvarchar(30) OUT
>>>AS
>>>BEGIN
>>>   SET @Param3 = 'Hello ' + @Param3
>>>   RETURN
>>>END
Then
            SqlConnection conn = new SqlConnection();
>>>            conn.ConnectionString = s;
>>>            conn.Open();
>>>
>>>            System.Data.Common.DbCommand command = new SqlCommand();
>>>            System.Data.IDbDataParameter p = command.CreateParameter();
>>>
>>>            p.ParameterName = "@Param3";
>>>            p.Direction = ParameterDirection.InputOutput;
>>>            p.Value = "World";
>>>            p.DbType = System.Data.DbType.String;
>>>            p.Size = 30;
>>>            command.Parameters.Add(p);
>>>
>>>            command.Connection = conn;
>>>            command.CommandType = System.Data.CommandType.StoredProcedure;
>>>            command.CommandText = "MyProc";
>>>            command.ExecuteNonQuery();
>>>            string result = (string) command.Parameters["@Param3"].Value;
>>
>>Yes, I see that I missed specifying the Size of the passed parameter. Once I added this (Size) the code works. Except (I have not tested it today) yesterday the call to ExecuteNonQuery() was returning -1. Maybe it is normal but I thought it should be 0. I will recheck.
>
>Docs:"
>For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1."
>
>>One other question I have. I see that you specified ParameterDirection as .InputOutput. Why not only .Output?
>I only used InPutOutput because I was also using the parameter to pass in a value - if that wasn't the case then Output would be fine..
>.

Thank you for the explanation.
1. I see why I was getting -1 since my code was not updating, deleting, etc.
2. I also understand now that if you pass a parameter you need .Input; hence InputOutput. I was actually passing an empty string as a value and didn't think of it as passing a parameter. Now I understand.
Again, thank you!
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform