adding a new record to sql server from asp.net using vb.net

The @first, @second are references to the text boxes we take the data from to insert

Imports System.Data

Imports System.Data.SqlClient

Partial Class Testaspnet
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DBConn As New SqlConnection("Data Source=RACHID\MYRACHID1;Initial Catalog=TELE;Integrated Security=True")
        Dim DBCmd As New SqlCommand
        Dim DBAdap As New SqlDataAdapter

        Dim DS As New DataSet

        DBConn.Open()

        Try

            'Add Insert Statement
            DBCmd = New SqlCommand("INSERT INTO TELETAB1 (name,age) VALUES (@first, @second)", DBConn)
            'Add Database Parameters
            DBCmd.Parameters.Add("@first", SqlDbType.NVarChar).Value = txt1.Text
            DBCmd.Parameters.Add("@second", SqlDbType.NChar).Value = txt2.Text
            DBCmd.ExecuteNonQuery()

            Response.Write("Your Record is Updated ")

            'Set the value of DataAdapter

            DBAdap = New SqlDataAdapter("SELECT * FROM TELETAB1", DBConn)

            'Fill the DataSet

            DBAdap.Fill(DS)

            'Bind with GridView control and Display the Record

            GridView1.DataSource = DS

            GridView1.DataBind()

        Catch exp As Exception

            Response.Write(exp)

        End Try

        DBCmd.Dispose()

        DBAdap.Dispose()

        DBConn.Close()

        DBConn = Nothing
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
     
    End Sub
End Class

No comments:

Post a Comment