Question: i have the following stored procedure in sql server and i am trying to take the permission value with C#. 

```
CREATE PROCEDURE [dbo].[GetPermission]
@userName varchar(50),
@permission int output
AS
BEGIN

select @permission = PERMISSION from USERS where UserName = @userName

END;

```

My C# code is the following:

```
        SqlCommand cmd = new SqlCommand(
            "sp_getPermission", conn);

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(
            new SqlParameter("@UserName", textBox1.Text));
        cmd.Parameters.Add(
           new SqlParameter("@permission", "none"));

        SqlDataReader rdr = null;

        rdr = cmd.ExecuteReader();
        MessageBox.Show( rdr["Permission"].ToString() );

```

But i get the following error on the last line of C# code:

> 
> Invalid attempt to read when no data is present.
> 
> 
> 

Any suggestions?

Answer: 
Let's simply this:

first, remove the output parameter `@permission`, then, change your procedure like this:

```
CREATE PROCEDURE [dbo].[GetPermission]
@userName varchar(50)
AS
BEGIN

select PERMISSION from USERS where UserName = @userName

END;

```

and for read the permission, use the [ExecuteScalar](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx) method:

```
SqlCommand cmd = new SqlCommand(
    "sp_getPermission", conn);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(
    new SqlParameter("@UserName", textBox1.Text));

var permission = (System.Int32)cmd.ExecuteScalar();

```
You need to `Read` your `SqlDataReader`:

```
rdr = cmd.ExecuteReader();
if (rdr.HasRows()){
   rdr.Read()
   MessageBox.Show( rdr["Permission"].ToString() );
   rdr.Close()
}

```

---

However, I don't believe you need a SqlDataReader for this situation. The following should work:

```
cmd.Parameters.Add(
           new SqlParameter("@permission", "none"));
cmd.Parameters["@permission"].Direction = ParameterDirection.Output;

cmd.ExecuteNonQuery();

MessageBox.Show(cmd.Parameters["@permission"].Value);

```