I am having this exception while running the code. Please check and let me know what I am missing here. I am providing the lines of code below. I am having the error at these line.
string breach_reason = (e.Row.FindControl("lblbreach") as Label).Text;
ddlbreachreason.Items.FindByValue(breach_reason).Selected = true;
Thanks,
Srivatsan Srinivasan.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Lblnotification.Text = "Please click the checkbox to edit the Gridview";
this.BindGrid();
}
}
private void BindGrid()
{
SqlCommand cmd = new SqlCommand("select * from Incident_Details");
GVData.DataSource = this.ExecuteQuery(cmd, "SELECT");
GVData.DataBind();
}
private DataTable ExecuteQuery(SqlCommand cmd, string action)
{
string conString = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
switch (action)
{
case "SELECT":
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
case "UPDATE":
con.Open();
cmd.ExecuteNonQuery();
con.Close();
break;
}
return null;
}
}
protected void Rowdatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd = new SqlCommand("select * from Breach_Reason");
DropDownList ddlbreachreason = (e.Row.FindControl("ddlbreachreason") as DropDownList);
ddlbreachreason.DataSource = this.ExecuteQuery(cmd, "SELECT");
ddlbreachreason.DataTextField = "breach_reason";
ddlbreachreason.DataValueField = "breach_reason";
ddlbreachreason.DataBind();
string breach_reason = (e.Row.FindControl("lblbreach") as Label).Text;
ddlbreachreason.Items.FindByValue(breach_reason).Selected = true;
}
}
protected void oncheckedchanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "Checkall")
{
foreach (GridViewRow row in GVData.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
}
CheckBox Checkall = (GVData.HeaderRow.FindControl("Checkall") as CheckBox);
Checkall.Checked = true;
foreach (GridViewRow row in GVData.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 1; i < row.Cells.Count; i++)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
}
if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
}
if (isChecked && !isUpdateVisible)
{
isUpdateVisible = true;
}
if (!isChecked)
{
Checkall.Checked = false;
}
}
}
}
Btnupdate.Visible = isUpdateVisible;
}
protected void Update(object sender, EventArgs e)
{
foreach (GridViewRow row in GVData.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
if (isChecked)
{
SqlCommand cmd = new SqlCommand("update Incident_Details set breach_reason = @breach_reason and Resolution_Cateogarization = @Resolution_Cateogarization where Incident_Number = @Incident_Number");
cmd.Parameters.AddWithValue("@Resolution_Cateogarization", row.Cells[2].Controls.OfType<TextBox>().FirstOrDefault().Text);
cmd.Parameters.AddWithValue("@breach_reason", row.Cells[3].Controls.OfType<DropDownList>().FirstOrDefault().SelectedItem.Value);
this.ExecuteQuery(cmd, "SELECT");
}
}
}
Btnupdate.Visible = false;
this.BindGrid();
}
}