<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dot Net Thoughts &#187; Databinding</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/databinding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Implementing Custom Paging in DataRepeater using C# and SQL Server</title>
		<link>http://www.dotnetthoughts.net/2009/10/22/implementing-custom-paging-in-datarepeater-using-c-and-sql-server/</link>
		<comments>http://www.dotnetthoughts.net/2009/10/22/implementing-custom-paging-in-datarepeater-using-c-and-sql-server/#comments</comments>
		<pubDate>Thu, 22 Oct 2009 14:40:23 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Databinding]]></category>
		<category><![CDATA[Repeater]]></category>

		<guid isPermaLink="false">http://www.dotnetthoughts.net/?p=558</guid>
		<description><![CDATA[Normally DataRepeater doesn&#8217;t have a paging feature; and you can implement it using LINQ, if you are using .Net 3.5. Here is an implementation which is using SQL Server 2005 feature ROW_NUMBER(). You can get more information about ROW_NUMBER() from MSDN. Stored Procedure &#8211; xsp_GetPersons CREATE PROCEDURE xsp_GetPersons @StartIndex INT, @Count INT AS BEGIN DECLARE [...]]]></description>
			<content:encoded><![CDATA[<p>Normally DataRepeater doesn&#8217;t have a paging feature; and you can implement it using LINQ, if you are using .Net 3.5. Here is an implementation which is using SQL Server 2005 feature ROW_NUMBER(). You can get more information about ROW_NUMBER() from <a href="http://msdn.microsoft.com/en-us/library/ms186734.aspx" target="_blank">MSDN</a>.</p>
<p>Stored Procedure &#8211; xsp_GetPersons</p>
<pre class="brush: sql;">
CREATE PROCEDURE xsp_GetPersons
@StartIndex INT, @Count INT
AS
BEGIN
DECLARE @EndIndex INT
SET @StartIndex = @StartIndex * @Count
SET @EndIndex = @StartIndex + @Count
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY UserId DESC) AS SlNo, UserId, FirstName, LastName, Email, ISNULL(DOB, GETDATE()) AS DOB FROM U_MyRegistration)Registration WHERE Registration.SlNo BETWEEN @StartIndex AND @EndIndex
END
</pre>
<p>And I added a DataRepeater control, with Next and Previous buttons in the Footer.</p>
<pre class="brush: xml;">
&lt;tr&gt;
    &lt;td colspan=&quot;3&quot; align=&quot;left&quot;&gt;
        &lt;asp:LinkButton CommandName=&quot;Navigation&quot; CommandArgument=&quot;Prev&quot; runat=&quot;server&quot; ID=&quot;linkPrevious&quot;
            Text=&quot;&lt;&lt; Previous&quot; /&gt;
    &lt;/td&gt;
    &lt;td colspan=&quot;3&quot; align=&quot;right&quot;&gt;
        &lt;asp:LinkButton CommandName=&quot;Navigation&quot; CommandArgument=&quot;Next&quot; runat=&quot;server&quot; ID=&quot;linkNext&quot; Text=&quot;Next &gt;&gt;&quot; /&gt;
    &lt;/td&gt;
&lt;/tr&gt;
</pre>
<p>Code behind. As it is a sample code, I am not added any validations.</p>
<pre class="brush: csharp;">
int count = 10;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindData(); //Binding Data.
    }
}

private void BindData()
{
    int startIndex = Convert.ToInt32(this.hidCurrentIndex.Value);
    Persons p = new Persons();
    this.Repeater1.DataSource = null;
    this.Repeater1.DataSource = p.GetPersons(startIndex, count);
    this.Repeater1.DataBind();
}

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName.Equals(&quot;Navigation&quot;,
StringComparison.CurrentCultureIgnoreCase))
    {
        if (e.CommandArgument.ToString().Equals(&quot;Prev&quot;,
StringComparison.CurrentCultureIgnoreCase))
        {
            this.hidCurrentIndex.Value = (Convert.ToInt32
(this.hidCurrentIndex.Value) - 1).ToString(); //Decrementing the count
            this.BindData();
        }
        else
        {
            this.hidCurrentIndex.Value = (Convert.ToInt32
(this.hidCurrentIndex.Value) + 1).ToString(); //Incrementing the count
            this.BindData();
        }
    }
}
</pre>
<p>And the implementation of Persons class from App_Code</p>
<pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.Data.SqlClient;

