Object cannot be cast from DBNull to other types
<p>I have an insert stored procedure like this </p>

<pre><code>  if exists(select EmailId from Profile_Master where EmailId=@EmailId)
set @result=-1
else
begin
set @result=0


 insert into Profile_Master(FirstName,LastName,Dob,Gender,MobileNo,Country,State,EmailId,Password)
 values 
(@FirstName,@LastName,@Dob,@Gender,@MobileNo,@Country,@State,@EmailId,@Password)
set @id=SCOPE_IDENTITY()
return
end 

 pid = cmd1.Parameters.Add("@id", SqlDbType.Int);
            pid.Direction = ParameterDirection.Output;
            cmd1.ExecuteNonQuery();
            int res = Convert.ToInt32(pid.Value);
</code></pre>

<p>I am catching the value of last inserted record but i am getting an error like</p>

<p>Object cannot be cast from DBNull to other types.How to catch the value of last inserted record?</p>
