Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
When i execute my code I get this. what does it mean???
System.Data.SqlClient.SqlException (0x80131904): An expression of non-boolean type specified in a context where a condition is expected, near '1'.

[edit] Added code from comment
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand cmd;
        SqlDataReader dr;
        string SqlStr;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=employee;Integrated Security=True");
            
            //textBox1.Text = con.ConnectionString;
            con.Open();
            cmd = con.CreateCommand();
            cmd.CommandText = "Select Empno,Ename,Job,Salary From Emplye Order By Empno";
            dr = cmd.ExecuteReader();
            ShowData();

        }
        private void ShowData()
        {
            if (dr.Read())
            {
                textBox1.Text = dr[0].ToString();
                textBox2.Text = dr[1].ToString();
                textBox3.Text = dr[2].ToString();
                textBox3.Text = dr[3].ToString();
            }
            else   MessageBox.Show("No data exists.");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ShowData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text=textBox2.Text=textBox3.Text=textBox4.Text="";
            dr.Close();
            cmd.CommandText="select IsNULL(Max(Empno),1000)+1 From Emplye";
            textBox1.Text=cmd.ExecuteScalar().ToString();
            button3.Enabled = true;
            textBox2.Focus();
        }
        private void ExecuteDML()
        {
            DialogResult d = MessageBox.Show("Are you sure of executing the below Sql Statement?\n\n" + SqlStr, "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                cmd.CommandText = SqlStr;
                int count = cmd.ExecuteNonQuery();
                if (count > 0) MessageBox.Show("Statement excuted successfully");
                else
                    MessageBox.Show("Statement failed execution");
                ShowData();
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            SqlStr="Insert Emplye(Empno,Ename,Job,Salary)Values("+textBox1.Text+",'"+textBox2.Text+"'.'"+textBox3.Text+"',"+textBox4.Text+")";
            ExecuteDML();
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlStr="Update Emplye Set Ename='"+textBox2.Text+"',Job='"+textBox3.Text+"',salary="+textBox4.Text+"where Empno+"+textBox1.Text;dr.Close();
            ExecuteDML();
        }

        private void button5_Click(object sender, EventArgs e)
        {
             SqlStr="Delete From Employe where Empno="+textBox1.Text;
             dr.Close();
             ExecuteDML();
        }

        private void button6_Click(object sender, EventArgs e)
        {
         if(con.State != ConnectionState.Closed)
         {
             con.Close();
         }
         this.Close();
        }
    }
}

[/edit]
Posted
Updated 24-Oct-13 3:21am
v2
Comments
CPallini 24-Oct-13 9:10am    
The message is quite clear: you should provide a boolean value, but you are providing instead a value of another type. Of course, without seeing your code, we cannot help you.
Achyuth Guddeti 24-Oct-13 9:12am    
tanq
phil.o 24-Oct-13 9:26am    
Very very bad practice to construct your SQL queries like you do by concatenating string values obtained from your controls.

Please use parameterized queries instead.

The problem lies in one of your queries, so you just have to focus on these ones, and find which of them is causing the issue (tip: F5 will allow you to see the actual content of SqlStr).

Update your button4_click event as mentioned below :

C#
private void button4_Click(object sender, EventArgs e)
      {
          SqlStr="Update Emplye Set Ename='"+textBox2.Text+"',Job='"+textBox3.Text+"',salary="+textBox4.Text+"where Empno = "+textBox1.Text;
          dr.Close();
          ExecuteDML();
      }
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900