Use vb.net to do a WinForm that configures Web.config functionality

Source: Internet
Author: User
Tags config count modify
The Web interface is simple, with five controls Txtservername, Cbodatabase, txtUserName, Txtpassword, cbwindowsauthentication

The following is the vb.net background code:

Imports System
Imports System.Diagnostics
Imports System.Configuration
Imports System.Xml
Imports System.Text
Imports System.Reflection

Public Class Frmmain
??? Inherits System.Windows.Forms.Form

#Region "code generated by the Windows forms Designer"

??? Public Sub New ()
??????? MyBase.New ()

??????? ' This call is required by the Windows Forms Designer.
??????? InitializeComponent ()

??????? ' Add any initialization after the InitializeComponent () call

??? End Sub

??? ' Form overrides dispose to clean up the list of components.
??? Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
??????? If disposing Then
??????????? If not (components are nothing) Then
??????????????? Components. Dispose ()
??????????? End If
??????? End If
??????? Mybase.dispose (disposing)
??? End Sub

??? ' Required by the Windows Forms Designer
??? Private Components as System.ComponentModel.IContainer

??? ' NOTE: The following procedure is required by the Windows Forms Designer
??? ' You can use the Windows Forms Designer to modify this procedure.
??? ' Do not modify it using the Code Editor.
??? Friend WithEvents GroupBox1 as System.Windows.Forms.GroupBox
??? Friend WithEvents Txtservername as System.Windows.Forms.TextBox
??? Friend WithEvents Cbwindowsauthentication as System.Windows.Forms.CheckBox
??? Friend WithEvents Grplogin as System.Windows.Forms.GroupBox
??? Friend WithEvents Lblpassword as System.Windows.Forms.Label
??? Friend WithEvents Lblusername as System.Windows.Forms.Label
??? Friend WithEvents Txtpassword as System.Windows.Forms.TextBox
??? Friend WithEvents txtUserName as System.Windows.Forms.TextBox
??? Friend WithEvents Grbdatabase as System.Windows.Forms.GroupBox
??? Friend WithEvents Cbodatabase as System.Windows.Forms.ComboBox
??? Friend WithEvents Btncancel as System.Windows.Forms.Button
??? Friend WithEvents Btnok as System.Windows.Forms.Button
??? Friend WithEvents Lblserver as System.Windows.Forms.Label
??? Private Sub InitializeComponent ()
??????? Me.groupbox1 = New System.Windows.Forms.GroupBox
??????? Me.txtservername = New System.Windows.Forms.TextBox
??????? Me.cbwindowsauthentication = New System.Windows.Forms.CheckBox
??????? Me.grplogin = New System.Windows.Forms.GroupBox
??????? Me.lblpassword = New System.Windows.Forms.Label
??????? Me.lblusername = New System.Windows.Forms.Label
??????? Me.txtpassword = New System.Windows.Forms.TextBox
??????? Me.txtusername = New System.Windows.Forms.TextBox
??????? Me.grbdatabase = New System.Windows.Forms.GroupBox
??????? Me.cbodatabase = New System.Windows.Forms.ComboBox
??????? Me.btncancel = New System.Windows.Forms.Button
??????? Me.btnok = New System.Windows.Forms.Button
??????? Me.lblserver = New System.Windows.Forms.Label
??????? Me.GroupBox1.SuspendLayout ()
??????? Me.grpLogin.SuspendLayout ()
??????? Me.grbDatabase.SuspendLayout ()
??????? Me.suspendlayout ()
??????? '
??????? ' GroupBox1
??????? '
??????? ME.GROUPBOX1.CONTROLS.ADD (Me.txtservername)
??????? ME.GROUPBOX1.CONTROLS.ADD (me.cbwindowsauthentication)
??????? ME.GROUPBOX1.CONTROLS.ADD (Me.grplogin)
??????? ME.GROUPBOX1.CONTROLS.ADD (Me.grbdatabase)
??????? ME.GROUPBOX1.CONTROLS.ADD (Me.btncancel)
??????? ME.GROUPBOX1.CONTROLS.ADD (Me.btnok)
??????? ME.GROUPBOX1.CONTROLS.ADD (Me.lblserver)
??????? Me.GroupBox1.Location = New System.Drawing.Point (8, 8)
??????? Me.GroupBox1.Name = "GroupBox1"
??????? Me.GroupBox1.Size = New System.Drawing.Size (321, 237)
??????? Me.GroupBox1.TabIndex = 15
??????? Me.GroupBox1.TabStop = False
??????? Me.GroupBox1.Text = "Database Settings"
??????? '
??????? ' Txtservername
??????? '
??????? Me.txtServerName.Location = New System.Drawing.Point (115, 30)
??????? Me.txtServerName.Name = "Txtservername"
??????? Me.txtServerName.Size = New System.Drawing.Size (178, 21)
??????? Me.txtServerName.TabIndex = 10
??????? Me.txtServerName.Text = ""
??????? '
??????? ' Cbwindowsauthentication
??????? '
??????? Me.cbWindowsAuthentication.Location = New System.Drawing.Point (38, 60)
??????? Me.cbWindowsAuthentication.Name = "Cbwindowsauthentication"
??????? Me.cbWindowsAuthentication.Size = New System.Drawing.Size (173, 22)
??????? Me.cbWindowsAuthentication.TabIndex = 1
??????? Me.cbWindowsAuthentication.Text = "Use Windows Authentication"
??????? '
??????? ' Grplogin
??????? '
??????? ME.GRPLOGIN.CONTROLS.ADD (Me.lblpassword)
??????? ME.GRPLOGIN.CONTROLS.ADD (Me.lblusername)
??????? ME.GRPLOGIN.CONTROLS.ADD (Me.txtpassword)
??????? ME.GRPLOGIN.CONTROLS.ADD (Me.txtusername)
??????? Me.grpLogin.Location = New System.Drawing.Point (29, 65)
??????? Me.grpLogin.Name = "Grplogin"
??????? Me.grpLogin.Size = New System.Drawing.Size (269, 73)
??????? Me.grpLogin.TabIndex = 2
??????? Me.grpLogin.TabStop = False
??????? '
??????? ' Lblpassword
??????? '
??????? Me.lblPassword.Location = New System.Drawing.Point (10, 43)
??????? Me.lblPassword.Name = "Lblpassword"
??????? Me.lblPassword.Size = New System.Drawing.Size (76, 22)
??????? Me.lblPassword.TabIndex = 13
??????? Me.lblPassword.Text = "Password"
??????? Me.lblPassword.TextAlign = System.Drawing.ContentAlignment.MiddleRight
??????? '
??????? ' Lblusername
??????? '
??????? Me.lblUserName.Location = New System.Drawing.Point (10, 17)
??????? Me.lblUserName.Name = "Lblusername"
??????? Me.lblUserName.Size = New System.Drawing.Size (76, 26)
??????? Me.lblUserName.TabIndex = 14
??????? Me.lblUserName.Text = "User"
??????? Me.lblUserName.TextAlign = System.Drawing.ContentAlignment.MiddleRight
??????? '
??????? ' Txtpassword
??????? '
??????? Me.txtPassword.Location = New System.Drawing.Point (86, 43)
??????? Me.txtPassword.Name = "Txtpassword"
??????? Me.txtPassword.PasswordChar = Microsoft.VisualBasic.ChrW (42)
??????? Me.txtPassword.Size = New System.Drawing.Size (173, 21)
??????? Me.txtPassword.TabIndex = 4
??????? Me.txtPassword.Text = ""
??????? '
??????? ' txtUserName
??????? '
??????? Me.txtUserName.Location = New System.Drawing.Point (86, 17)
??????? Me.txtUserName.Name = "txtUserName"
??????? Me.txtUserName.Size = New System.Drawing.Size (173, 21)
??????? Me.txtUserName.TabIndex = 3
??????? Me.txtUserName.Text = ""
??????? '
??????? ' Grbdatabase
??????? '
??????? ME.GRBDATABASE.CONTROLS.ADD (Me.cbodatabase)
??????? Me.grbDatabase.Location = New System.Drawing.Point (29, 142)
??????? Me.grbDatabase.Name = "Grbdatabase"
??????? Me.grbDatabase.Size = New System.Drawing.Size (269, 52)
??????? Me.grbDatabase.TabIndex = 5
??????? Me.grbDatabase.TabStop = False
??????? Me.grbDatabase.Text = "Database"
??????? '
??????? ' Cbodatabase
??????? '
??????? Me.cboDatabase.ItemHeight = 12
??????? Me.cboDatabase.Location = New System.Drawing.Point (14, 22)
??????? Me.cboDatabase.Name = "Cbodatabase"
??????? Me.cboDatabase.Size = New System.Drawing.Size (240, 20)
??????? Me.cboDatabase.TabIndex = 6
??????? '
??????? ' Btncancel
??????? '
??????? Me.btnCancel.Location = New System.Drawing.Point (176, 202)
??????? Me.btnCancel.Name = "Btncancel"
??????? Me.btnCancel.Size = New System.Drawing.Size (90, 25)
??????? Me.btnCancel.TabIndex = 8
??????? Me.btnCancel.Text = "&c Cancel"
??????? '
??????? ' Btnok
??????? '
??????? Me.btnOk.Location = New System.Drawing.Point (72, 202)
??????? Me.btnOk.Name = "Btnok"
??????? Me.btnOk.Size = New System.Drawing.Size (90, 25)
??????? Me.btnOk.TabIndex = 7
??????? Me.btnOk.Text = "&o OK"
??????? '
??????? ' Lblserver
??????? '
??????? Me.lblServer.Location = New System.Drawing.Point (29, 30)
??????? Me.lblServer.Name = "Lblserver"
??????? Me.lblServer.Size = New System.Drawing.Size (77, 26)
??????? Me.lblServer.TabIndex = 9
??????? Me.lblServer.Text = "Server"
??????? Me.lblServer.TextAlign = System.Drawing.ContentAlignment.MiddleRight
??????? '
??????? ' Frmmain
??????? '
??????? Me.autoscalebasesize = New System.Drawing.Size (6, 14)
??????? Me.clientsize = New System.Drawing.Size (336, 253)
??????? ME.CONTROLS.ADD (Me.groupbox1)
??????? Me.Name = "Frmmain"
??????? Me.GroupBox1.ResumeLayout (False)
??????? Me.grpLogin.ResumeLayout (False)
??????? Me.grbDatabase.ResumeLayout (False)
??????? Me.resumelayout (False)

