ASP.NET Binding and an Introduction to Database Queries Please use speaker notes for additional information!
My schedule sub Page_Load if Not Page.IsPostBack then dim myCourses=New ArrayList myCourses.Add("CIS13") myCourses.Add("CIS44") myCourses.Add("CIS45") myCourses.Add("CIS46") myCourses.Add("CIS47") myCourses.Add("CIS48") myCourses.Add("CIS49") myCourses.Add("CIS50") myCourses.Add("CIS53") myCourses.Add("CIS63") myCourses.Sort() crs.DataSource=myCourses crs.DataBind() end if end sub sub showSchedule(s as Object,e As EventArgs) lblSchedule.text=lblSchedule.text & " " & crs.SelectedItem.Text end sub <asp:DropDownList id="crs" runat="server" AutoPostBack="True" onSelectedIndexChanged="showSchedule" /> Since Page_Load is run every time the page is loaded this can be used to control running only on first time. The ArrayList is myCourses, the DropDown list has an id of crs. The DataSource property of the DropDownList is set to the Array List name and the DataBind() method of the DropDownList binds the data.
My schedule <!-- function __doPostBack(eventTarget, eventArgument) { var theform = document._ctl0; theform.__EVENTTARGET.value = eventTarget; theform.__EVENTARGUMENT.value = eventArgument; theform.submit(); } // --> CIS13 CIS44 CIS45 CIS46 CIS47 CIS48 CIS49 CIS50 CIS53 CIS63 CIS47 CIS50 CIS63
Sub Page_Load Dim sPath As String = Server.MapPath("\YourUserName\db\YourDB.mdb") Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0; DATA Source=" & sPath & ";" Dim objConn As OleDbConnection Dim cmdSelect As OleDbCommand Dim dtrReader As OleDbDataReader Dim strResults As String objConn = New OleDbConnection( connString ) objConn.Open() cmdSelect= New OleDbCommand( "SELECT SomeField From SomeTable", objConn) dtrReader= cmdSelect.ExecuteReader() While dtrReader.Read() strResults = strResults & dtrReader("SomeField") & " " End While lblResults.Text = strResults dtrReader.Close() objConn.Close() End Sub Suggested code from for using an Access database. “General Users can mimic the following code snipplet to connect to an MS Access database using ASP.Net (VB).”
Sub Page_Load Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0; DATA Source=" & sPath & ";" Dim objConn As OleDbConnection Dim cmdSelect As OleDbCommand Dim dtrReader As OleDbDataReader Dim strResults As String objConn = New OleDbConnection( connString ) objConn.Open() cmdSelect= New OleDbCommand( "SELECT DName From Donor2000", objConn) dtrReader= cmdSelect.ExecuteReader() While dtrReader.Read() strResults = strResults & dtrReader("DName") & " " End While lblResults.Text = strResults dtrReader.Close() objConn.Close() End Sub I uploaded donor.mdb to my site at brinkster in the db directory. This is the path. Output
Sub Page_Load Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0; DATA Source=" & sPath & ";" Dim objConn As OleDbConnection Dim cmdSelect As OleDbCommand Dim dtrReader As OleDbDataReader Dim strResults As String objConn = New OleDbConnection( connString ) objConn.Open() cmdSelect= New OleDbCommand( "SELECT DIdno, DName From Donor2000", objConn) dtrReader= cmdSelect.ExecuteReader() While dtrReader.Read() strResults = strResults & dtrReader("DIdno") & " " & dtrReader("DName") & " " End While lblResults.Text = strResults dtrReader.Close() objConn.Close() End Sub
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub
Donor2000 table in the access database donor.mdb. Results from program on previous page.
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT DIdno, DName, DYrFirst FROM Donor2000 where DYrFirst =" & "'1996'" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub
DIdno DName DYrFirst Mary Wilson Robert Brooks 1996
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT DIdno, DName, DYrFirst FROM Donor2000 order by DYrFirst" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT DName, DCity FROM Donor2000 Where DCity = " & "'Fall River'" & " or " & "Dcity = " & "'Braintree'" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub Note that I coded this all on one line and showed it on two lines only for the presentation.
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT Donor2000.DIdno, DName, DAmtCont FROM Donor2000, Donation2000 Where Donor2000.DIdno = Donation2000.DIdno" Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub The SELECT was coded on one line, I made it two lines to fit on the slide.
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT Donor2000.DIdno, DName, DAmtCont FROM Donor2000, " & _ "Donation2000 Where Donor2000.DIdno = Donation2000.DIdno" Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT Donor2000.DIdno, DName, Donation2000.DDriveNo, DDriveName, DAmtCont FROM Donor2000, Donation2000, Drive2000 Where Donor2000.DIdno = Donation2000.DIdno and Donation2000.DDriveNo = Drive2000.DDriveNo" Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub The SELECT was coded on one line, I wrapped when I pasted it on the slide.
Before Search
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() end sub
sub Get_Record(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000 where DName = '" & nameInput.Text & "'" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub Enter the Donor Name:
Prior to doing the add. After doing the Add and the the Display.
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() end sub
sub Add_Record(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "INSERT INTO Donor2000 VALUES('55555','Susan Ash', '12 Oak St', 'Braintree', 'MA', '02184','1997', 'Ann Smith')" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub Note that I coded this as one long string - I wrapped when I pasted on the slide.
sub Show_Records(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "Select * from Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub
After the add of This shows the screen after the submit has deleted the record. I then went back and ran datatest.aspx which shows all of the records is not there.
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() end sub sub Delete_Record(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";"
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "DELETE FROM Donor2000 WHERE DIdno = '55555'" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub
I filled in the textboxes, clicked add and then clicked display.
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() end sub sub Add_Record(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";"
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "INSERT INTO Donor2000 VALUES('" & idInput.Text & "', '" & nameInput.Text & "', '" & adrInput.Text & "', '" & cityInput.Text & "', '" & stateInput.Text & "', '" & zipInput.Text & "', '" & yrfstInput.Text & "', '" & contInput.Text & "')" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub sub Show_Records(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" I wrote this code as one long statement - easier I think to keep track of the quotes. I then wrapped when I pasted on the slide.
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "Select * from Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub Enter the Donor ID: Enter the Donor Name: Enter the Donor Address: Enter the Donor City: Enter the Donor State: Enter the Donor ZIP: Enter the Donor Year First: Enter the Donor Contact:
I entered and clicked Delete. Then I clicked Display The results of the display.
sub Page_Load(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() end sub
sub Delete_Record(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "DELETE FROM Donor2000 WHERE DIdno = " & "'" & idInput.Text & "'" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub
sub Show_Records(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "Select * from Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() end sub Enter the Donor ID:
I keyed in 66666
I am going to give David a middle initial of A and change his contact to Roger Brown.
Changes I keyed in.
This shows the changes.
sub Page_Load(sender as Object, e as EventArgs) if Not Page.IsPostBack then Show_Records(sender, e) end if end sub sub Get_Record(sender as Object, e as EventArgs) enterlabel.visible = false getidInput.visible = false submitbtn.visible = false 'idlabel.visible = true 'idInput.visible = true namelabel.visible = true nameInput.visible = true addrlabel.visible = true addrInput.visible = true citylabel.visible = true cityInput.visible = true statelabel.visible = true stateInput.visible = true ziplabel.visible = true zipInput.visible = true yrlabel.visible = true yrInput.visible = true contlabel.visible = true contInput.visible = true updatebtn.visible = true displaybtn.visible = true 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";"
'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000 where DIdno = '" & getidInput.Text & "'” 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) 'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader() 'Set the textbox values objDataReader.Read() idInput.text = objDataReader("DIdno") nameInput.text = objDataReader("DName") addrInput.text = objDataReader("DStAdr") cityInput.text = objDataReader("DCity") stateInput.text = objDataReader("DState") zipInput.text = objDataReader("DZip") yrInput.text =objDataReader("DYrFirst") contInput.text = objDataReader("DContact") objDataReader.Close() 'Do the DataBinding objDataReader = objCommand.ExecuteReader() dgResults.DataSource = objDataReader dgResults.DataBind() 'Close the datareader/db connection objDataReader.Close() objConnection.Close() end sub
sub Update_Record(sender as Object, e as EventArgs) 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "UPDATE donor2000 SET " & _ "DName = '" & nameInput.text & "', " & _ "DStAdr = '" & addrInput.text & "', " & _ "DCity = '" & cityInput.text & "', " & _ "DState = '" & stateInput.text & "', " & _ "DZip = '" & zipInput.text & "', " & _ "DYrFirst = '" & yrInput.text & "', " & _ "DContact = '" & contInput.text & "' " & _ "WHERE DIdno = '" & idInput.text & "'" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection) objCommand.ExecuteNonQuery objConnection.Close() Show_Records(sender, e) end sub
sub Show_Records(sender as Object, e as EventArgs) enterlabel.visible = true getidInput.text = "" getidInput.visible = true submitbtn.visible = true idlabel.visible = false idInput.visible = false namelabel.visible = false nameInput.visible = false addrlabel.visible = false addrInput.visible = false citylabel.visible = false cityInput.visible = false statelabel.visible = false stateInput.visible = false ziplabel.visible = false zipInput.visible = false yrlabel.visible = false yrInput.visible = false contlabel.visible = false contInput.visible = false updatebtn.visible = false displaybtn.visible = false 'Create a connection string Dim sPath As String = Server.MapPath("\pgrocer\db\donor.mdb") Dim connString as String connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & sPath & ";" 'Open a connection Dim objConnection as OleDbConnection objConnection = New OleDbConnection(connString) objConnection.Open() 'Specify the SQL string Dim strSQL as String = "SELECT * FROM Donor2000" 'Create a command object Dim objCommand as OleDbCommand objCommand = New OleDbCommand(strSQL, objConnection)
'Get a datareader Dim objDataReader as OleDbDataReader objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection) 'Do the DataBinding dgResults.DataSource = objDataReader dgResults.DataBind() end sub