See this

Sujit Patil replied to Arvind M at 05-Jul-08 02:16
First, you will need to import the System.Data.SqlClient name space at the top of your page.

Imports System.Data.SqlClient

Next, we write the actual code to connect to SQL Server and insert the data. We apply the below commands to the Click Event of a button control. Notice that we are getting our values from the .Text property of the txtFirstName TextBox and the txtLastName TextBox.

Dim cmd As New SqlCommand("INSERT INTO Customers (FirstName, LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')", New SqlConnection(strConn))

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()

After we add the data above, we want to display it on the page along with all past records added. This is done by calling a Method that will bind the data to a repeater control.

Dim cmd As New SqlCommand("Select * From Customers", New SqlConnection(strConn))

cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()

The full listing for the code behind page is as follows

Imports System.Data.SqlClient

Public Class index
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents btnInsert As System.Web.UI.WebControls.Button
Protected WithEvents txtFirstName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtLastName As System.Web.UI.WebControls.TextBox
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx; Password=xxxxxx; Persist Security Info=True;packet size=4096"

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData()
End Sub

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Dim strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx; Password=xxxxxx; Persist Security Info=True;packet size=4096"
Dim cmd As New SqlCommand("INSERT INTO Customers (FirstName, LastName)VALUES('" & txtFirstName.Text & "','" & txtLastName.Text & "')", New SqlConnection(strConn))
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
LoadData()
End Sub

Sub LoadData()
Dim strConn As String = "data source=xx.xx.xxx.xxx; User ID=xxxxxx; Password=xxxxxx; Persist Security Info=True;packet size=4096"
Dim strSQL As String = "Select * From Customers"
Dim cmd As New SqlCommand(strSQL, New SqlConnection(strConn))
cmd.Connection.Open()
Repeater1.DataSource = cmd.ExecuteReader
Repeater1.DataBind()
cmd.Connection.Close()
cmd.Connection.Dispose()
End Sub

End Class


Go thr these links also;
http://www.easerve.com/developer/tutorials/asp-net-tutorials-adding-records-database.aspx

http://www.codeproject.com/KB/database/simpledbreadwrite.aspx

http://msdn.microsoft.com/en-us/library/ms971485.aspx


Best Luck!!!!!!!!!!!!!!!!!!!!
Sujit.


Click here to sign in and reply. You could earn money via our $500 contest just for being helpful.
  insert new record in database - Arvind M  05-Jul-08 01:59 1:59:08 AM
      See this - Sujit Patil  05-Jul-08 02:16 2:16:18 AM
          re:See this - Arvind M  05-Jul-08 02:24 2:24:57 AM
              Try this - sri sri  05-Jul-08 02:28 2:28:56 AM
              Use LOOP - Sujit Patil  05-Jul-08 03:17 3:17:24 AM
      Insert new record in database - Sanjay Verma  05-Jul-08 02:44 2:44:25 AM
      solution to insert new record in database - Umapathy Kaliaperumal  05-Jul-08 02:49 2:49:26 AM
      Reply - alice johnson  05-Jul-08 03:20 3:20:32 AM
      use Bulkcopy - Vasanthakumar D  05-Jul-08 04:18 4:18:41 AM
      u can do this using xml - Arvind Kumar  07-Jul-08 01:20 1:20:01 AM
View Posts