Show Grid View in ASP. NET using VB.NET

In this article we will learn to show gridview in asp.net.
  • 2016
 

Show Grid View in VB.Net

  1. Create a new website in visual studio 2005/2008.
     
  2. Select a file location and file name and language.
     
  3. Create some text boxes with name in web form.

    Example:

    _img1.jpg

    Now drag a grid view in web form and click the edit column property  show in figure and add the item inside the grid view .You can also change it format by using property Auto Format.

    _img2.jpg

    Now choose the data source and click on ok button.

    _img3.jpg

    Now click on New Connection and then click on

    _img4.jpg

    Choose either Window or server authentication, server name and attach the database. Click on ok button

    Now Click next

    _img5.jpg

    _img6.jpg

    Further Click next

    _img7.jpg

    Click next and test Query and click finish

    _img8.jpg

    Now use the Defalut.aspx.vb page and add the following code

    Imports System.Data.SqlClient

    Partial Class _Default

        Inherits System.Web.UI.Page
        Dim con As SqlConnection
        Dim cmd As SqlCommand
        Dim str As String
        Dim ad As SqlDataAdapter
        Dim dr As SqlDataReader

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

            con = New SqlConnection("Data Source =MCNDESKTOP06; Initial catalog = empinfo ; User ID = sa;Password = wintellect")
            con.Open()
            Response.Write("connect")

            str = (("select * from emp_info where name ='" + TextBox1.Text & "' ,address = '") + TextBox2.Text & "',email_id ='") + TextBox3.Text & "'"
            ad = New SqlDataAdapter(str, con)
            ad.Fill(ds, "emp_info")
            GridView1.DataSource = ds.tables(emp_info).defaultview
            GridView1.DataMember = "emp_info"
            GridView1.DataBind()

        End Sub

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.