public class Persons : List&lt;Persons.Person&gt;
{
    public class Person
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public DateTime DOB { get; set; }
    }

    public Persons GetPersons(int startIndex, int count)
    {
        Persons persons = new Persons();
        using (SqlConnection connection = new SqlConnection (&quot;Server=.\SQLEXPRESS;User Id=sa;Password=sapwd;Database=myDatabase&quot;))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(&quot;xsp_GetPersons&quot;,

connection))
            {
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.Parameters.AddWithValue(&quot;@StartIndex&quot;, startIndex);
                command.Parameters.AddWithValue(&quot;@Count&quot;, count);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    persons.Add(new Person()
                    {
                        DOB = Convert.ToDateTime(reader[&quot;DOB&quot;].ToString()),
                        Email = reader[&quot;Email&quot;].ToString(),
                        FirstName = reader[&quot;FirstName&quot;].ToString(),
                        LastName = reader[&quot;LastName&quot;].ToString(),
                        UserId = Convert.ToInt32(reader[&quot;UserId&quot;].ToString()),
                    });
                }
            }
        }
        return persons;
    }
}
</pre>
<p>The core thing is stored procedure, which will return the results based on the given index. Thanks to <strong>Aneesh</strong> for the details of ROW_NUMBER() function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/10/22/implementing-custom-paging-in-datarepeater-using-c-and-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Databinding in ASP.Net dropdownlist</title>
		<link>http://www.dotnetthoughts.net/2007/05/14/databinding-in-aspnet-dropdownlist/</link>
		<comments>http://www.dotnetthoughts.net/2007/05/14/databinding-in-aspnet-dropdownlist/#comments</comments>
		<pubDate>Mon, 14 May 2007 07:55:48 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C#.Net]]></category>
		<category><![CDATA[Databinding]]></category>
		<category><![CDATA[Hash Table]]></category>
		<category><![CDATA[VB.Net]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/2007/05/14/databinding-in-aspnet-dropdownlist/</guid>
		<description><![CDATA[In many forums, I used to see people asking for code to bind the dropdown list from basic collections. Here is the code to bind a Hash Table to ASP:Dropdown list, and setting the value of the selected item to the Text box. This is in ASP.Net 1.1. Protected Sub Page_Load(ByVal sender As Object, ByVal [...]]]></description>
			<content:encoded><![CDATA[<p>In many forums, I used to see people asking  for code to bind the dropdown list from basic collections. Here is the code to bind a Hash Table to ASP:Dropdown list, and setting the value of the selected item to the Text box. This is in ASP.Net 1.1.</p>
<pre class="brush: vb;">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not Page.IsPostBack Then
  Dim students As New Hashtable
  students.Add(&quot;1&quot;, &quot;Student1&quot;)
  students.Add(&quot;2&quot;, &quot;Student2&quot;)
  students.Add(&quot;3&quot;, &quot;Student3&quot;)
  students.Add(&quot;4&quot;, &quot;Student4&quot;)
  students.Add(&quot;5&quot;, &quot;Student5&quot;)
  StudentDropdown.DataTextField = &quot;Value&quot;
  StudentDropdown.DataValueField = &quot;key&quot;
  StudentDropdown.DataSource = students
  StudentDropdown.DataBind()
  End If
End Sub
</pre>
<p>And set the <em>Autopostback</em> property of the StudentDropdown to True. And in the SelectedIndexChanged event, write the code to set the value of the Dropdown to the Textbox.</p>
<pre class="brush: vb;">
Protected Sub StudentDropdown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StudentDropdown.SelectedIndexChanged
        If uxSample.SelectedIndex  &lt;&gt; -1 Then
            StudentId.Text = StudentDropdown.Items(StudentDropdown.SelectedIndex).Value
        End If
End Sub
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2007/05/14/databinding-in-aspnet-dropdownlist/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
