2

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, InvalidCastException.

This is my 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!");
            }
    }

I'm trying to assign the bit parameter to a int variable. Any question post on comments.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
TimeToCode
  • 901
  • 2
  • 16
  • 34

4 Answers4

4

I change the (int) to this, now works perfectly:

Boolean lol = Convert.ToBoolean(cmd.Parameters["@bit"].Value);
TimeToCode
  • 901
  • 2
  • 16
  • 34
1

I believe a bit will convert to a boolean. Which should make your code a bit simpler too.

i.e.

...
var bit = (bool)cmd.Parameters["@bit"].Value;
if (bit)
{
...
meganaut
  • 493
  • 2
  • 10
1

Use this following Line

bool isConfirmed = (bool)cmd.Parameters["@bit"].Value;
if(isConfirmed ){
                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!");
}

**UPDATE: **if the bit column allows nulls -- many ways you can do this

bool isConfirmed = cmd.Parameters["@bit"].Value as bool? ?? null;

and also read this- SQL Server Data Types and Their .NET Framework Equivalents

Community
  • 1
  • 1
RU Ahmed
  • 558
  • 4
  • 23
0

You are trying to convert your boolean output to INT, please convert it in Boolean:

bool bitValue= Convert.ToBoolean(cmd.Parameters["@bit"].Value)