??? End Sub

#End Region

?

??? Private Sub frmmain_load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
??????? Dim XmlDoc as XmlDocument
??????? Dim TOPM as XmlNodeList
??????? xmldoc = New XmlDocument
??????? Xmldoc.load ("Web.config")
??????? TOPM = XmlDoc.DocumentElement.ChildNodes
??????? For each element as XmlElement in TOPM
??????????? If element. Name.tolower () = "appsettings" Then
??????????????? Dim _node as XmlNodeList = element. ChildNodes
??????????????? If _node. Count > 0 Then
??????????????????? For each El as XmlElement in _node
??????????????????????? If el. Attributes ("key"). INNERXML = "ServerName" Then _
??????????????????????????? Txtservername.text = el. Attributes ("value"). Value
??????????????????????? If el. Attributes ("key"). INNERXML = "DataBaseName" Then _
??????????????????????????? Cbodatabase.text = el. Attributes ("value"). Value
??????????????????????? If el. Attributes ("key"). INNERXML = "UserName" Then _
???????????????????????????? txtUsername.Text = Securityservice.symmetricdecrypt (el. Attributes ("value"). Value)
??????????????????????? If el. Attributes ("key"). INNERXML = "Password" Then _
??????????????????????????? txtPassword.Text = Securityservice.symmetricdecrypt (el. Attributes ("value"). Value)
??????????????????????? If el. Attributes ("key"). INNERXML = "WindowsAuthentication" Then _
?????????????????????????? Me.cbWindowsAuthentication.Checked = Convert.toboolean (el. Attributes ("value"). Value)
??????????????????? Next
??????????????? End If

