Hi SandeshMR,
Check this sample. now take its reference and correct your code.
HTML
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Eval("EmployeeID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblEmpName" Text='<%# Eval("Name") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" Text='<%# Eval("City") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:HiddenField ID="hfCountry" Value='<%# Eval("Country") %>' runat="server" />
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedCountry">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="txtDescription" runat="server" />
<asp:RequiredFieldValidator ID="rfvDescription" runat="server" ControlToValidate="txtDescription"
Text="Required" Enabled="false" ForeColor="Red" ValidationGroup="CheckDescription"
Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="OnSubmit" ValidationGroup="CheckDescription" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindEmployees();
}
}
protected void OnSubmit(object sender, EventArgs e)
{
foreach (GridViewRow row in gvEmployees.Rows)
{
Label lemp = (Label)row.Cells[0].FindControl("lblEmp");
int empid = Convert.ToInt32(lemp.Text);
Label lAttendDt = (Label)row.Cells[4].FindControl("lblAttend");
string AttendDt = lAttendDt.Text;
Label EmpStatus = (Label)row.Cells[5].FindControl("lblEStatus");
string EStatus = EmpStatus.Text;
DropDownList ddlstatus = (DropDownList)row.Cells[6].FindControl("ddlStatus");
string MgrStatus = ddlstatus.SelectedValue;
TextBox txtremarks = (TextBox)row.Cells[8].FindControl("txtRemarks");
string Remarks = txtremarks.Text;
int MgrIDA = 5;
string ApprovedDt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Updaterow(empid, AttendDt, MgrStatus, Remarks, MgrIDA, ApprovedDt);
}
Response.Write("<script language='javascript'>window.alert('Attendance Marked Successfully!');window.location = 'pgMufins.aspx';</script>");
}
private void Updaterow(int empid, string AttendDt, string MgrStatus, string Remarks, int Approvedby, string ApprovedDt)
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string updateData = "update tblMusterAttendance set MgrMusterStatus ='" + MgrStatus + "', MgrRemarks = '" + Remarks + "', Approvedby=" + Approvedby + ", ApproveDate='" + ApprovedDt + "',SubmitStatus='Y' where EmpId =" + empid + "and AttendanceDt ='" + AttendDt + "'";
SqlConnection con = new SqlConnection(constring);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = updateData;
cmd.Connection = con;
cmd.ExecuteNonQuery();
}
protected void OnSelectedCountry(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)(sender as DropDownList).NamingContainer as GridViewRow);
if ((row.FindControl("hfCountry") as HiddenField).Value != (sender as DropDownList).SelectedValue)
{
RequiredFieldValidator rfvTextDescription = (row.FindControl("rfvDescription") as RequiredFieldValidator);
rfvTextDescription.Enabled = true;
}
}
private void BindEmployees()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 EmployeeID,(FirstName+' '+LastName) AS Name,City,Country FROM Employees", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvEmployees.DataSource = dt;
this.gvEmployees.DataBind();
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountry = (e.Row.FindControl("ddlCountry") as DropDownList);
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Employees", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
ddlCountry.DataSource = dt;
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "Country";
ddlCountry.DataBind();
string selectedCountry = DataBinder.Eval(e.Row.DataItem, "Country").ToString();
ddlCountry.Items.FindByValue(selectedCountry).Selected = true;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindEmployees()
End If
End Sub
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvEmployees.Rows
Dim lemp As Label = CType(row.Cells(0).FindControl("lblEmp"), Label)
Dim empid As Integer = Convert.ToInt32(lemp.Text)
Dim lAttendDt As Label = CType(row.Cells(4).FindControl("lblAttend"), Label)
Dim AttendDt As String = lAttendDt.Text
Dim EmpStatus As Label = CType(row.Cells(5).FindControl("lblEStatus"), Label)
Dim EStatus As String = EmpStatus.Text
Dim ddlstatus As DropDownList = CType(row.Cells(6).FindControl("ddlStatus"), DropDownList)
Dim MgrStatus As String = ddlstatus.SelectedValue
Dim txtremarks As TextBox = CType(row.Cells(8).FindControl("txtRemarks"), TextBox)
Dim Remarks As String = txtremarks.Text
Dim MgrIDA As Integer = 5
Dim ApprovedDt As String = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
Updaterow(empid, AttendDt, MgrStatus, Remarks, MgrIDA, ApprovedDt)
Next
Response.Write("<script language='javascript'>window.alert('Attendance Marked Successfully!');window.location = 'pgMufins.aspx';</script>")
End Sub
Private Sub Updaterow(ByVal empid As Integer, ByVal AttendDt As String, ByVal MgrStatus As String, ByVal Remarks As String, ByVal Approvedby As Integer, ByVal ApprovedDt As String)
Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim updateData As String = "update tblMusterAttendance set MgrMusterStatus ='" & MgrStatus & "', MgrRemarks = '" & Remarks & "', Approvedby=" & Approvedby & ", ApproveDate='" & ApprovedDt & "',SubmitStatus='Y' where EmpId =" & empid & "and AttendanceDt ='" & AttendDt & "'"
Dim con As SqlConnection = New SqlConnection(constring)
con.Open()
Dim cmd As SqlCommand = New SqlCommand()
cmd.CommandText = updateData
cmd.Connection = con
cmd.ExecuteNonQuery()
End Sub
Protected Sub OnSelectedCountry(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = (TryCast(CType((TryCast(sender, DropDownList)).NamingContainer, GridViewRow), GridViewRow))
If (TryCast(row.FindControl("hfCountry"), HiddenField)).Value <> (TryCast(sender, DropDownList)).SelectedValue Then
Dim rfvTextDescription As RequiredFieldValidator = (TryCast(row.FindControl("rfvDescription"), RequiredFieldValidator))
rfvTextDescription.Enabled = True
End If
End Sub
Private Sub BindEmployees()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 5 EmployeeID,(FirstName+' '+LastName) AS Name,City,Country FROM Employees", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvEmployees.DataSource = dt
Me.gvEmployees.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlCountry As DropDownList = (TryCast(e.Row.FindControl("ddlCountry"), DropDownList))
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT DISTINCT Country FROM Employees", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
ddlCountry.DataSource = dt
ddlCountry.DataTextField = "Country"
ddlCountry.DataValueField = "Country"
ddlCountry.DataBind()
Dim selectedCountry As String = DataBinder.Eval(e.Row.DataItem, "Country").ToString()
ddlCountry.Items.FindByValue(selectedCountry).Selected = True
End Using
End Using
End Using
End If
End Sub
Screenshot
![](https://i.imgur.com/8tSX6gR.gif)