Parameter Direction Output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • klharding
    New Member
    • Oct 2009
    • 13

    Parameter Direction Output

    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.Sq lClient.SqlComm and.Parameters' cannot be used like a method." on the line "cmd.Parameters ("@PersonID").D irection = ParameterDirect ion.Output;"
    Code:
        public long InsertPerson()
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
    
            long lngPersonID = 0;
    
            try
            {
                conn = new SqlConnection("Server=Global2;DataBase=PictureCapture;Integrated Security=True");
                conn.Open();
    
                SqlCommand cmd = new SqlCommand("InsertPerson", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@practicecode", this.practicecode));
                cmd.Parameters.Add(new SqlParameter("@mrn", this.mrn));
                cmd.Parameters.Add(new SqlParameter("@fname", this.fname));
                cmd.Parameters.Add(new SqlParameter("@lname", this.lname));
                cmd.Parameters.Add(new SqlParameter("@dob", this.dob));
                cmd.Parameters.Add(new SqlParameter("@ssn", this.ssn));
                cmd.Parameters.Add(new SqlParameter("@PersonID", 0));
                cmd.Parameters("@PersonID").Direction = ParameterDirection.Output;
    
                rdr = cmd.ExecuteScalar();
                lngPersonID = Convert.ToInt32(cmd.Parameters("@PersonID").Value);
    
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            return lngPersonID;
        }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Parameters is an array, not a function.
    This is C#, use the array brackets, not the function brackets
    cmd.Parameters["@PersonID"].Direction = ParameterDirect ion.Output;

    Comment

    • klharding
      New Member
      • Oct 2009
      • 13

      #3
      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.ExecuteScal ar();
      "Cannot implicitly convert type 'object' to 'System.Data.Sq lClient.SqlData Reader'. An explicit conversion exists (are you missing a cast?)"

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Then cast it :-)

        sdr= (SqlDataReader) cmd.ExecuteScal ar();

        Comment

        • klharding
          New Member
          • Oct 2009
          • 13

          #5
          Thanks again Plater!

          Comment

          Working...