??????????? End If
??????? Next

??? End Sub

??? '**********************************************************************************************
??? ' * * Class:????????? Getdatabases
??? '**
??? ' * * Description:???
??? '**********************************************************************************************
??? Private Sub getdatabases ()

??????? Try

??????????? Dim oDataBase as New Database
??????????? Cbodatabase.beginupdate ()
??????????? Cursor.current = Cursors.waitcursor

??????????? Configuration.server = Txtservername.text
??????????? Configuration.database = "Master" ' Cbodatabase.text
??????????? Configuration.password = txtPassword.Text
??????????? Configuration.username = txtUsername.Text
??????????? Configuration.windowsauthentication = cbwindowsauthentication.checked


??????????? Dim Rd as Sqlclient.sqldatareader
??????????? RD = Odatabase.getdatabases
??????????? While Rd. Read ()
??????????????? CboDatabase.Items.Add (Rd. GetString (1))
??????????? End While
??????????? Rd. Close ()
??????????? cbodatabase.sorted = True


??????? Catch Acomexception as System.Runtime.InteropServices.COMException

??????????? Cbodatabase.datasource = Nothing
??????????? CboDatabase.Items.Clear ()
??????????? Cbodatabase.text = ""

??????????? MsgBox ("Could not connect to" & Txtservername.text & ".", MsgBoxStyle.Exclamation, _
??????????????? "SQL Connection Error")
??????? Catch E1 as Exception
??????????? MsgBox (E1. Message)

