Monday, May 6, 2013

How to Bind Listview using Sqldataadapter, Datatable and Query in Asp.net(C#, VB)


Introduction: In this post I will tell you how to bind the Listview data control in Asp.net.

Description:
I have created a table name STUDENT_DETAIL.
STUDENT_ID
int
STUDENT_NAME
varchar(50)
STUDENT_ADDRESS
varchar(50)
STUDENT_CLASS
varchar(50)

STUDENT_ID is primary key.
Now open the Visual Studio>Go to File>New>Website. Add the Connectionstring in web.config file of website.
<configuration>
       <connectionStrings>
    <add name="connection" connectionString="Data Source=SYS-1F78031ED0A;Initial Catalog=TestBlog;Integrated Security=True"/>
       </connectionStrings>
       <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
</configuration>


After that add new web form to website, drag and drop the Listview Data control from Toolbox.
<asp:ListView ID="liststudent" runat="server" DataKeyNames="STUDENT_ID">
                 <LayoutTemplate>
     <table style="margin:0px auto;border:1px solid #c1c1c1;">
  <tr style="background-color:#E5E5FE;font-weight:bold">
   <td align="left"><asp:Label ID="lblstudent" runat="server">Student Name</asp:Label></td>
   <td align="left"><asp:Label ID="lblstudentaddress" runat="server">Student Address</asp:Label></td>
   <td align="left"><asp:Label ID="lblstudentclass" runat="server">Student Class</asp:Label></td>
</tr>
    <tr id="itemPlaceholder" runat="server"></tr>
</LayoutTemplate>
        <ItemTemplate>
        <tr style="background-color: #E0FFFF; color: #333333;font-style:italic;text-align:center;">
       <td> <asp:Label ID="lblstudentname" runat="server" Text='<%#Eval("STUDENT_NAME")%>'></asp:Label></td>
         <td><asp:Label ID="lbladdress" runat="server" Text='<%#Eval("STUDENT_ADDRESS") %>'></asp:Label></td>
          <td><asp:Label ID="lblclass" runat="server" Text='<%#Eval("STUDENT_CLASS") %>'></asp:Label></td></tr>
          </ItemTemplate>
          <EmptyDataTemplate>
         <table border="1">
             <tr style="color:Red;"><td align="center">
                 <asp:Label ID="lblmessage" runat="server" Text="No Data Found"></asp:Label></td></tr></table>
          </EmptyDataTemplate>
             </asp:ListView>

After that go to .aspx.cs page and add namespace.

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Create a function to bind Listview.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        if (!IsPostBack)
        {
            bindListview();
        }
    }
    private void bindListview()
    {
        try
        {
            SqlDataAdapter adp = new SqlDataAdapter("select * from STUDENT_DETAIL", con);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                liststudent.DataSource = dt;
                liststudent.DataBind();
            }
            else
            {
                liststudent.DataSource = null;
                Response.Write("No Records Available");
            }
        }
        catch (Exception ex)
        {
        }

    }

In VB
Go to .aspx.vb page and add namespace.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Private con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If
        If Not IsPostBack Then
            bindListview()
        End If
    End Sub
    Private Sub bindListview()
        Try
            Dim adp As New SqlDataAdapter("select * from STUDENT_DETAIL", con)
            Dim dt As New DataTable()
            adp.Fill(dt)
            If dt.Rows.Count > 0 Then
                liststudent.DataSource = dt
                liststudent.DataBind()
            Else
                liststudent.DataSource = Nothing
                Response.Write("No Records Available")
            End If
        Catch ex As Exception
        End Try


Now debug the project and check the result.

Is it helpful?

If yes post your comment to admire my work. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment