简体   繁体   English

有人可以帮我修改此代码,使其在 C# ASP.NET 中工作吗?

[英]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
}

See the reference .请参阅参考资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以告诉我为什么我的标题没有显示asp.net C# - Can someone tell me why my header isn't showing up asp.net c# 有人可以将我指向一个练习C#/ ASP.net网站 - Can someone point me towards a practice C# / ASP.net website 有人可以在C#中帮我解决这个问题吗 - Can someone help me solve this in c# 有人可以在C#中帮助我处理数组吗? - Can someone help me with arrays in c#? 如何将复选框布尔值转换为字符串,有人可以帮助我ASP.NET MVC吗? - How to convert value from checkbox bool to string can someone help me ASP.NET MVC? 种子方法中的类似代码,第一个起作用,第二个不起作用。 ASP.NET MVC向用户添加角色。 谁能帮我这个? - Similar code in seed method, first works, second not. ASP.NET MVC adding role to user. Can anyone help me with this? 如何编码html源代码,以便没有人可以在网上看到它。 asp.net c# - How to encode html source code so that no one can see it on web. asp.net c# 有人可以使用C#统一代码为我的测验游戏提供帮助吗 - Can someone help me in my Quiz Game using C# unity code 我如何在 C# 中执行它有人可以帮助我 - how can i execute this in C# can someone help me 有人可以向我解释这个ASP.NET MVC代码块吗? - Can someone explain this block of ASP.NET MVC code to me, please?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM