Connecting Tech Pros Worldwide Help | Site Map

Parameter Direction Output

Newbie
 
Join Date: Oct 2009
Posts: 13
#1: Oct 8 '09
I have an SQL Stored Procedure that is outputting (using scope_indentity) the PersonID after a record is inserted into the table. I need this outputted value to be returned to my application. I have tried the following but I am getting errors..."Non-invocable member 'System.Data.SqlClient.SqlCommand.Parameters' cannot be used like a method." on the line "cmd.Parameters("@PersonID").Direction = ParameterDirection.Output;"
Expand|Select|Wrap|Line Numbers
  1.     public long InsertPerson()
  2.     {
  3.         SqlConnection conn = null;
  4.         SqlDataReader rdr = null;
  5.  
  6.         long lngPersonID = 0;
  7.  
  8.         try
  9.         {
  10.             conn = new SqlConnection("Server=Global2;DataBase=PictureCapture;Integrated Security=True");
  11.             conn.Open();
  12.  
  13.             SqlCommand cmd = new SqlCommand("InsertPerson", conn);
  14.             cmd.CommandType = CommandType.StoredProcedure;
  15.             cmd.Parameters.Add(new SqlParameter("@practicecode", this.practicecode));
  16.             cmd.Parameters.Add(new SqlParameter("@mrn", this.mrn));
  17.             cmd.Parameters.Add(new SqlParameter("@fname", this.fname));
  18.             cmd.Parameters.Add(new SqlParameter("@lname", this.lname));
  19.             cmd.Parameters.Add(new SqlParameter("@dob", this.dob));
  20.             cmd.Parameters.Add(new SqlParameter("@ssn", this.ssn));
  21.             cmd.Parameters.Add(new SqlParameter("@PersonID", 0));
  22.             cmd.Parameters("@PersonID").Direction = ParameterDirection.Output;
  23.  
  24.             rdr = cmd.ExecuteScalar();
  25.             lngPersonID = Convert.ToInt32(cmd.Parameters("@PersonID").Value);
  26.  
  27.         }
  28.         finally
  29.         {
  30.             if (conn != null)
  31.             {
  32.                 conn.Close();
  33.             }
  34.             if (rdr != null)
  35.             {
  36.                 rdr.Close();
  37.             }
  38.         }
  39.         return lngPersonID;
  40.     }
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,158
#2: Oct 8 '09

re: Parameter Direction Output


Parameters is an array, not a function.
This is C#, use the array brackets, not the function brackets
cmd.Parameters["@PersonID"].Direction = ParameterDirection.Output;
Newbie
 
Join Date: Oct 2009
Posts: 13
#3: Oct 8 '09

re: Parameter Direction Output


Thanks, that solved that issue. I guess I am having a hard time letting go of the VB.NET I am used to!

But now I am getting an error on the line...
rdr = cmd.ExecuteScalar();
"Cannot implicitly convert type 'object' to 'System.Data.SqlClient.SqlDataReader'. An explicit conversion exists (are you missing a cast?)"
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,158
#4: Oct 8 '09

re: Parameter Direction Output


Then cast it :-)

sdr= (SqlDataReader)cmd.ExecuteScalar();
Newbie
 
Join Date: Oct 2009
Posts: 13
#5: Oct 8 '09

re: Parameter Direction Output


Thanks again Plater!
Reply