Unveiling www.ScreencastADay.com

What is ScreencastADay?

Welcome to www.ScreencastADay.com, a website committed to educate you everyday. The idea of the website is really simple. We believe that in order to be successful you need to learn something new everyday. ScreencastADay puts this thought into action by providing new screencast every day. This means you will get 7 screencasts a week and 365 screencasts a year unless of course it is a leap year in that case you get one more bonus screencast.

Is this website completely free?

Yes! Currently, there is no registration fee associated with the website.


How can I request a Screencast?

You can request a website using the ScreenCastADay Google Group .


Are there any exceptions?

Although we don’t like to make any exceptions to this rule but life is uncertain and can take unexpected turns. All we can say is that we will try our best to work around your wishes and provide screencast every day.

Subscribe to my feeds

Hi you can subscribe to my feeds using the link below:

http://feeds.feedburner.com/Azamsharp

Thanks,
Azam

So long GeeksWithBlogs!

I have blogged on GeeksWithBlogs for a long time and I have enjoyed every second of it. For the past couple of days I have been working on my own blog and today it has been launched. I will be blogged at www.azamsharp.com. I will miss GeeksWithBlogs and I thank everyone for creating such a wonderful blogging community.

As of today (May 27 2008) this blog is officially closed.

Please update your links to www.azamsharp.com.

Thanks,

AzamSharp 

Article Published: Only Updating the Changed Rows in GridView Control

Matt Berseth wrote a very interesting article on “Bulk Inserting Data Using the ListView Control". The idea is to give the user an Excel like interface where they can edit the rows with custom data and finally click the update button to persist the data in the database. Matt’s solution was great but lacks a very important detail. There was no way to know what rows were changed by the user. Matt took the road to update all the rows whether they were changed or not. In this article we are going to take a look at an alternative method of updating only the changed rows.

Updating Only Changed Rows in the GridView Control

Updating Only Changed Rows Using GridView Control

Mark Berseth wrote a very interested article about Bulk Insert Data Using ListView Control. Basically, the ListView control is displayed with the TextBox controls in each row (like MS Excel). Now, the user can write in the TextBox and then use the submit button to insert all the new rows to the database. This works well when you are inserting the data into the database. The problem starts when you try to update the data which is already in the database. Let's say you populate the ListView with some data and now you want to edit it. You will change only one row and press the submit button. The ListView control does not know which row has been changed and hence you will have to send all the rows back to the database even though only a single row got changed.

There should be some way to only update the changed row. In this post I am going to discuss a simple technique that solves this problem. Let's start by populating the GridView control (You can use ListView if you prefer).

 private void BindData()
        {
            string connectionString = "Server=localhost;Database=Northwind;Trusted_Connection=true";
            SqlConnection conn = new SqlConnection(connectionString);
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Categories", conn);

            DataSet ds = new DataSet();
            ad.Fill(ds);

            gvCategories.DataSource = ds;
            gvCategories.DataBind();
        }

And here is the HTML view of the GridView control.

<asp:GridView ID="gvCategories" runat="server" DataKeyNames="id" AutoGenerateColumns="false"
            >
   
    <Columns>
   
    <asp:TemplateField HeaderText="CategoryID">
   
    <ItemTemplate>
    <%# Eval("id") %>
    </ItemTemplate>
   
    </asp:TemplateField>
   
       <asp:TemplateField HeaderText="CategoryName">
   
    <ItemTemplate>
    <asp:TextBox ID="txtCategoryName" onchange='<%# AttachFunction(Container.DataItemIndex) %>' runat="server" Text='<%# Eval("CategoryName") %>' />
    </ItemTemplate>
   
    </asp:TemplateField>
   
    <asp:TemplateField HeaderText="Description">
   
    <ItemTemplate>
    <asp:TextBox ID="txtCategoryDescription" onchange='<%# AttachFunction(Container.DataItemIndex) %>' runat="server" Text='<%# Eval("Description") %>' />
    </ItemTemplate>
   
    </asp:TemplateField>
   
    </Columns>
   
    </asp:GridView>

 

I am attaching the onchange JavaScript function to the TextBoxes. The onchange event will be fired whenever the text inside the TextBox will change. I am also passing the current RowIndex of the GridView row. This will keep track which row has been changed. Now, let's check out the AttachFunction which is a server side function.

 protected string AttachFunction(int rowIndex)
        {
            string function = "saveRowIndex(";

            return function + rowIndex + ")";
        }

function saveRowIndex(rowIndex)
{
    AjaxWithJQuery.SimpleService.NoteChangedRows(rowIndex);   
}

In the above code I am firing an Ajax method named NoteChangedRows.

[WebMethod(true)]
        public void NoteChangedRows(int rowIndex)
        {
            List<Int32> list = null;

            if (Session["List"] == null)
            {
                list = new List<int>();

                if (list.Contains(rowIndex)) return;


                list.Add(rowIndex);
                Session["List"] = list;
            }

            else
            {
                list = Session["List"] as List<Int32>;

                if (list.Contains(rowIndex)) return;

                list.Add(rowIndex);
            }

           
        }

NoteChangedRows simply stores the row index of the rows that have been changed. Now, let's see the Update method which is fired when the user clicks the update button.

        protected void Update(object sender, EventArgs e)
        {
            // get only the changed rows and not all the rows
            List<Int32> list = Session["List"] as List<Int32>;

          
            for (int i = 0; i < list.Count; i++)
            {
                GridViewRow row = gvCategories.Rows[list[i]];


                int id = (int) gvCategories.DataKeys[list[i]].Value;
                string categoryName = (row.FindControl("txtCategoryName") as TextBox).Text;
                string categoryDescription = (row.FindControl("txtCategoryDescription") as TextBox).Text;

                // DO THE ACTUAL UPDATE HERE!

                lblFinal.Text += id + "<BR>" + categoryName + "<BR>" + categoryDescription;
            }

            // reset the list!
            Session["List"] = null;
           

        }

The update method simply looks into the RowIndex list and finds the changed rows from that list. Now, we only have to update those changed rows and not all the rows.

I will write an article on this technique and publish it on GridViewGuy. So, stay tunned!

Twitter