I have verified your code. You need to change the JavaScript code.
Rest code is ok.
Refer below code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
You need to modify the code as per your columns in the table.
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/tag-it/2.0/css/jquery.tagit.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/tag-it/2.0/css/tagit.ui-zendesk.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tag-it/2.0/js/tag-it.min.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
url: "<%=ResolveUrl("Default.aspx/GetCustomers")%>",
data: "{}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
var sampleTags = [];
$.each(data.d, function (i, item) {
sampleTags.push(item);
});
$(".myTags").tagit({
availableTags: sampleTags
});
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
});
$(document).on('click', '[id*=btnSave]', function () {
var ids = [];
$.each($('[id*=GridView1] tr:has(td)'), function () {
var currentIds = [];
$.each($(this).find('.tagit-choice'), function () {
currentIds.push($(this).find('input[type=hidden]').val());
});
ids.push(currentIds.join(','));
});
$('[id*=hfCustomerIds]').val(ids.join('|'));
});
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Candara" Width="1300px">
<Columns>
<asp:BoundField DataField="orderid" HeaderText="id" ItemStyle-Width="30" />
<asp:BoundField DataField="purchaseorderno" HeaderText="PO NO" ItemStyle-Width="100" />
<asp:BoundField DataField="quantity" HeaderText="Quantity" />
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<div align="center">
<asp:TextBox ID="txtdate" runat="server" Height="38" Width="200" Text='<%#DateTime.Today.ToString("MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture) %>'></asp:TextBox>
</div>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Waybill">
<ItemTemplate>
<asp:TextBox ID="txtwaybill" runat="server" Width="100" Height="38"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:TextBox ID="txtcomment" runat="server" Width="100" Height="38"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity Been Recieved">
<ItemTemplate>
<asp:TextBox ID="txtcomment1" runat="server" Width="100" Height="38"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Baseno" ItemStyle-Width="200" ItemStyle-Height="38">
<ItemTemplate>
<ul id="myTags" class="myTags">
</ul>
<asp:HiddenField ID="hfIds" runat="server" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hfCustomerIds" runat="server" />
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="OnUpdate" />
Code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GridView1.DataSource = GetData();
GridView1.DataBind();
}
foreach (GridViewRow row in GridView1.Rows)
{
foreach (TableCell cell in row.Cells)
{
cell.Attributes.CssStyle["text-align"] = "center";
}
}
// txtdate.Text = DateTime.Today.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
}
[WebMethod]
public static string[] GetCustomers()
{
List<string> customers = new List<string>();
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT DISTINCT CustomerID baseno FROM Orders";
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(sdr["baseno"].ToString());
}
}
conn.Close();
}
}
return customers.ToArray();
}
protected void OnUpdate(object sender, EventArgs e)
{
//if (Session["tt"].ToString() == "3")
{
List<string> tags = hfCustomerIds.Value.Split(new char[] { ',', '|' }).ToList();
List<string> duplicates = tags.Where(x => x != "").GroupBy(x => x).SelectMany(g => g.Skip(1)).ToList();
if (duplicates.Count == 0)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
string orderid = row.Cells[0].Text;
string deliverydate = (row.FindControl("txtdate") as TextBox).Text.Trim();
string waybillno = (row.FindControl("txtwaybill") as TextBox).Text.Trim();
string comment = (row.FindControl("txtcomment") as TextBox).Text.Trim();
string qtyreceived = (row.FindControl("txtcomment1") as TextBox).Text.Trim();
// string selectedTags = (row.FindControl("hfIds") as HiddenField).Value.Trim().Replace(",", "/");
string selectedTags = hfCustomerIds.Value.Split('|')[i];
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "UPDATE orders SET deliverydate=@deliverydate, qtyreceived=@qtyreceived ,waybillno=@waybillno,comment=@comment,clicked ='2' WHERE orderid=@orderid ";
query += " INSERT INTO orders_cylinder (orderid,baseno) VALUES (@orderid,@accepted)";
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@orderid", orderid);
cmd.Parameters.AddWithValue("@deliverydate", deliverydate);
cmd.Parameters.AddWithValue("@waybillno", waybillno);
cmd.Parameters.AddWithValue("@comment", comment);
cmd.Parameters.AddWithValue("@qtyreceived", qtyreceived);
cmd.Parameters.AddWithValue("@accepted", selectedTags);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Response.Redirect("1.aspx");
}
else
{
Response.Redirect("2.aspx");
}
}
}
private DataTable GetData()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT TOP 4 EmployeeId orderid,OrderId purchaseorderno,ShipVia quantity FROM orders";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
Screenshot