Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

below code is working for me
C#
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Name", TxtSearch.Text.ToString());
SqlParameter op1 = new SqlParameter();
op1.ParameterName = "@ItemKey";
op1.SqlDbType = System.Data.SqlDbType.Int;
op1.Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add(op1);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
string val = op1.Value.ToString();

but if I want a varchar (50) value to be returned
I am using op1.SqlDbType = System.Data.SqlDbType.varchar;
which is giving an error

so please guide


Thanks and Regards,
Praveen Machat
Posted
Updated 11-Jul-14 5:13am
v3
Comments
CHill60 11-Jul-14 11:16am    
What is the error?
praveenmachat 11-Jul-14 11:21am    
String[1]: the Size property has an invalid size of 0.

That's quite a long-winded way of creating the parameter but if you do it that way you also need to set the size
op1.SqlDbType = System.Data.SqlDbType.varchar;
op1.Size=50;

Alternatively you could do it all on one line..
C#
SqlParameter op1 = new SqlParameter("@name", System.Data.SqlDbType.VarChar, 50);
 
Share this answer
 
Comments
praveenmachat 11-Jul-14 11:49am    
Thank you very much....

Praveen
CHill60 11-Jul-14 20:48pm    
Hope that sorts it out
praveenmachat 12-Jul-14 0:00am    
Yes ,Thanks.
Can you please guide me for http://www.codeproject.com/Questions/795817/How-do-retrieve-data-table-from-stored-procedure
RaisKazi 11-Jul-14 14:31pm    
My 5.
CHill60 11-Jul-14 20:48pm    
Thank you!
You can't use ExecuteNonQuery to get a result back.
Use ExecuteReader or ExecuteScalar.
 
Share this answer
 
Comments
praveenmachat 11-Jul-14 11:22am    
but I am able to get int values
praveenmachat 11-Jul-14 11:24am    
Can you please provide code sample,
I just need to receive all my out put values (Result of stored procedure) to my C# code

Thanks,
Praveen
CHill60 11-Jul-14 11:28am    
I disagree - OP is using an out parameter as opposed to a return value
praveenmachat 11-Jul-14 11:35am    
i am just beginner ,my knowledge is very limited....

for below code I am getting above error

using (var connection = new SqlConnection("data source=TP-PC;Persist Security Info=false;database=Asset;user id=sa;password=asd123...;Connection Timeout = 0"))
using (var command = new SqlCommand("codesearch", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Code", TxtSearch.Text.ToString());

SqlParameter op1 = new SqlParameter();
op1.ParameterName = "@Name";
op1.SqlDbType = System.Data.SqlDbType.VarChar;
op1.Direction = System.Data.ParameterDirection.Output;

SqlParameter op2 = new SqlParameter();
op2.ParameterName = "@Description";
op2.SqlDbType = System.Data.SqlDbType.VarChar;
op2.Direction = System.Data.ParameterDirection.Output;

command.Parameters.Add(op1);
command.Parameters.Add(op2);

connection.Open();

command.ExecuteNonQuery();
connection.Close();
string code1 = op1.Value.ToString();
string code2 = op2.Value.ToString();
}

My procedure

ALTER procedure [dbo].[codesearch]
(
@Code varchar(50),
@Name varchar(50) out,
@Description varchar(50) out
)
AS
Begin

select @Name = Name,@Description = Description from MstAsset where Code= @Code

End
and for below code I am getting int value

using (var connection = new SqlConnection("data source=TP-PC;Persist Security Info=false;database=Asset;user id=sa;password=asd123...;Connection Timeout = 0"))
using (var command = new SqlCommand("codesec", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@Name", TxtSearch.Text.ToString());

SqlParameter op1 = new SqlParameter();
op1.ParameterName = "@ItemKey";
op1.SqlDbType = System.Data.SqlDbType.Int;
op1.Direction = System.Data.ParameterDirection.Output;

command.Parameters.Add(op1);

connection.Open();
command.ExecuteNonQuery();
connection.Close();
string val = op1.Value.ToString();
}

my procedure

ALTER procedure [dbo].[codesec]
(
@Name varchar(50),
@ItemKey int out
)
AS

begin

select @ItemKey =ItemKey from MstAsset where Name= @Name

end
George Jonsson 11-Jul-14 13:39pm    
I missed that little detail. Sigh.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900