Friday, 14 October 2016

5. Design a form and Save data to the Login Table

Hi,
This is very important, that using this option we can design a form and the data can be save to database without going to the Database view.

At first add a web form , come to the design view and drag and HTML table



After that type Name and Password in the first column and then adjust the column width


Adjusting column width


The add Button and Text box
from Tool Box

Adding Button

Change ID of name and password text box


See the properties box in the right side for changing ID


give name as txtName and password as txtPassword


Double click the button add code


The place to copy the code












Copy the code in Blue to your button click function. The Red code change according to your folder path and table name.

Imports System.Data
Imports System.Data.SqlClient
Partial Class Default4
    Inherits System.Web.UI.Page
    Public constr As String
    Public ds As New DataSet()
    Public da As SqlClient.SqlDataAdapter
    Public mySqlConn1 As New SqlClient.SqlConnection(constr)
    Public sql, sql1, code, asd, type, cartno As String
   

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim conn As New SqlConnection
        Dim constr As String
        constr = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project1\App_Data\Database.mdf;Integrated Security=true;User Instance=true"

        conn.ConnectionString = constr
        conn.Open()
        sql = "insert into table1 values('" & txtName.Text & "','" & txtPassword.Text & "')"

        Dim comm As New SqlCommand(sql, conn)



        Dim i As Integer
        i = comm.ExecuteNonQuery()


        MsgBox("Data saved")

        conn.Close()
    End Sub
End Class

No comments:

Post a Comment