退回的书籍贷款在c#中不起作用

时间:2015-12-24 04:42:54

标签: c# sql asp.net

我已经在aspx和c#中创建了学生库页面。

我添加了两个按钮,一个用于提交点击的图书借阅,另一个是返回按钮簿。

例如:

我输入学生ID和书籍ID,然后点击提交方式,它显示状态为'等待'。

如果我为退回的书输入相同的学生ID和书籍ID,则显示状态为'返回'。

上述过程正常。

但是,如果为图书借阅menas输入相同的学生ID和书籍ID,它就不起作用。

我可以知道吗?

我是.net的新手,有人可以指导我吗?

任何帮助都将受到高度赞赏。

感谢。

源代码:

图书借阅:

protected void btnsub_Click(object sender, EventArgs e)
        {
                SqlConnection con = Connection.DBconnection();
                SqlCommand com = new SqlCommand("sp_lendingstatus", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@studentid", txtstudentid.Text.Trim());
                com.Parameters.AddWithValue("@bookid", txtbookid.Text.Trim());
                com.Parameters.AddWithValue("@date", Calendar1.TodaysDate.Date.ToString());
                com.Parameters.AddWithValue("@returndate", Calendar2.SelectedDate.ToString());
                SqlParameter retval = new SqlParameter("@output", SqlDbType.VarChar, 50);
                retval.Direction = ParameterDirection.Output;
                com.Parameters.Add(retval);
                txtstudentid.Text = string.Empty;
                txtbookid.Text = string.Empty;
                com.ExecuteNonQuery();
                string Output = retval.Value.ToString();
                output.Text = Output;

        }

返回:

protected void btnrtn_Click(object sender, EventArgs e)
        {
            SqlConnection con = Connection.DBconnection();
            SqlCommand com = new SqlCommand("sp_bookreturn", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@studentid", txtstudentid.Text.Trim());
            com.Parameters.AddWithValue("@bookid", txtbookid.Text.Trim());
            com.Parameters.AddWithValue("@returnstatus", txtbookid.Text.Trim());
            com.ExecuteNonQuery();

        }

页面加载:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlConnection con = Connection.DBconnection();
                {
                    SqlCommand com = new SqlCommand("sp_selectlendingstatus", con);
                    com.CommandType = CommandType.StoredProcedure;                  
                    SqlDataAdapter adp = new SqlDataAdapter(com);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    GridView1.DataSource = ds;
                    GridView1.DataBind();

                }
            }
        }

sp_selectlendingstatus :(用于显示状态的page_load)

select *, (CASE WHEN book_lending.bookid IS NULL THEN 'Available' ELSE 'Not 
Available' END) as status from studentlibrary left outer join book_lending 
ON book_lending.bookid=studentlibrary.Book_id and 
book_lending.returnstatus='pending'

用于图书借阅的sp_lendingstatus:

ALTER PROCEDURE sp_lendingstatus
(
@studentid int,
@output varchar(50) output,
@bookid int,
@date varchar(50),
@returndate varchar(50)
)
AS
IF NOT EXISTS (SELECT * FROM student WHERE ID=@studentid)
BEGIN
SET @output = 'student id does not exist'
END
ELSE IF NOT EXISTS (SELECT * FROM studentlibrary WHERE Book_id=@bookid) 
begin
SET @output = 'Book id does not exist'
END
ELSE IF EXISTS (SELECT * FROM book_lending inner join studentlibrary ON studentlibrary.Book_id=book_lending.bookid where Book_id=@bookid)
begin
SET @output = 'Book id not available'
return
End
ELSE
BEGIN
Insert into book_lending (studentid,bookid,date,returndate) values (@studentid,@bookid,@date,@returndate) 
END

book_return:

ALTER PROCEDURE sp_bookreturn
(
@returnstatus varchar(50),
@bookid int,
@studentid int
)
AS
begin
update book_lending set returnstatus='Returned' where bookid=@bookid and studentid=@studentid
End

注意:我为bookid和studentid设置了外键。

1 个答案:

答案 0 :(得分:1)

由于存储过程中的此声明

ELSE IF EXISTS (SELECT * FROM book_lending inner join studentlibrary ON studentlibrary.Book_id=book_lending.bookid where Book_id=@bookid)

一旦书籍被检出一次,无论是否已经退回,这都会有结果。你没有删除被借出的书的记录(好主意)。更新此项以排除显示已退回图书的记录

ELSE IF EXISTS (SELECT * FROM book_lending inner join 
studentlibrary ON studentlibrary.Book_id=book_lending.bookid 
Where Book_id=@bookid AND book_lending.returnstatus<>'Returned')

此更新本质上应该使本书准备好再次借出。