Written some cool source code? Upload it to Programmer's Heaven.
*/

Other Views

corner
*/

How to Insert Data Entered in a Textbox into the Database

How do I insert data entered in a textbox into the database?

The data you enter in the textboxes will be inserted into the database when of the click of a Button.

The working senario is a database called "Emp" with a table named "Table1" with three columns. Also a Form with three TextBoxes and one Command Button.

Imports System.Data.OleDb
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim icount As Integer
Dim str As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles_ Button2.Click
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;Persist Security_ Info=False")
cn.Open()
str = "insert into table1 values(" & CInt(TextBox1.Text) & ",'" & TextBox2.Text & "','" & TextBox3.Text & "')" 'string stores the command
'and CInt is used to convert number, to string
cmd = New OleDbCommand(str, cn)
icount = cmd.ExecuteNonQuery
MessageBox.Show(icount) 'displays number of records inserted
Catch
End Try
End Sub


Written by Sandeep Mogulla, Webmaster at www.startvbdotnet.com

Index
corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.
Resource Listings