Hey akhter,
Please refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="InTime" HeaderText="InTime" />
<asp:BoundField DataField="OutTime" HeaderText="OutTime" />
<asp:TemplateField HeaderText="Hours">
<ItemTemplate>
<asp:TextBox ID="txtHoures" runat="server" Text='<%#Eval("Hours") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Days">
<ItemTemplate>
<asp:TextBox ID="txtDay" runat="server" Text='<%#Eval("Days") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button Text="Update" runat="server" OnClick="Update" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM TestDemotest", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
protected void Update(object sender, EventArgs e)
{
foreach (GridViewRow row in gvCustomers.Rows)
{
string name = row.Cells[0].Text;
string inTime = row.Cells[1].Text;
string OutTime = row.Cells[2].Text;
TextBox txtHours = row.FindControl("txtHoures") as TextBox;
TextBox txtDay = row.FindControl("txtDay") as TextBox;
int hours = 0, day = 0;
if (inTime != " " && OutTime != " ")
{
hours = Convert.ToInt32(txtHours.Text);
day = Convert.ToInt32(txtDay.Text);
}
else if (inTime == " " || OutTime == " ")
{
txtHours.Text = hours.ToString();
txtDay.Text = day.ToString();
}
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("Update TestDemotest set Hours = @hours,Days=@days WHERE name = @name", con))
{
cmd.Parameters.AddWithValue("@hours", hours);
cmd.Parameters.AddWithValue("@days", day);
cmd.Parameters.AddWithValue("@name", name);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvCustomers.Rows
Dim name As String = row.Cells(0).Text
Dim inTime As String = row.Cells(1).Text
Dim OutTime As String = row.Cells(2).Text
Dim txtHours As TextBox = TryCast(row.FindControl("txtHoures"), TextBox)
Dim txtDay As TextBox = TryCast(row.FindControl("txtDay"), TextBox)
Dim hours As Integer = 0, day As Integer = 0
If inTime <> " " AndAlso OutTime <> " " Then
hours = Convert.ToInt32(txtHours.Text)
day = Convert.ToInt32(txtDay.Text)
ElseIf inTime = " " OrElse OutTime = " " Then
txtHours.Text = hours.ToString()
txtDay.Text = day.ToString()
End If
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("Update TestDemotest set Hours = @hours,Days=@days WHERE name = @name", con)
cmd.Parameters.AddWithValue("@hours", hours)
cmd.Parameters.AddWithValue("@days", day)
cmd.Parameters.AddWithValue("@name", name)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End Sub
Screenshot
Updated Database Table.
name |
InTime |
OutTime |
Hours |
Days |
Mudassar |
5/26/2018 9:22 |
5/26/2018 21:22 |
12 |
1 |
john |
5/26/2018 9:22 |
|
0 |
0 |
robert |
|
5/26/2018 21:22 |
0 |
0 |