Creating and Using TextBox in Rows Like An Array To Update Multiple Records At Once In Asp.Net

Updating multiple data at multiple rows in table is very often process on web application. Especially on interal / private system.

We have to make a grid like excel to do that. User inputs their data into multiple textboxes in rows / cells then clicks submit button to update those data at once.

Asp.Net server input control has many ways to access list of textboxes. Not only using array like asp classic do but we can do it using repeater / listview / gridview / table control as a grid creator and use FindControl method to access control inside grid including textboxes.

I have an example to update email of many customers so that when submit button is clicked then related email textboxes text will be stored on database.

I use VS 2010 and my local SQL Express 2005 with Northwind sample database to make this example.

The design mode code:

<form id="form1" runat="server">
    <div>
    <table>
    <asp:Repeater ID="repeater1" runat="server" DataSourceID="customerdbs" >
        <HeaderTemplate>
           <thead>
           <tr>
            <th>Company Name</th>
            <th>Contact Name</th>
            <th>Contact Title</th>
            <th>Phone</th>
            <th>Email</th>
            </tr>
           </thead>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
            <td><%# Eval("CompanyName") %></td>
            <td><%# Eval("ContactName") %></td>
            <td><%# Eval("ContactTitle") %></td>
            <td><%# Eval("Phone") %></td>
            <td><asp:TextBox ID="email" runat="server" Text='<%# Eval("Email") %>'></asp:TextBox>
                <asp:HiddenField ID="customerid" runat="server" Value='<%# Eval("customerid") %>' />
            </td>
            
            </tr>
        </ItemTemplate>
    </asp:Repeater>
    </table>
    <asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />
    </div>
    
    <asp:SqlDataSource runat="server" ID="customerdbs" 
     SelectCommand="select customerid, companyname, contactname, contacttitle, phone, email from Customers" SelectCommandType="Text">       
    </asp:SqlDataSource>
    </form>

Here I have repeater and inside this repeater there are textbox and hiddenfield control in ItemTemplate. With repeater those textbox and hiddenfield will be repeated so that I have multiple of those.
Textboxes will contains email data and hiddenfields will have customerid. I need the cusomterid to update email in specific record.

I use sqldatasource to fill in repeater control with Northwind’s customers data.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Misc
{
    public partial class TextboxArray : System.Web.UI.Page
    {
        private string connectionstr;
        protected void Page_Load(object sender, EventArgs e)
        {
            connectionstr = @"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated security=true";
            customerdbs.ConnectionString = connectionstr;
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            string query = "";
            SqlConnection conn = new SqlConnection(connectionstr);
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            for (int i = 0; i < repeater1.Items.Count; i++)
            {
                TextBox txtemail = (TextBox)repeater1.Items[i].FindControl("email");
                HiddenField custid = (HiddenField)repeater1.Items[i].FindControl("customerid");

                if (txtemail.Text.Trim().Length > 0)
                {
                    query = "update customers set email='" + txtemail.Text.Trim() + "' where customerid='" + custid.Value + "'";
                    comm.CommandText = query;
                    comm.ExecuteNonQuery();
                }
              
            }

            comm.Dispose();
            conn.Close();
        }
    }
}

I do a loop for all repeater’s items and use FindControl method with each control ID to access multiple textboxes & hiddenfields control.
If email’s textbox is not empty then update it to database with reference from customerid hiddenfield.
Off course you can do better to validate email address.

Regards,
Agung Gugiaji

Advertisements

One response to “Creating and Using TextBox in Rows Like An Array To Update Multiple Records At Once In Asp.Net

  1. You are an angel for me. I have wasted my whole night in this problem. m newbie :(… thx for the help… Bookmarking it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s