PatelNehal4u
Active member
- Joined
- Apr 6, 2009
- Messages
- 25
- Programming Experience
- 1-3
I had written a code for data insertion in asp.net form. Code work fine but Please correct me if i am wrong somewhere.
VB.NET:
Imports System.Data
Imports System.Data.OleDb
Partial Class InsertPage
Inherits System.Web.UI.Page
Dim connectionstring As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & Server.MapPath("CustomerDB.mdb")
Dim con As OleDbConnection
Dim cmd As OleDbCommand
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
'User Generated Functions and routines
Public Sub InsertData()
Try
con.Open()
da.InsertCommand = New OleDbCommand("INSERT INTO Customer(CustomerName,City) VALUES(@CustomerName,@City)")
da.InsertCommand.Parameters.Add("@CustomerName", OleDbType.VarChar, 40, "CustomerName")
da.InsertCommand.Parameters("@CustomerName").Value = txtName.Text
da.InsertCommand.Parameters.Add("@City", OleDbType.VarChar, 40, "City")
da.InsertCommand.Parameters("@City").Value = txtCity.Text
da.InsertCommand.Connection = con
Dim nrow As DataRow
nrow = ds.Tables(0).NewRow
nrow("CustomerName") = txtName.Text
nrow("City") = txtCity.Text
ds.Tables(0).Rows.Add(nrow)
da.Update(ds)
ds.AcceptChanges()
lblResult.Text = "Record added sucessfully"
Catch ex As Exception
Response.Write(ex.ToString)
Finally
con.Close()
End Try
End Sub
Private Sub LoadData()
Try
con = New OleDbConnection(connectionstring)
con.Open()
da.SelectCommand = New OleDbCommand("SELECT * FROM Customer", con)
da.Fill(ds)
Catch ex As Exception
Response.Write(ex.ToString)
Finally
con.Close()
End Try
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Me.InsertData()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.LoadData()
End Sub