无法通过Windows窗体从表中删除用户

时间:2014-12-04 20:36:01

标签: c# winforms class stored-procedures

我在从表中删除用户时遇到问题。我可以将数据从表单插入到表中,但删除它时只会将else语句结果显示为“在处理请求时发生的一些错误”。 StaffID是自动增量。请帮忙。

删除按钮:

 private void btnDeleteUser_Click(object sender, EventArgs e)
    {
        try
        {
            int result = uc.ManageUser(txtFullName.Text, txtAddress.Text, txtPhone.Text, txtEmail.Text, Convert.ToDateTime(dateTimePickerJoinedDate.Text), txtUserame.Text, txtPassword.Text, Convert.ToDateTime(dateTimePickerCreatedDate.Text), "D");
            if (result == 1)
            {
                MessageBox.Show("User Deleted");
                dgvUserDetails.DataSource = uc.SelectAllUsers();
                //MakeFieldsBlank();
            }
            else
            {
                MessageBox.Show("SOME ERRORS OCCURRED WHILE PROCESSING THE REQUEST");
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }
    }
}

ManageUser Class

public int ManageUser(String Name, String Address, String Phone, String Email, DateTime JoinedDate, String Username, String Password, DateTime CreatedDate, String Mode)
    {
        try
        {
            int result = 0;
            SqlCommand cmd = new SqlCommand("sp_ManageUser", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@StaffID",DBNull.Value);
            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;
        }
    }

过程:sp_ManageUser

USE [db_ProjectStatusManager]
GO
/****** Object:  StoredProcedure [dbo].[sp_ManageUser]    Script Date: 12/05/2014 01:29:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
   -- Author:       <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[sp_ManageUser]
-- Add the parameters for the stored procedure here
@StaffID int,
@Name nvarchar(100),
@Address nvarchar(500),
@Phone nvarchar(100),
@Email nvarchar(100),
@JoinedDate date,
@Username nvarchar(50),
@Password nvarchar(max),
@CreatedDate date,
@Mode varchar(1)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT OFF;


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

将用户加载到TextBox

    private void FrmUsers_Load(object sender, EventArgs e)
    {
        UserClass uc = new UserClass();
        dgvUserDetails.DataSource = uc.SelectAllUsers();
        dgvUserDetails.AllowUserToAddRows = false;
        dgvUserDetails.AllowUserToOrderColumns = false;
                    panel1.Enabled = false;
    }

UserClass的。 SelectAllUsers

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;
        }
    }

2 个答案:

答案 0 :(得分:1)

您需要传递参数@StaffID的值,因为SP需要此参数用于UPDATE和DELETE部分。只有INSERT部分不需要@StaffID值

uc.ManageUser(txtStaffID.Text, txtFullName.Text, .......
....

public int ManageUser(string staffID, String Name, ......)
{
    try
    {
        int result = 0;
        SqlCommand cmd = new SqlCommand("sp_ManageUser", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@StaffID", Convert.ToInt32(staffID));
        cmd.Parameters.AddWithValue("@Name", Name);
        ....
}

当然,这意味着您需要在加载用户数据时保存该值。

还要注意参数@StaffID的数据类型。 SP期望整数不是字符串。

答案 1 :(得分:0)

您在命令参数的'StaffID'列中传递了一个NULL值,但您的商店程序的条件为'StaffID',首先您需要获取'StaffID'然后传递它。

您通过简单查询获得StaffID

Select StaffID from tbl_Staff where Name=@Name and Username = @Username  ;

您可以按照此代码获取Staffid

public int getstaffid()
{ 
int staffid = 0;
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
string query = " Select StaffID from tbl_Staff where Name=@Name and Username = @Username";
cmd.CommandText = query;

SqlParameter param = new SqlParameter("@Name", txtFullName.Text);
cmd.Parameters.Add(param);
SqlParameter param = new SqlParameter("@Username", txtUserame.Text);
cmd.Parameters.Add(param);
 try
{
    con.Open();

    staffid= (Int32)cmd.ExecuteScalar();    
    return staffid;       
}
catch (Exception ex)
{
    throw;
}
}

现在在ManagerUSer()

public int ManageUser(String Name, ......)
{
try
{
    int Staffid = getstaffid();
    int result = 0;
    SqlCommand cmd = new SqlCommand("sp_ManageUser", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@StaffID",Staffid);
    cmd.Parameters.AddWithValue("@Name", Name);
    ....
}
}