Confusion about An expression of non-boolean type specified in a context where a condition is expected

StackOverflow https://stackoverflow.com/questions/20782054

  •  21-09-2022
  •  | 
  •  

Domanda

I am fallowing an example .I have a field named Deleted in the db. .When I click the delete button actually not deleting data just replcaing "N" with "Y" value The problem is i downloaded the source code It works but after slightly changed I get this exception.I am wondering what am i doing wrong

this code works

         if (e.CommandName.Equals("Delete"))
         {

        long holidayNum = Convert.ToInt64(e.CommandArgument);




        using (SqlConnection con = new SqlConnection())
        {

            using (SqlCommand cmd = new SqlCommand())
            {

                cmd.Parameters.Add("@Deleted", SqlDbType.NChar).Value = "Y";

                cmd.CommandText = "Update HolidaysDetails set Deleted=@Deleted where HolidayNum=" + holidayNum;

                con.ConnectionString = connectionString;

                cmd.Connection = con;




                if (con.State == ConnectionState.Closed)
                {

                    con.Open();

                }

                int result = cmd.ExecuteNonQuery();

                con.Close();

                dlHoliday.EditItemIndex = -1;

                GetData();

            }

        }




    }

But my code throws an exception

      if (e.CommandName.Equals("delete"))
    {

        long holidayNum = Convert.ToInt64(e.CommandArgument);
         cmdText = "Update  HolidaysDetails set Deleted=@deleted where HolidayNum"  
         + holidayNum;
        using (con = new SqlConnection(cnnStr))
        {

            if (con.State == ConnectionState.Closed)
            {

                con.Open();
            }
            using (cmd = new SqlCommand())
            {

                cmd.Connection = con;
                cmd.CommandText = cmdText;
                cmd.Parameters.AddWithValue("@deleted", SqlDbType.NChar).Value = "Y";
                cmd.ExecuteNonQuery();
                con.Close();
                dlHoliday.EditItemIndex = -1;
                GetData();


            }
        }


    }
È stato utile?

Soluzione

Simply missing the equal sign here

cmdText = "Update  HolidaysDetails set Deleted=@deleted " + 
           "where HolidayNum=" + holidayNum;
                            ^

By the way, while you are trying to follow the correct practices and use a parameter for the @deleted flag, you are doing it wrong. AddWithValue requires, as second parameter, the value not the parameter type. While at fixing it, add also the second parameter required by the query

cmdText = "Update  HolidaysDetails set Deleted=@deleted " + 
          "where HolidayNum=@num";
......
cmd.Connection = con;
cmd.CommandText = cmdText;
cmd.Parameters.AddWithValue("@deleted", "Y");
cmd.Parameters.AddWithValue("@num", holidaynum);
cmd.ExecuteNonQuery();
....
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top