home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 23 web forms and controls / databinding / simplecontrolsform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  4.9 KB  |  108 lines

  1. Imports System.Data.SqlClient
  2.  
  3. Public Class WebForm1
  4.     Inherits System.Web.UI.Page
  5.  
  6. #Region " Web Form Designer Generated Code "
  7.  
  8.     'This call is required by the Web Form Designer.
  9.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  10.  
  11.     End Sub
  12.  
  13.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  14.         'CODEGEN: This method call is required by the Web Form Designer
  15.         'Do not modify it using the code editor.
  16.         InitializeComponent()
  17.     End Sub
  18.     Protected WithEvents ddlTitles As System.Web.UI.WebControls.DropDownList
  19.     Protected WithEvents txtTitle As System.Web.UI.WebControls.TextBox
  20.     Protected WithEvents txtPrice As System.Web.UI.WebControls.TextBox
  21.     Protected WithEvents ddlPublishers As System.Web.UI.WebControls.DropDownList
  22.     Protected WithEvents txtType As System.Web.UI.WebControls.TextBox
  23.     Protected WithEvents btnSave As System.Web.UI.WebControls.Button
  24.     Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
  25.  
  26. #End Region
  27.  
  28.     ' Define a connection object that points to Pubs.
  29.     Dim cn As New SqlConnection(SqlPubsConnString)
  30.  
  31.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  32.         If Not IsPostBack Then
  33.             ' Fill the two dropdownlist controls.
  34.             FillListControl(ddlTitles, cn, "Titles", "title", "title_id")
  35.             FillListControl(ddlPublishers, cn, "Publishers", "pub_name", "pub_id")
  36.             ' Display the first record
  37.             ShowTitleInfo(ddlTitles.Items(0).Value)
  38.         End If
  39.     End Sub
  40.  
  41.     ' Bind a List control to a table in a database.
  42.     ' (Provider-agnostic code)
  43.     Sub FillListControl(ByVal ctrl As ListControl, ByVal cn As IDbConnection, ByVal tableName As String, ByVal textField As String, ByVal valueField As String)
  44.         ' Open the connection if necessary.
  45.         If cn.State = ConnectionState.Closed Then cn.Open()
  46.         Dim cmd As IDbCommand = cn.CreateCommand
  47.         cmd.CommandText = String.Format("SELECT {0},{1} FROM {2}", textField, valueField, tableName)
  48.         Dim dr As IDataReader = cmd.ExecuteReader
  49.         ' Bind the control.
  50.         ctrl.DataSource = dr
  51.         ctrl.DataTextField = textField
  52.         ctrl.DataValueField = valueField
  53.         ctrl.DataBind()
  54.         ' Close the DataReader.
  55.         dr.Close()
  56.     End Sub
  57.  
  58.     Sub ShowTitleInfo(ByVal titleId As String)
  59.         ' Read info on a specific title.
  60.         If cn.State = ConnectionState.Closed Then cn.Open()
  61.         Dim cmd As New SqlCommand("SELECT * FROM Titles WHERE title_id='" & titleId & "'", cn)
  62.         Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow)
  63.         ' Fill all controls on the form.
  64.         dr.Read()
  65.         txtTitle.Text = dr("title").ToString
  66.         txtPrice.Text = dr("price").ToString
  67.         txtType.Text = dr("type").ToString
  68.         ' Display the correct element in the ddlPublishers control.
  69.         SelectItemFromValue(ddlPublishers, dr("pub_id").ToString)
  70.         ' close the DataReader.
  71.         dr.Close()
  72.     End Sub
  73.  
  74.     Private Sub ddlTitles_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTitles.SelectedIndexChanged
  75.         ' Update fields on the form
  76.         ShowTitleInfo(ddlTitles.SelectedItem.Value)
  77.     End Sub
  78.  
  79.     ' Update a record in the title record.
  80.     Sub UpdateTitle(ByVal title_id As String, ByVal title As String, ByVal price As String, ByVal type As String, ByVal pub_id As String)
  81.         ' Prepare the update command
  82.         Dim sql As String = "UPDATE Titles SET title=@title, price=@price, type=@type, pub_id=@pub_id WHERE title_id=@title_id"
  83.         Dim cmd As New SqlCommand(sql, cn)
  84.         cmd.Parameters.Add("@title", title)
  85.         cmd.Parameters.Add("@price", CDec(price))
  86.         cmd.Parameters.Add("@type", type)
  87.         cmd.Parameters.Add("@pub_id", pub_id)
  88.         cmd.Parameters.Add("@title_id", title_id)
  89.         If cn.State = ConnectionState.Closed Then cn.Open()
  90.         cmd.ExecuteNonQuery()
  91.     End Sub
  92.  
  93.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  94.         ' Retrieve title_id and pub_id of visible record.
  95.         Dim titleId As String = ddlTitles.SelectedItem.Value
  96.         Dim pubId As String = ddlPublishers.SelectedItem.Value
  97.         ' Update this record.
  98.         UpdateTitle(titleId, txtTitle.Text, txtPrice.Text, txtType.Text, pubId)
  99.         ' Ensure that the title in the dropdownlist control matches the new title.
  100.         ddlTitles.SelectedItem.Text = txtTitle.Text
  101.     End Sub
  102.  
  103.     Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
  104.         ' CLose the connection if still open
  105.         If cn.State <> ConnectionState.Closed Then cn.Close()
  106.     End Sub
  107. End Class
  108.