CSharp Updating Database – ADO.Net Database

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %><html>
  <head>
    <title>Updating a database</title>
  </head>
  <body>
    <form method="post" runat="server">
      <asp:Label id="txtFirstName" runat="server" /><br/><br/>
      <asp:TextBox id="txtLastName" runat="server" /><br/><br/>
      <asp:Button id="btnChange" runat="server"
                  OnClick="btnChange_Click" Text="Change" />
    </form>
  </body>
</html><script language="VB" runat="server">
Dim strConnection As String = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=w3mentoruser;Password='password';"
Dim objConnection As SqlConnectionSub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
  If Not IsPostBack Then
    LoadProduct()
  End If
End SubSub LoadProduct()
  Connect()
  Dim strQuery As String = "SELECT FirstName, LastName " & _
                           "FROM Employee"  Dim dbComm As New SqlCommand(strQuery, objConnection)
  Dim reader As SqlDataReader = dbComm.ExecuteReader()  reader.Read()
  txtFirstName.Text = reader.GetString(0)
  txtLastName.Text = reader.GetString(0)  reader.Close()
  Disconnect()
End SubSub btnChange_Click(ByVal Sender As Object, ByVal E As EventArgs)
  UpdateProduct()
  LoadProduct()
End SubPrivate Sub UpdateProduct()
  Dim strSQL As String = "UPDATE Employee" & _
                         " SET FirstName = " & txtLastName.Text & _
                         " WHERE ID = 1"
  Connect()
  Dim dbComm As New SqlCommand(strSQL, objConnection)
  dbComm.ExecuteNonQuery()
  Disconnect()
End SubPrivate Sub Connect()
  If objConnection Is Nothing Then
    objConnection = New SqlConnection(strConnection)
  End If  If objConnection.State = ConnectionState.Closed Then
    objConnection.Open()
  End If
End SubPrivate Sub Disconnect()
  objConnection.Close()
End Sub
</script>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.