sql parameter

M

MikeJ

example below can we get a parameter by name or have to use the indexOf

SqlCommand cmd = new SqlCommand("proc_ERRORLOG_INSERT", oConn);

cmd.CommandType = CommandType.StoredProcedure;

SqlParameter param = new SqlParameter();

cmd.Parameters.Add("@Fileid", SqlDbType.Int).Value=99999;

cmd.Parameters.Add("@ErrorMessage", SqlDbType.VarChar).Value="Service Failed
on APP Server1";

cmd.Parameters.Add("@Retvalue",
SqlDbType.Int).Direction=ParameterDirection.Output;

int iret = cmd.ExecuteNonQuery();

Console.WriteLine("return value "+iret.ToString()); //iRet is Rows
Affected

Console.WriteLine(cmd.Parameters.IndexOf("@Retvalue")); // Proc Return
Number (value)

console.Read();

how can i get the @retvalue by name , Not by its index position in the
collection



TIA MJ
 
N

Nicholas Paldino [.NET/C# MVP]

MikeJ,

You should be able to do:

SqlParameter = cmd.Parameters["@retvalue"];

Hope this helps.
 
M

MikeJ

Thank you
MJ

Nicholas Paldino said:
MikeJ,

You should be able to do:

SqlParameter = cmd.Parameters["@retvalue"];

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

MikeJ said:
example below can we get a parameter by name or have to use the indexOf

SqlCommand cmd = new SqlCommand("proc_ERRORLOG_INSERT", oConn);

cmd.CommandType = CommandType.StoredProcedure;

SqlParameter param = new SqlParameter();

cmd.Parameters.Add("@Fileid", SqlDbType.Int).Value=99999;

cmd.Parameters.Add("@ErrorMessage", SqlDbType.VarChar).Value="Service
Failed on APP Server1";

cmd.Parameters.Add("@Retvalue",
SqlDbType.Int).Direction=ParameterDirection.Output;

int iret = cmd.ExecuteNonQuery();

Console.WriteLine("return value "+iret.ToString()); //iRet is Rows
Affected

Console.WriteLine(cmd.Parameters.IndexOf("@Retvalue")); // Proc Return
Number (value)

console.Read();

how can i get the @retvalue by name , Not by its index position in the
collection



TIA MJ
 
M

MikeJ

here is a piece of my code...the parameter fileid is not returning
SqlCommand Cmd = new SqlCommand("oa_SERVICE_OA2_getFileToProcess",
oConnect);

Cmd.CommandType = CommandType.StoredProcedure;

Cmd.CommandTimeout = 600;

Cmd.Parameters.Add("Statusid", SqlDbType.Int).Value = 123; //Direction =
ParameterDirection.Input;

Cmd.Parameters.Add("FormTypeid", SqlDbType.Int).Value = 2;//Direction =
ParameterDirection.Input;

Cmd.Parameters.Add("SetStatusIdTo", SqlDbType.Int).Value = 139; //Direction
= ParameterDirection.Input;

Cmd.Parameters.Add("FileId", SqlDbType.Int).Direction =
ParameterDirection.Output;

Cmd.Parameters.Add("FilePath", SqlDbType.VarChar,244).Direction =
ParameterDirection.Output;

Cmd.Parameters.Add("ReturnCode", SqlDbType.Int).Direction =
ParameterDirection.ReturnValue;

int iRet = Cmd.ExecuteNonQuery();

Console.WriteLine(Cmd.Parameters["ReturnCode"].Value); //shows
correct value

Console.WriteLine(Cmd.Parameters["Fileid"].Value); //shows
empty

Console.WriteLine(Cmd.Parameters["FilePath"].Value); //Empty

Console.WriteLine("Ret {0}", iRet.ToString()); // Shows
Correct Value

Console.Read();


Tia
MJ




Alberto Poblacion said:
MikeJ said:
how can i get the @retvalue by name , Not by its index position in the
collection

cmd.Parameters["@retvalue"] should work.
 
A

Alberto Poblacion

MikeJ said:
here is a piece of my code...the parameter fileid is not returning
[...]
Cmd.Parameters.Add("FileId", SqlDbType.Int).Direction = [...]
Console.WriteLine(Cmd.Parameters["Fileid"].Value);
//shows empty

I suspect a casing problem: you are creating the parameter as "FileId"
(uppercase "I") and then looking for "Fileid" (lowercase "i").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top