Sunday, 14 June 2015

How to Bind DataList in asp.net ?

In design page : -


<asp:DataList ID="lstAddress" runat="server"  >
                     <HeaderTemplate>
                          <tr>
                            <th style="width: 150px; text-align:left">Name</th>
                            <th style="text-align: left;">Address</th>
                        </tr>
                     </HeaderTemplate>
                     <ItemTemplate>
                         <tr>
                             <td style="width: 150px;"><%#Eval("Name") %></td>
                             <td style="text-align: left;"><%#Eval("Address") %></td>
                         </tr>
                     </ItemTemplate>

                </asp:DataList>

In code behind page : -

lstAddress.DataSource = BLL.Registration.GetAddressDetails();
lstAddress.DataBind();


Make method for GetAddressDetails from database : -

    public class Registration
    {
        public static DataTable GetAddressDetails()
        {
            DataTable dt = new DataTable();
            try
            {
                using (SqlConnection con = new SqlConnection(Common.Global.myconstring))
                {
                    SqlDataAdapter adp = new SqlDataAdapter("GetAddressDetails", con);
                    adp.Fill(dt);
                }
            }
            catch
            {
                dt = (DataTable)null;
            }
            return dt;
        }
    }

Make table like this : -

CREATE TABLE [dbo].[Address](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Address] [nvarchar](50) NULL
)

Make procedure like this : -

CREATE PROCEDURE GetAddressDetails

AS
BEGIN
select Name,Address from Address
END

GO

Get output like this : -








No comments:

Post a Comment

Thanks for comments.