title: Getting a bit value from stored procedure in C#
body: <p>The situation is following, i have a stored procedure in SQL Server , this has a few output parameters, one of them is a bit type, what I want is to take the value of that parameter, but I have a conversion error, <code>InvalidCastException</code>.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public void exec()
         {
              String strConnString = "Server=.\\SQLEXPRESS;Database=recalls;Integrated Security=true";
              SqlConnection con = new SqlConnection(strConnString);
              SqlCommand cmd = new SqlCommand();
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.CommandText = "findVinCamp";
              int c = Int32.Parse(campaing.Text);
              cmd.Parameters.Add("@camp", SqlDbType.Int).Value = c;
              cmd.Parameters.Add("@vin", SqlDbType.VarChar, 100).Value = vin.Text;
              cmd.Parameters.Add("@desc", SqlDbType.NVarChar, 255).Direction = ParameterDirection.Output;
              cmd.Parameters.Add("@st", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
              cmd.Parameters.Add("@bit", SqlDbType.Bit).Direction = ParameterDirection.Output;
              cmd.Connection = con;
              con.Open();
              cmd.ExecuteNonQuery();
              bit = (int)cmd.Parameters["@bit"].Value; //Exception Here
              if (bit == 1)
              {
                  desc.Text = cmd.Parameters["@desc"].Value.ToString();
                  stt.Text = cmd.Parameters["@st"].Value.ToString();
                  camp = cmd.Parameters["@camp"].Value.ToString();
                  if (stt.Text.Equals("APPLIED"))
                  {
                      stt.ForeColor = System.Drawing.Color.LawnGreen;
                  }
                  else
                  {
                      stt.ForeColor = System.Drawing.Color.Firebrick;
                      label3.Enabled = true;
                      newstatus.Enabled = true;
                      update.Enabled = true;
                  }
              }
              else
              {
                  MessageBox.Show("Doesn't exits!");
              }
      }
  </code></pre>
  
  <p>I'm trying to assign the bit parameter to a int variable. Any question post on comments.</p>