In this article I am going to explain How to insert data to table using button click event with VB
First you have to create a new web project as we discussed earlier article. How to create new project with visual studio.
Watch Video
I am going to user how to bind data with dropdownlist for this project also.
First we have to design screen layout, Text box and Button controller as the previous projects, you have to drag and drop controllers from toolbox to designing form.
Next you can change the ID from properties and double click on the Button controller
then button Click even coding screen will display.
You have to type the coding as displayed below.
Import System.Data.SqlClient
Coding for Button Click event
Imports System.DataImports System.Data.SqlClient
Imports Class1
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub brnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles brnSave.Click
Dim myConnection As SqlConnection = New SqlConnection(GetConnectionString)
Dim myCommand As SqlCommand
myConnection.Open()
myCommand = New SqlCommand("INSERT INTO Banks ( b_name) VALUES ('" & txtBankName.Text.Trim & "')", myConnection)
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
DropDownList1.DataBind()
End Sub
End Class
Make the connection String using Class1.vb
Imports Microsoft.VisualBasicImports System.Configuration
Public Class Class1
Public Shared Function GetConnectionString() As String
GetConnectionString = ConfigurationManager.ConnectionStrings("SUPERMARKETConnectionString").ConnectionString
Return GetConnectionString
End Function
End Class
Connection String getting form Web.config
<connectionStrings><add name="SUPERMARKETConnectionString" connectionString="Data Source=SERVER;Initial Catalog=SUPERMARKET;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Finally run the application and you can input name for text box and press Save button.
Then bank name with save to SQL table.
For details Watch Video
Related articles
0 comments:
Post a Comment