[英]Can someone help me to modify this code so it works in C# ASP.NET?
The user should be able to enter student id number in the textbox at the top of the form and all the information should be shown in the grid view order.用户应该能够在表格顶部的文本框中输入学生 ID 号,并且所有信息都应该以网格视图顺序显示。 The user should be able to click on the Return Book button to return the book
用户应该能够单击“归还书”按钮来归还图书
Note: I am not connecting to my database can someone correct this code please注意:我没有连接到我的数据库,请有人更正此代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
namespace CHS_Online_Library.Admin
{
public partial class ReturnBooks: System.Web.UI.Page
{
string str = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
dataGridView1_bookreturn.DataSource = GetBookReturnList();
}
private DataTable GetBookReturnList()
{
DataTable dtViewBook = new DataTable();
using(SqlConnection con = new SqlConnection(str))
{
using(SqlCommand cmd = new SqlCommand("SELECT * FROM BookReturn", con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
dtViewBook.Load(reader);
}
}
return dtViewBook;
}
protected void BR_Return_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
try
{
con.Open();
cmd.CommandText = "insert into BookReturn(StudentID, BookName,IssueDate,ReturnDate,) values('" + textBox_brsearch.Text + "', '" + BR_bookname.Text + "', '" + BR_issuedate.Text + "', '" + dateTimePicker1.Text + "')";
cmd.ExecuteNonQuery();
lblRMsg.Text = "Book was return successfully";
lblRMsg.Visible = true;
lblRMsg.ForeColor = Color.Green;
} catch (Exception ex) {
lblRMsg.Text = "Try Again";
lblRMsg.Visible = true;
lblRMsg.ForeColor = Color.Red;
} finally {
con.Close();
}
}
protected void textBox_brsearch_TextChanged(object sender, EventArgs e)
{
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "";
con.Open();
if (textBox_brsearch.Text != "")
{
SqlCommand cmd = new SqlCommand("Select BookName, IssueDate from IssueBook where StudentID =@StudentID", con);
cmd.Parameters.AddWithValue("@StudentID", int.Parse(textBox_brsearch.Text));
SqlDataReader da = cmd.ExecuteReader();
while (da.Read()) {
BR_bookname.Text = da.GetValue(0).ToString();
BR_issuedate.Text = da.GetValue(1).ToString();
}
con.Close();
}
}
}
}
}
I don't know if I'm right, but looking at your code and with what you explain I think this is it.我不知道我是否正确,但是查看您的代码以及您所解释的内容,我认为就是这样。
protected void Page_Load(object sender, EventArgs e)
{
dataGridView1_bookreturn.DataSource = GetBookReturnList();
dataGridView1_bookreturn.DataBind(); //you forget this part
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.