Imports MySql.Data.MySqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Dim Conn As MySqlConnection = New MySqlConnection
Dim da As MySqlDataAdapter
Dim dt As New DataTable
Dim ds As DataSet = New DataSet()
Dim pop As New MySqlCommand
Dim add_state As Boolean = False
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnstring As String = "Server=localhost; User Id=root; Password=1; database= database_kku_dev; Pooling=false"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConnstring
.Open()
End With
Load1()
End Sub
Sub Load1()
Dim sqlland As String = ""
sqlland = "SELECT * FROM bank "
'sqlland &= " ORDER BY LANDID"
da = New MySqlDataAdapter(sqlland, Conn)
da.Fill(dt)
If dt.Rows.Count > 0 Then
Me.txtaccount.Text = dt.Rows(0)("account_name")
Me.txtBlank.Text = dt.Rows(0)("bank_name")
Me.txtNumber.Text = dt.Rows(0)("account_no")
Me.txtBranch.Text = dt.Rows(0)("bank_branch")
Me.bank_id.Text = dt.Rows(0)("bank_id")
End If
gridview()
End Sub
Sub gridview()
Dim objCmd As MySqlCommand
Dim strConnstring As String = "Server=localhost; User Id=root; Password=1; database= database_kku_dev; Pooling=false"
Dim sqlland As String = ""
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConnstring
.Open()
End With
Dim strSQL As String
strSQL = "SELECT * FROM bank"
Dim da As MySqlDataReader
objCmd = New MySqlCommand(strSQL, Conn)
da = objCmd.ExecuteReader()
'*** BindData to GridView ***'
GridView1.DataSource = da
GridView1.DataBind()
da.Close()
da = Nothing
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
txtaccount.Text = ""
txtBlank.Text = ""
txtNumber.Text = ""
txtBranch.Text = ""
add_state = True
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Save()
End Sub
Sub Save()
Dim strConnString As String = "Server=localhost; User Id=root; Password=1; database= database_kku_dev; Pooling=false"
Dim sqlupdate As String
Dim sqladd As String
sqlupdate = " update bank"
sqlupdate &= " set account_name = '" & txtaccount.Text & "',"
sqlupdate &= " account_no= '" & txtNumber.Text & "',"
sqlupdate &= " bank_name= '" & txtBlank.Text & "',"
sqlupdate &= " bank_branch = '" & txtBranch.Text & "'"
sqlupdate &= " where bank_id = '" & bank_id.Text & "'"
sqladd = "insert into bank (account_name,account_no,"
sqladd &= "bank_name,bank_branch)"
sqladd &= " values ('" & txtaccount.Text & "',"
sqladd &= "'" & txtNumber.Text & "',"
sqladd &= "'" & txtBlank.Text & " ',"
sqladd &= "'" & txtBranch.Text & "')"
Try
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConnString
.Open()
End With
pop = New MySqlCommand
With pop
.CommandType = CommandType.Text
If add_state = True Then
.CommandText = sqladd
Else
.CommandText = sqlupdate
End If
.Connection = Conn
.ExecuteNonQuery()
End With
btnAdd.Visible = False
'pop.ExecuteNonQuery()
lblStatus.Visible = True
Catch ex As Exception
lblStatus.Text = "Record can not update"
End Try
lblStatus.Text = "Record Updated"
Page_UnLoad()
End Sub
Sub Page_UnLoad()
Conn.Close()
Conn = Nothing
End Sub
End Class
Tag : ASP.NET MySQL, Web (ASP.NET), VB.NET, VS 2010 (.NET 4.x)