While i have click on Button get error.
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
namespace webcamimge
{
public partial class emp2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
if (Session["emp_nm"] == null)
Response.Redirect("nlgn.aspx");
else
{
String EmplooyeId = Session["emp_id"].ToString();
Label1.Text = " " + EmplooyeId + "";
String emp_nm = Session["emp_nm"].ToString();
Label2.Text = "" + emp_nm + " ";
String dept = Session["dept"].ToString();
Label3.Text = "" + dept + " ";
}
foreach (GridViewRow row in GridView1.Rows)
{
(row.FindControl("Button3") as Button).Visible = false;
(row.FindControl("Button4") as Button).Visible = true;
(row.FindControl("Button1") as Button).Visible = true;
if (row.Cells[8].Text == "YES")
{
(row.FindControl("Button3") as Button).Visible = true;
(row.FindControl("Button4") as Button).Visible = false;
(row.FindControl("Button1") as Button).Visible = false;
}
}
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Id, vst_nm, idtyp,idnm,fa_hu_nm,gndr,crnt_add,vstd_dt,cisf_in_flag FROM vstr2 WHERE EMP_ID=" + Session["emp_id"] + " and is_vstd='No'", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
protected void btnupdate_Click1(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE vstr2 SET vst_nm = @vst_nm, idtyp = @idtyp, idnm = @idnm, fa_hu_nm = @fa_hu_nm, gndr = @gndr, crnt_add = @crnt_add,vstd_dt = @vstd_dt WHERE Id = @Id", con))
{
con.Open();
cmd.Parameters.AddWithValue("@Id", txtvisitorId.Text);
cmd.Parameters.AddWithValue("@vst_nm", txtName.Text);
cmd.Parameters.AddWithValue("@idtyp", txtIdentitytype.Text);
cmd.Parameters.AddWithValue("@idnm", txttxtIdentityno.Text);
cmd.Parameters.AddWithValue("@fa_hu_nm", txtCustomerfa_hu_nm.Text);
cmd.Parameters.AddWithValue("@gndr", txtCustomergndr.Text);
cmd.Parameters.AddWithValue("@crnt_add", txtCustomercrnt_add.Text);
cmd.Parameters.AddWithValue("@vstd_dt", txtCustomervstd_dt.Text);
cmd.ExecuteNonQuery();
con.Close();
this.BindGrid();
txtvisitorId.Text = "";
txtName.Text = "";
txtIdentitytype.Text = "";
txttxtIdentityno.Text = "";
txtCustomerfa_hu_nm.Text = "";
txtCustomergndr.Text = "";
txtCustomercrnt_add.Text = "";
txtCustomervstd_dt.Text = "";
}
}
}
protected void View(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
txtvisitorId.Text = row.Cells[0].Text;
txtName.Text = row.Cells[1].Text;
txtIdentitytype.Text = row.Cells[2].Text;
txttxtIdentityno.Text = row.Cells[3].Text;
txtCustomerfa_hu_nm.Text = row.Cells[4].Text;
txtCustomergndr.Text = row.Cells[5].Text;
txtCustomercrnt_add.Text = row.Cells[6].Text;
txtCustomervstd_dt.Text = row.Cells[7].Text;
}
protected void Closed(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
int Id = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32((sender as Button).CommandArgument)].Values[0]);
string emp_out_flag = "YES";
string mrk_out_emp = DateTime.Now.ToString();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE vstr2 SET emp_out_flag = @emp_out_flag ,mrk_out_emp=@mrk_out_emp WHERE Id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@emp_out_flag", emp_out_flag);
cmd.Parameters.AddWithValue("@mrk_out_emp", mrk_out_emp);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
row.Visible = false;
}
}
}
protected void Visited(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
int Id = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32((sender as Button).CommandArgument)].Values[0]);
string is_vstd = "YES";
// string mrk_out_emp = DateTime.Now.ToString();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE vstr2 SET is_vstd = @is_vstd WHERE Id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", Id);
// cmd.Parameters.AddWithValue("@emp_out_flag", emp_out_flag);
cmd.Parameters.AddWithValue("@is_vstd", is_vstd);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
row.Visible = false;
}
}
}
protected void dlt(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
int Id = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32((sender as Button).CommandArgument)].Values[0]);
string is_dlt = "YES";
// string mrk_out_emp = DateTime.Now.ToString();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE vstr2 SET is_dlt = @is_dlt WHERE Id = @Id"))
{
cmd.Parameters.AddWithValue("@Id", Id);
// cmd.Parameters.AddWithValue("@emp_out_flag", emp_out_flag);
cmd.Parameters.AddWithValue("@is_dlt", is_dlt);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
row.Visible = false;
}
}
}
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Visitor Id" />
<asp:BoundField DataField="vst_nm" HeaderText="Visitor Name" />
<asp:BoundField DataField="idtyp" HeaderText="Visitor Identity" />
<asp:BoundField DataField="idnm" HeaderText="Identity No." />
<asp:BoundField DataField="fa_hu_nm" HeaderText="Father/Husband Name" />
<asp:BoundField DataField="gndr" HeaderText="Gender" />
<asp:BoundField DataField="crnt_add" HeaderText="Current Address" />
<asp:BoundField DataField="vstd_dt" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Visited Date" />
<asp:BoundField DataField="cisf_in_flag" HeaderText="Gate Pass Issued" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="Button1" Text="Edit" runat="server" CausesValidation="false" OnClick="View" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Visited">
<ItemTemplate>
<asp:Button ID="Button3" Text="Visited" runat="server" CausesValidation="false" CommandName="Visited" OnClientClick="return confirm('Are you sure you want to mark as VISITED this event?');" OnClick="Visited" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="Button4" Text="Delete" runat="server" CausesValidation="false" CommandName="dlt" OnClientClick="return confirm('Are you sure you want to Delete this event?');" OnClick="dlt" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>