??????? Finally

??????????? Cbodatabase.endupdate ()
??????????? Cursor.current = Cursors.Default

??????? End Try

??? End Sub

??? Private Sub Btnok_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnok.click
??????? Dim XmlDoc as XmlDocument
??????? Dim TOPM as XmlNodeList
??????? xmldoc = New XmlDocument
??????? Xmldoc.load ("Web.config")
??????? TOPM = XmlDoc.DocumentElement.ChildNodes
??????? For each element as XmlElement in TOPM
??????????? If element. Name.tolower () = "appsettings" Then
??????????????? Dim _node as XmlNodeList = element. ChildNodes
??????????????? If _node. Count > 0 Then
??????????????????? For each El as XmlElement in _node
??????????????????????? If el. Attributes ("key"). INNERXML = "ServerName" Then _
?????????????????????????? El. Attributes ("value"). Value = Txtservername.text
??????????????????????? If el. Attributes ("key"). INNERXML = "DataBaseName" Then _
?????????????????????????? El. Attributes ("value"). Value = Cbodatabase.text
??????????????????????? If el. Attributes ("key"). INNERXML = "UserName" Then _
?????????????????????????? El. Attributes ("value"). Value = Securityservice.symmetricencrypt (txtUsername.Text)
??????????????????????? If el. Attributes ("key"). INNERXML = "Password" Then _
?????????????????????????? El. Attributes ("value"). Value = Securityservice.symmetricencrypt (txtPassword.Text)
??????????????????????? If el. Attributes ("key"). INNERXML = "WindowsAuthentication" Then _
????????????????????????? El. Attributes ("value"). Value = Me.cbWindowsAuthentication.Checked.ToString ()
??????????????????? Next
??????????????? End If

??????????? End If
??????? Next

??????? ' Write XML to file
??????? Xmldoc.save ("Web.config")
??????? Me.close ()

??? End Sub

??? Private Sub cbwindowsauthentication_checkedchanged (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles CB Windowsauthentication.checkedchanged

??????? If cbwindowsauthentication.checked Then
??????????? txtUsername.Text = ""
??????????? txtPassword.Text = ""
??????????? txtusername.enabled = False
??????????? txtpassword.enabled = False
??????? Else
??????????? txtusername.enabled = True
??????????? txtpassword.enabled = True
??????? End If

??????? Configuration.windowsauthentication = cbwindowsauthentication.checked
??? End Sub

??? Private Sub Btncancel_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btncancel.click
??????? If MsgBox ("Are you sure your want to exit w/o configuring the database?", msgboxstyle.okcancel) = Msgboxresult.ok Then
??????????? Me.close ()
??????? End If
??? End Sub
??? Private Sub Txtpassword_lostfocus (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles TXTPASSWORD.LOSTFOC us
??????? If cbwindowsauthentication.checked Or (txtUsername.Text <> "" and txtPassword.Text <> "") Then
??????????? Getdatabases ()
??????? End If
??? End Sub
End Class


The Securityservice class is used to add/decrypt, so that users cannot see the database user and password directly from the web.config.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.