Hi lingers
pls refer below code
HTML
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="None"
Font-Names="Century Gothic" Font-Size="x-Small" DataKeyNames="id" Width="38%" Style="margin-bottom: 0px" CssClass="grid">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chPrkSelect" runat="server" OnCheckedChanged="OnChckedChanged" Text="Add" AutoPostBack="True" />
<asp:HiddenField ID="hfPreText" runat="server" Value='<%# Eval("pretext") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" HeaderText="id" />
<asp:BoundField DataField="accepted" HeaderText="Cylinder No" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#7EA9D3" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000000" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="#000000" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindUserDetails();
}
}
protected void BindUserDetails()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("Select id,purchaseorderno,accepted,pretext from orders ORDER BY id DESC", con))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
}
}
protected void OnChckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
string id = row.Cells[1].Text;
string number = row.Cells[2].Text;
string preText = (row.FindControl("hfPreText") as HiddenField).Value;
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
string acceptedCylinder = GetAcceptedCylinder(preText);
if (chk.Checked)
{
if (string.IsNullOrEmpty(acceptedCylinder))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Tracking VALUES(@PreText, @Cylinder)", con))
{
cmd.Parameters.AddWithValue("@PreText", preText);
cmd.Parameters.AddWithValue("@Cylinder", "{'" + number.Trim() + "'}");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record inserted successfully.')", true);
}
else
{
List<string> cylinderNo = acceptedCylinder.Replace("{", "").Replace("}", "").Replace("'", "").Split(',').ToList();
cylinderNo.Add(number);
using (SqlCommand cmd = new SqlCommand("UPDATE Tracking SET accepted_cylinderid = @Cylinder WHERE pretext = @Text", con))
{
cmd.Parameters.AddWithValue("@Text", preText);
cmd.Parameters.AddWithValue("@Cylinder", "{'" + string.Join("','", cylinderNo.Distinct()) + "'}");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record updated successfully.')", true);
}
}
else
{
if (!string.IsNullOrEmpty(acceptedCylinder))
{
List<string> cylinderNo = acceptedCylinder.Replace("{", "").Replace("}", "").Replace("'", "").Split(',').ToList();
cylinderNo.Remove(number);
using (SqlCommand cmd = new SqlCommand("UPDATE Tracking SET accepted_cylinderid = @Cylinder WHERE pretext = @Text", con))
{
cmd.Parameters.AddWithValue("@Text", preText);
cmd.Parameters.AddWithValue("@Cylinder", "{'" + string.Join("','", cylinderNo.Distinct()) + "'}");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Deleted successfully')", true);
}
}
}
}
private string GetAcceptedCylinder(string pretext)
{
string acceptCylinder = "";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT accepted_cylinderid FROM Tracking WHERE pretext = @text";
cmd.Connection = con;
cmd.Parameters.AddWithValue("@text", pretext);
con.Open();
acceptCylinder = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
return acceptCylinder;
}
VB.Net
Class SurroundingClass
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
BindUserDetails()
End If
End Sub
Protected Sub BindUserDetails()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("Select id,purchaseorderno,accepted,pretext from orders ORDER BY id DESC", con)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvDetails.DataSource = dt
gvDetails.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnChckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim chk As CheckBox = CType(sender, CheckBox)
Dim row As GridViewRow = CType(chk.NamingContainer, GridViewRow)
Dim id As String = row.Cells(1).Text
Dim number As String = row.Cells(2).Text
Dim preText As String = (TryCast(row.FindControl("hfPreText"), HiddenField)).Value
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Dim acceptedCylinder As String = GetAcceptedCylinder(preText)
If chk.Checked Then
If String.IsNullOrEmpty(acceptedCylinder) Then
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Tracking VALUES(@PreText, @Cylinder)", con)
cmd.Parameters.AddWithValue("@PreText", preText)
cmd.Parameters.AddWithValue("@Cylinder", "{'" & number.Trim() & "'}")
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('Record inserted successfully.')", True)
Else
Dim cylinderNo As List(Of String) = acceptedCylinder.Replace("{", "").Replace("}", "").Replace("'", "").Split(","c).ToList()
cylinderNo.Add(number)
Using cmd As SqlCommand = New SqlCommand("UPDATE Tracking SET accepted_cylinderid = @Cylinder WHERE pretext = @Text", con)
cmd.Parameters.AddWithValue("@Text", preText)
cmd.Parameters.AddWithValue("@Cylinder", "{'" & String.Join("','", cylinderNo.Distinct()) & "'}")
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('Record updated successfully.')", True)
End If
Else
If Not String.IsNullOrEmpty(acceptedCylinder) Then
Dim cylinderNo As List(Of String) = acceptedCylinder.Replace("{", "").Replace("}", "").Replace("'", "").Split(","c).ToList()
cylinderNo.Remove(number)
Using cmd As SqlCommand = New SqlCommand("UPDATE Tracking SET accepted_cylinderid = @Cylinder WHERE pretext = @Text", con)
cmd.Parameters.AddWithValue("@Text", preText)
cmd.Parameters.AddWithValue("@Cylinder", "{'" & String.Join("','", cylinderNo.Distinct()) & "'}")
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
ScriptManager.RegisterClientScriptBlock(Me, Me.[GetType](), "alertMessage", "alert('Record Deleted successfully')", True)
End If
End If
End Using
End Sub
Private Function GetAcceptedCylinder(ByVal pretext As String) As String
Dim acceptCylinder As String = ""
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT accepted_cylinderid FROM Tracking WHERE pretext = @text"
cmd.Connection = con
cmd.Parameters.AddWithValue("@text", pretext)
con.Open()
acceptCylinder = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Return acceptCylinder
End Function
End Class
Screenshot
Database after insert