无法转换为bool类型

时间:2014-11-16 11:31:54

标签: c# implicit-conversion

我是C#的新手。我对bool类型的转换有问题。我有我的表tbl_Staff,其中IsActive列有类型位,我想绑定到combox并将cmbIsActive的数据发送到tbl_Staff列IsActive,但无法定义bit的类型转换。请帮助。

以下是我的程序和解决方案的代码。

表定义tbl_Staff

    CREATE TABLE [dbo].[tbl_Staff](
    [StaffID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](100) NOT NULL,
    [Address] [nvarchar](500) NULL,
    [Phone] [nvarchar](100) NULL,
    [Email] [nvarchar](100) NULL,
    [JoinedDate] [date] NULL,
    [Username] [nvarchar](50) NULL,
    [Password] [nvarchar](max) NULL,
    [CreatedDate] [date] NULL,
    [IsActive] [bit] NOT NULL
    ) ON [PRIMARY]

存储过程:sp_ManageUser

    ALTER PROCEDURE [dbo].[sp_ManageUser]
    -- Add the parameters for the stored procedure here
    @StaffIDId int,
    @Name nvarchar(100),
    @Address nvarchar(500),
    @Phone nvarchar(100),
    @Email nvarchar(100),
    @JoinedDate date,
    @Username nvarchar(50),
    @Password nvarchar(max),
    @CreatedDate date,
    @IsActive bit,
    @Mode varchar(1)
     AS
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    if(@Mode='I')
    insert into tbl_Staff       (Name,Address,Phone,Email,JoinedDate,Username,Password,CreatedDate,IsActive)values(@Name,@Address,@Phone,@Email,@JoinedDate,@Username,@Password,@CreatedDate,@IsActive)
if(@Mode='U')
Update tbl_Staff set Name=@Name,Address=@Address,Phone=@Phone,Email=@Email,JoinedDate=@JoinedDate,Username=@Username,Password=@Password,CreatedDate=@CreatedDate,IsActive=@IsActive where StaffID=@StaffIDId
if(@Mode='D')
Delete from tbl_Staff where StaffID=@StaffIDId
end

保存按钮的代码

private void btnSaveUser_Click(object sender, EventArgs e)
        {
            PSMBusinessLogicClass pblc = new PSMBusinessLogicClass();
            UserClass uc = new UserClass();
            try
            {
                bool result = uc.ManageUser(txtFullName.Text, txtAddress.Text, txtPhone.Text, txtEmail.Text, Convert.ToDateTime(dateTimePickerJoinedDate),txtUserame.Text, txtPassword.Text, Convert.ToDateTime(dateTimePickerCreatedDate),cmbIsActive, "I");
                if (result == true)
                {
                    MessageBox.Show("New Project Saved");
                    dgvUserDetails.DataSource = uc.SelectAllUsers();
                    //MakeFieldsBlank();
                }
                else
                {
                    MessageBox.Show("SOME ERRORS OCCURED WHILE PROCESSING THE REQUEST");
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

UserClass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;


namespace PSMDataAccessLayer
{
    public class UserClass
    {
        SqlConnection conn = new SqlConnection(DataConnectionClass.DbConnection);
        public int ManageUser( String Name, String Address, String Phone, String Email, DateTime JoinedDate, String Username, String Password, DateTime CreatedDate, Boolean IsActive, String Mode)
        {
            try
            {
                int result = 0;
                SqlCommand cmd = new SqlCommand("sp_ManageUser", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Name", Name);
                cmd.Parameters.AddWithValue("@Address", Address);
                cmd.Parameters.AddWithValue("@Phone", Phone);
                cmd.Parameters.AddWithValue("@Email", Email);
                cmd.Parameters.AddWithValue("@JoinedDate", JoinedDate);
                cmd.Parameters.AddWithValue("@Username", Username);
                cmd.Parameters.AddWithValue("@Password", Password);
                cmd.Parameters.AddWithValue("@CreatedDate", CreatedDate);
                cmd.Parameters.AddWithValue("@IsActive", IsActive);
                cmd.Parameters.AddWithValue("@Mode", Mode);
                conn.Open();
                result = cmd.ExecuteNonQuery();
                conn.Close();
                return result;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public DataTable SelectAllUsers()
        {
            try
            {
                SqlCommand cmd = new SqlCommand("Select * from tbl_Staff", conn);
                DataTable dt = new DataTable();
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                dt.Load(dr);
                conn.Close();
                return dt;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public int Login(String Username, String Password)
        {
            try
            {
                int result = 0;
                SqlCommand cmd = new SqlCommand("Select * from tbl_Staff where Username=@Username and Password=@Password", conn);
                cmd.Parameters.AddWithValue("@Username", Username);
                cmd.Parameters.AddWithValue("@Password", Password);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                conn.Close();
                if (dt.Rows.Count > 0)
                    result = 1;
                else
                    result = 0;
                return result;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

Convert.ToBoolean(value) - 将您的值转换为布尔值。

所以你的代码应该是这样的。

  

bool result = uc.ManageUser(txtFullName.Text,txtAddress.Text,   txtPhone.Text,txtEmail.Text,
  Convert.ToDateTime(dateTimePickerJoinedDate),txtUserame.Text,   txtPassword.Text,   Convert.ToDateTime(dateTimePickerCreatedDate),Convert.ToBoolean(cmbIsActive.SelectedValue)   " I&#34);

我不知道这是cmbIsActive这是什么,它是组合框。因此,您需要在调用函数Convert.ToBoolean

之前检索所选值