Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not IsPostBack Then
BindData()
End If
End Sub
Private Sub BindData()
Dim obj As DBController = New DBController
obj.BindListBox("select * from BookInfo order by BookID asc", BookList)
obj.Close()
End Sub
Private Sub BookList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BookList.SelectedIndexChanged
Dim BookID As String = BookList.SelectedValue.ToString.Trim
Dim sqlstr As String = "select * from BookInfo where BookID =" + BookID
Dim obj As DBController = New DBController
obj.Open()
Dim myreader As SqlDataReader = obj.ExecReaderSql(sqlstr)
If myreader.Read Then
txtBookName.Text = myreader("BookName")
txtPubName.Text = myreader("PubName")
txtBookAuthor.Text = myreader("BookAuthor")
DpStyle.SelectedValue = myreader("BookStyle")
txtUploadDate.Text = myreader("UploadDate")
txtPubDate.Text = myreader("PubDate")
txtSeries.Text = myreader("Series")
txtSubject.Text = myreader("Subject")
DpLanguage.SelectedValue = myreader("Language")
txtISBN.Text = myreader("ISBN")
txtSearchNO.Text = myreader("SearchNO")
DpBookState.SelectedValue = myreader("BookState")
txtPrice.Text = myreader("Price")
txtBarCode.Text = myreader("BarCode")
End If
obj.Close()
------解决方案--------------------------------------------------------
- VB.NET code
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 If Not IsPostBack Then BindData()'首次加载时绑定数据 End If End Sub Private Sub BindData()’绑定BookList对象的方法 Dim obj As DBController = New DBController obj.BindListBox("select * from BookInfo order by BookID asc", BookList) obj.Close() End Sub Private Sub BookList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BookList.SelectedIndexChanged Dim BookID As String = BookList.SelectedValue.ToString.Trim'取选中项的值 Dim sqlstr As String = "select * from BookInfo where BookID =" + BookID'根据取值拼结sql字符串 Dim obj As DBController = New DBController obj.Open() Dim myreader As SqlDataReader = obj.ExecReaderSql(sqlstr) If myreader.Read Then'取出记录集,若存在,则对下面的一系列控件赋值 txtBookName.Text = myreader("BookName") txtPubName.Text = myreader("PubName") txtBookAuthor.Text = myreader("BookAuthor") DpStyle.SelectedValue = myreader("BookStyle") txtUploadDate.Text = myreader("UploadDate") txtPubDate.Text = myreader("PubDate") txtSeries.Text = myreader("Series") txtSubject.Text = myreader("Subject") DpLanguage.SelectedValue = myreader("Language") txtISBN.Text = myreader("ISBN") txtSearchNO.Text = myreader("SearchNO") DpBookState.SelectedValue = myreader("BookState") txtPrice.Text = myreader("Price") txtBarCode.Text = myreader("BarCode") End If obj.Close()'关闭数据库对象