<add name=”connection” connectionString=”Data Source=***.***.*.*;Initial Catalog=DataBaseName;Persist Security Info=True;User ID=SA**Uaser;Password=******” providerName=”System.Data.SqlClient”/>

DbHelper

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace TestNameSpace
{
internal class DbHelper : IDisposable
{
public DbHelper()
{
OpenConnection();
}

#region PRIVATE MEMBERS

private SqlConnection _connection;

///

/// Gets Connection String from web configuration file
///

private static string ConnectionString
{
get { return ConfigurationManager.ConnectionStrings[“connection”].ConnectionString; }
}

#endregion

#region METHODS

///

/// Establishes connection with sql server
///

private void OpenConnection()
{
try
{
// create a new instance of sqlconnection class.
_connection = new SqlConnection(ConnectionString);

// if connection is open the close it and again open it.This approach is followed
// only for safety or ignoring the status any existing connection.
if (_connection.State == ConnectionState.Open)
{
_connection.Close();
}
_connection.Open();
}
catch
{
throw new Exception(“An error occured while communicating to sql server database.”);
}
}

///

/// Closes existing sql server connection from database
///

private void CloseConnection()
{
try
{
if (_connection.State == ConnectionState.Open)
{
_connection.Close();
}
}
finally
{
_connection.Dispose();
}
}

///

/// Executes any Create , Update or Delete query and in lieu returns the total
/// number of rows affected by the query
///

/// sql statement as string whether it is sp or inline query.
/// command type
///
public int ExecuteNonQuery(string commandText, CommandType commandType)
{
int result;
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
result = command.ExecuteNonQuery();
}
return result;
}

///

/// Executes any Create , Update or Delete query and in lieu returns the total
/// number of rows affected by the query
///

/// sql statement as string
/// type of command whether it is sp or inline query
/// sql parameter
///
public int ExecuteNonQuery(string commandText, CommandType commandType, SqlParameter parameter)
{
int result;
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
command.Parameters.Add(parameter);
result = command.ExecuteNonQuery();
command.Parameters.Clear();

}
return result;
}

///

/// Executes any Create , Update or Delete query and in lieu returns the total
/// number of rows affected by the query
///

/// sql statement as string
/// type of command whether it is sp or inline query
/// array of sql parameter
///
public int ExecuteNonQuery(string commandText, CommandType commandType, SqlParameter[] parameters)
{
int result;
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
command.Parameters.AddRange(parameters);
result = command.ExecuteNonQuery();
command.Parameters.Clear();

}
return result;
}

///

/// Executes any Create , Update, Delete or even Select query and in lieu returns the value
/// of first row and first column
///

/// sql statement as string whether it is sp or inline query.
/// command type
///
public object ExecuteScalar(string commandText, CommandType commandType)
{
object result;
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
result = command.ExecuteScalar();

}
return result;
}

///

/// Executes any Create , Update, Delete or even Select query and in lieu returns the value
/// of first row and first column
///

/// sql statement as string whether it is sp or inline query.
/// command type
/// sql parameter
///
public object ExecuteScalar(string commandText, CommandType commandType, SqlParameter parameter)
{
object result;
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
command.Parameters.Add(parameter);
result = command.ExecuteScalar();
command.Parameters.Clear();

}
return result;
}

///

/// Executes any Create , Update, Delete or even Select query and in lieu returns the value
/// of first row and first column
///

/// sql statement as string whether it is sp or inline query.
/// command type
/// array of sql parameter
///
public object ExecuteScalar(string commandText, CommandType commandType, SqlParameter[] parameters)
{
object result;
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
command.Parameters.AddRange(parameters);
result = command.ExecuteScalar();
command.Parameters.Clear();

}
return result;
}

///

/// Executes Select query and in lieu returns the table of the resultant query
///

/// sql statement as string whether it is sp or inline query.
/// command type
/// Datatable
public DataTable ExecuteSelect(string commandText, CommandType commandType)
{
var result = new DataTable();
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
using (SqlDataReader reader = command.ExecuteReader())
{
result.Load(reader);
}
}
return result;
}

///

/// Executes Select query and in lieu returns the table of the resultant query
///

/// sql statement as string whether it is sp or inline query.
/// command type
/// sql parameter
/// datatable
public DataTable ExecuteSelect(string commandText, CommandType commandType, SqlParameter parameter)
{
var result = new DataTable();
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
command.Parameters.Add(parameter);
using (SqlDataReader reader = command.ExecuteReader())
{
result.Load(reader);
}
command.Parameters.Clear();

}
return result;
}

///

/// Executes Select query and in lieu returns the table of the resultant query
///

/// sql statement as string whether it is sp or inline query.
/// command type
/// array of sql parameter
/// Datatable
public DataTable ExecuteSelect(string commandText, CommandType commandType, SqlParameter[] parameters)
{
var result = new DataTable();
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
command.Parameters.AddRange(parameters);
using (SqlDataReader reader = command.ExecuteReader())
{
result.Load(reader);
}
command.Parameters.Clear();

}
return result;
}

///

/// Executes Select query and in lieu returns the set of tables of the resultant query
///

/// sql statement as string whether it is sp or inline query.
/// command type
/// Datatset
public DataSet ExecuteSelectDataSet(string commandText, CommandType commandType)
{
var result = new DataSet();
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
using (var adapter = new SqlDataAdapter(command))
{
adapter.Fill(result);
}
}
return result;
}

///

/// Executes Select query and in lieu returns the set of tables of the resultant query
///

/// sql statement as string whether it is sp or inline query.
/// command type
/// sql parameter
/// dataset
public DataSet ExecuteSelectDataSet(string commandText, CommandType commandType, SqlParameter parameter)
{
var result = new DataSet();
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
command.Parameters.Add(parameter);
using (var adapter = new SqlDataAdapter(command))
{
adapter.Fill(result);
}
command.Parameters.Clear();

}
return result;
}

///

/// Executes Select query and in lieu returns the set of tables of the resultant query
///

/// sql statement as string whether it is sp or inline query.
/// command type
/// array of sql parameter
/// Dataset
public DataSet ExecuteSelectDataSet(string commandText, CommandType commandType, SqlParameter[] parameters)
{
var result = new DataSet();
using (var command = new SqlCommand(commandText, _connection))
{
command.CommandType = commandType;
command.Parameters.AddRange(parameters);
using (var adapter = new SqlDataAdapter(command))
{
adapter.Fill(result);
}
command.Parameters.Clear();

}
return result;
}

#endregion

// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.

#region IDisposable Members

public void Dispose()
{
CloseConnection();

// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}

#endregion
}
}

Page loading in update panel

<%–
<“div style=”position: fixed; text-align: center; height: 100%; width: 100%; top: 0; right: 0; left: 0; z-index: 9999999; background-color: #000000; opacity: 0.7;”>
<“span style=”border-width: 0px; position: fixed; padding: 50px; background-color: #000000; font-size: 36px; left: 40%; top: 40%;”>
<“img alt=”” height=”10px” width=”20px” src=”../images/loader.gif” style=”width: 91%; height: 18%;”>

 

–%>

Gridview Add update delete

*******ASPX page Coding*********

<asp:GridView ID=”grdInspectionMap” runat=”server” BorderWidth=”1px” AutoGenerateColumns=”false”
AllowSorting=”true” BackColor=”White” CssClass=”summary” BorderStyle=”Solid”
BorderColor=”#009955″ CellPadding=”2″ CellSpacing=”2″ DataKeyNames=”DivisionId”
AllowPaging=”True” PageSize=”20″ OnPageIndexChanging=”grdInspectionMap_PageIndexChanging”
OnRowCreated=”grdInspectionMap_RowCreated” OnRowCancelingEdit=”grdInspectionMap_RowCancelingEdit”
OnRowDeleting=”grdInspectionMap_RowDeleting” OnRowEditing=”grdInspectionMap_RowEditing”
OnRowUpdating=”grdInspectionMap_RowUpdating”>
<PagerTemplate>
<table width=”100%”>
<tr>
<td align=”right”>
<font color=”black”>Goto Page</font>
<asp:DropDownList ID=”ddlInspectionPageSelector” runat=”server” AutoPostBack=”true”
OnSelectedIndexChanged=”ddlInspectionPageSelector_SelectedIndexChanged”>
</asp:DropDownList>
<asp:Button Text=”First” CommandName=”Page” CssClass=”inputButton” CommandArgument=”First”
runat=”server” ID=”btnFirst” />
<asp:Button Text=”Previous” CommandName=”Page” CssClass=”inputButton” CommandArgument=”Prev”
runat=”server” ID=”btnPrevious” />
<asp:Button Text=”Next” CommandName=”Page” CssClass=”inputButton” CommandArgument=”Next”
runat=”server” ID=”btnNext” />
<asp:Button Text=”Last” CommandName=”Page” CssClass=”inputButton” CommandArgument=”Last”
runat=”server” ID=”btnLast” />
</td>
</tr>
</table>
</PagerTemplate>
<Columns>
<asp:TemplateField HeaderText=”SNO”>
<ItemTemplate>
<%# Container.DataItemIndex +1 %>
</ItemTemplate>
<ItemStyle Width=”10px” />
</asp:TemplateField>
<asp:TemplateField HeaderText=”ID” Visible=”false”>
<ItemTemplate>
<asp:Label ID=”lblInspectionQuestion” runat=”server” Text='<%# Bind(“DivisionId”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Division Name”>
<ItemTemplate>
<asp:Label ID=”lblName” runat=”server” Text='<%# Bind(“Name”) %>’></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=”txtName” Text='<%# Bind(“Name”) %>’
runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”rfvInspectionQuestion1″ runat=”server” ControlToValidate=”txtName”
Display=”None” ValidationGroup=”Edit_Inspection”></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID=”txtName_F” runat=”server”></asp:TextBox>
<asp:RequiredFieldValidator ID=”rfvInspectionQuestion2″ runat=”server” ControlToValidate=”txtName_F”
Display=”None” ValidationGroup=”Add_Inspection”></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText=”Action”>
<ItemTemplate>
<asp:LinkButton ID=”btnEdit” Text=”Edit” runat=”server” CausesValidation=”true” CommandName=”Edit”
Visible='<%#(Convert.ToBoolean(Eval(“DivisionId”))) %>’ />&nbsp;&nbsp;
<asp:LinkButton ID=”btnDelete” Text=”Delete” runat=”server” Visible='<%#(Convert.ToBoolean(Eval(“DivisionId”))) %>’
CommandName=”Delete” />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID=”btnUpdate” Text=”Update” runat=”server” CommandName=”Update”
ValidationGroup=”Edit_Inspection” />
<asp:LinkButton ID=”btnCancel” Text=”Cancel” runat=”server” CommandName=”Cancel” />
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID=”btnSave” runat=”server” ValidationGroup=”Add_Inspection” CausesValidation=”True”
OnCommand=”btnAddFeedback_Click” CommandName=”Save” Text=”Save” ForeColor=”#FF0000″></asp:LinkButton>
&nbsp;<asp:LinkButton ID=”btnCancel” runat=”server” CausesValidation=”False” OnCommand=”btnAddFeedback_Click”
CommandName=”Cancel” Text=”Cancel” ForeColor=”#FF0000″></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle ForeColor=”#8C4510″ BackColor=”#F7DFB5″></FooterStyle>
<PagerStyle ForeColor=”#8C4510″ HorizontalAlign=”right”></PagerStyle>
<HeaderStyle ForeColor=”black” BackColor=”#ffcc99″></HeaderStyle>
<RowStyle BackColor=”#f5f5f5″ />
<AlternatingRowStyle BackColor=”#ffffff” />
<PagerSettings FirstPageText=”First” LastPageText=”Last” Mode=”NextPreviousFirstLast”
NextPageText=”Next” PreviousPageText=”Previous” Position=”Top” />
</asp:GridView>

 

************CS Page Coding************

GridViewRow AddNewGridRow;
private void BindGrid()
{
try
{
var InspectionQuestionData = sidb.Divisions.Select(_ => new clsDesignation { DivisionID = _.DivisionId, Name = _.DivisionName}).ToList();
if (InspectionQuestionData.Any())
{
grdInspectionMap.DataSource = InspectionQuestionData;
grdInspectionMap.DataBind();
}
else
{
dt = new DataTable();
dt.Columns.Add(“DivisionID”);
dt.Columns.Add(“Name”, typeof(String));
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = “false”;
dt.Rows[0][1] = “NO RESULT FOUND!”;
grdInspectionMap.DataSource = dt;
grdInspectionMap.DataBind();
}
}
catch (Exception ex)
{
(new MailClass()).SendErrorMail(ex, “ADMIN_InstectionFormMapping::BindGrid”);
}
}

protected void grdInspectionMap_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdInspectionMap.EditIndex = -1;
grdInspectionMap.PageIndex = e.NewPageIndex;
BindGrid();
}

protected void grdInspectionMap_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
SetPagerButtonStates(grdInspectionMap, e.Row, this, “ddlInspectionPageSelector”);
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
int DivisionId = 0;
if (int.TryParse(Convert.ToString(grdInspectionMap.DataKeys[e.Row.RowIndex].Values[“DivisionId”]), out DivisionId))
{

}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
AddNewGridRow = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell th = new TableHeaderCell();
th.ColumnSpan = e.Row.Cells.Count;
if (grdInspectionMap.EditIndex == -1)
{
th.HorizontalAlign = HorizontalAlign.Right;
LinkButton btnAddNew = new LinkButton();
btnAddNew.Text = “Add New”;
btnAddNew.ForeColor = System.Drawing.Color.Red;
btnAddNew.Command += new CommandEventHandler(btnAddFeedback_Click);
btnAddNew.CommandName = “AddNew”;
th.Controls.Add(btnAddNew);
}
AddNewGridRow.Cells.Add(th);
grdInspectionMap.Controls[0].Controls.Add(AddNewGridRow);
e.Row.BackColor = grdInspectionMap.FooterStyle.BackColor;
e.Row.Font.Bold = true;
}

}
protected void ddlInspectionPageSelector_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlPageSelector = (DropDownList)grdInspectionMap.TopPagerRow.FindControl(“ddlInspectionPageSelector”);
grdInspectionMap.PageIndex = ddlPageSelector.SelectedIndex;
BindGrid();
}

protected void grdInspectionMap_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
GridViewRow row = (GridViewRow)grdInspectionMap.Rows[e.RowIndex];
int RowID = Convert.ToInt32(grdInspectionMap.DataKeys[e.RowIndex].Values[“DivisionId”].ToString());
var DeleteInspectionOption = sidb.Divisions.Where(_ => _.DivisionId.Equals(RowID)).ToList();
if (DeleteInspectionOption.Any())
{
sidb.Divisions.DeleteAllOnSubmit(DeleteInspectionOption);
sidb.SubmitChanges();
}
grdInspectionMap.EditIndex = -1;
BindGrid();
}
catch (Exception ex)
{
(new MailClass()).SendErrorMail(ex, “ADMIN_InstectionFormMapping::grdInspectionMap_RowDeleting”);
}
}

protected void grdInspectionMap_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int DivisionId = Convert.ToInt32(grdInspectionMap.DataKeys[e.RowIndex].Values[“DivisionId”].ToString());
string txtName = ((TextBox)grdInspectionMap.Rows[e.RowIndex].FindControl(“txtName”)).Text.Trim();

var EditableInspectionQuestion = sidb.Divisions.Where(_ => _.DivisionId.Equals(DivisionId)).SingleOrDefault();
if (EditableInspectionQuestion != null)
{
EditableInspectionQuestion.DivisionName = txtName;
sidb.SubmitChanges();
}

grdInspectionMap.EditIndex = -1;
BindGrid();
}
protected void grdInspectionMap_RowEditing(object sender, GridViewEditEventArgs e)
{
grdInspectionMap.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void grdInspectionMap_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdInspectionMap.EditIndex = -1;
BindGrid();
}

protected void btnAddFeedback_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == “AddNew”)
{
BindGrid();
grdInspectionMap.FooterRow.Visible = true;
AddNewGridRow.Visible = false;
}
else if (e.CommandName == “Save”)
{
InsertNewFeedBackMapping((TableRow)grdInspectionMap.FooterRow);
}
}

private void InsertNewFeedBackMapping(TableRow htmlTableRow)
{

string txtName = ((TextBox)htmlTableRow.FindControl(“txtName_F”)).Text.Trim();

Division obj = new Division();
obj.DivisionName = txtName;
sidb.Divisions.InsertOnSubmit(obj);
sidb.SubmitChanges();
grdInspectionMap.EditIndex = -1;
BindGrid();
}

 

private void SetPagerButtonStates(GridView gridView, GridViewRow gvPagerRow, Page page, String DropDownName)
{
int pageIndex = gridView.PageIndex;
int pageCount = gridView.PageCount;
Button btnFirst = (Button)gvPagerRow.FindControl(“btnFirst”);
Button btnPrevious = (Button)gvPagerRow.FindControl(“btnPrevious”);
Button btnNext = (Button)gvPagerRow.FindControl(“btnNext”);
Button btnLast = (Button)gvPagerRow.FindControl(“btnLast”);
btnFirst.Enabled = btnPrevious.Enabled = (pageIndex != 0);
btnNext.Enabled = btnLast.Enabled = (pageIndex < (pageCount – 1));
DropDownList ddlPageSelector = (DropDownList)gvPagerRow.FindControl(DropDownName);
ddlPageSelector.Items.Clear();
for (int i = 1; i <= gridView.PageCount; i++)
{
ddlPageSelector.Items.Add(i.ToString());
}
ddlPageSelector.SelectedIndex = pageIndex;
}

class clsDesignation
{
public int DivisionID { get; set; }
public string Name { get; set; }
}

Favicon on asp.net

<html>
  <head>
   <link runat="server" rel="shortcut icon" href="~/favicon.ico" type="image/x-icon" />
    <link runat="server" rel="icon" href="~/favicon.ico" type="image/ico" />
</head>
<body>
</body>
</html>

http://pramadha.com

Gridview, Add, Update, Delete and view option gridview on first gridview column click

<%——————– ASPX Page Coding ————————————–%>
<table width=”100%”>
<tr>
<td align=”center”>

Gridview, Add, Update, Delete and view option gridview on first gridview column click

Goto Page

‘>


runat=”server”>

‘>

‘>

‘>

‘ />  

CommandName=”Delete” />

 

</td>
</tr>
<tr>
<td>

</td>
</tr>
<tr>
<td align=”left”>

Question:

Goto Page

‘ Width=”150px”>

‘>

‘ Width=”150px”>

‘ Width=”150px”>

‘>

‘ Width=”150px”>

‘ />  

CommandName=”Delete” />

 

</td>
</tr>
</table>
<asp:HiddenField ID=”hfInspectionQuestionID” runat=”server” Value=”” />
<%———————————— CS Page Coding—————————————————-%>
LinqDataContext sidb = new LinqDataContext();
DataTable dt = new DataTable();
List<int> lstPriority = new List<int>();
List<clsAnswerType> lstAnswerType = new List<clsAnswerType>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ BindGrid(); }
else
lblQuestionMappingMsg.Text = “”;

}

private List<int> BindPriorityList()
{
lstPriority = new List<int>();
try
{
for (int i = 1; i <= 100; i++)
{
lstPriority.Add(i);
}
}
catch (Exception ex)
{
// throw;
}
return lstPriority;
}

private List<clsAnswerType> GetAnswerTypeList()
{
lstAnswerType = new List<clsAnswerType>();
try
{
lstAnswerType.Add(new clsAnswerType { AnswerType = “Single Line Descriptive”, AnswerValue = “SingleLineDescriptive” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “Multi Line Descriptive”, AnswerValue = “MultiLineDescriptive” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “Yes / No”, AnswerValue = “YesNo” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “Tabular”, AnswerValue = “Tabular” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “GoodAverageBest”, AnswerValue = “GoodAverageBest” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “Heading”, AnswerValue = “Heading” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “Date”, AnswerValue = “Date” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “FromDB”, AnswerValue = “FromDB” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “DivisionName”, AnswerValue = “DivisionName” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “BlockName”, AnswerValue = “BlockName” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “DistrictName”, AnswerValue = “DistrictName” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “ClusterName”, AnswerValue = “ClusterName” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “VillageName”, AnswerValue = “VillageName” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “InspectorName”, AnswerValue = “InspectorName” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “InspectorDesignation”, AnswerValue = “InspectorDesignation” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “InspectorDate”, AnswerValue = “InspectorDate” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “Scale10”, AnswerValue = “Scale10” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “DaysCount”, AnswerValue = “DaysCount” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “MultiRadioList”, AnswerValue = “MultiRadioList” });
lstAnswerType.Add(new clsAnswerType { AnswerType = “SchoolName”, AnswerValue = “SchoolName” });
}
catch (Exception ex)
{
// throw;
}
return lstAnswerType;
}

#region grdInspectionMap Grid
GridViewRow AddNewGridRow;
private void BindGrid()
{
try
{
var InspectionQuestionData = sidb.InspectionQuestionMasters.OrderBy(_ => _.Priority).Select(_ => new clsInspectionQuestionData { InspectionQuestionId = _.InspectionQuestionId, InspectionQuestion = _.InspectionQuestion, Priority = _.Priority, Status = _.Status, AnswerType = _.AnswerType, OptionCount = _.InspectionQuestionOptionMasters.Count() }).ToList();
if (InspectionQuestionData.Any())
{
grdInspectionMap.DataSource = InspectionQuestionData;
grdInspectionMap.DataBind();
}
else
{
dt = new DataTable();
dt.Columns.Add(“InspectionQuestionId”);
dt.Columns.Add(“InspectionQuestion”, typeof(String));
dt.Columns.Add(“Priority”, typeof(String));
dt.Columns.Add(“Status”, typeof(String));
dt.Columns.Add(“AnswerType”, typeof(String));
dt.Columns.Add(“OptionCount”, typeof(String));
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = “false”;
dt.Rows[0][3] = “NO RESULT FOUND!”;
grdInspectionMap.DataSource = dt;
grdInspectionMap.DataBind();
}
}
catch (Exception ex)
{
(new MailClass()).SendErrorMail(ex, “ADMIN_InstectionFormMapping::BindGrid”);
}
}

protected void grdInspectionMap_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string _Priority = Convert.ToString(grdInspectionMap.DataKeys[e.Row.RowIndex].Values[“Priority”]);
string _AnswerType = Convert.ToString(grdInspectionMap.DataKeys[e.Row.RowIndex].Values[“AnswerType”]);
int __Priority = 0;
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
// Bind ddlPriority
DropDownList ddlPriority = (DropDownList)e.Row.FindControl(“ddlPriority”);
ddlPriority.DataSource = BindPriorityList();
ddlPriority.DataBind();
if (int.TryParse(_Priority, out __Priority))
ddlPriority.SelectedValue = Convert.ToString(__Priority);

// Bind ddlAnswerType
DropDownList ddlAnswerType = (DropDownList)e.Row.FindControl(“ddlAnswerType”);
ddlAnswerType.DataSource = GetAnswerTypeList();
ddlAnswerType.DataTextField = “AnswerType”;
ddlAnswerType.DataValueField = “AnswerValue”;
ddlAnswerType.DataBind();
if (!string.IsNullOrEmpty(_AnswerType))
ddlAnswerType.SelectedValue = Convert.ToString(_AnswerType);

}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlPriority = (DropDownList)e.Row.FindControl(“ddlPriority”);
ddlPriority.DataSource = BindPriorityList();
ddlPriority.DataBind();

// Bind ddlAnswerType
DropDownList ddlAnswerType = (DropDownList)e.Row.FindControl(“ddlAnswerType”);
ddlAnswerType.DataSource = GetAnswerTypeList();
ddlAnswerType.DataTextField = “AnswerType”;
ddlAnswerType.DataValueField = “AnswerValue”;
ddlAnswerType.DataBind();
}
}

protected void grdInspectionMap_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdInspectionMap.EditIndex = -1;
grdInspectionMap.PageIndex = e.NewPageIndex;
BindGrid();
}

protected void grdInspectionMap_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
SetPagerButtonStates(grdInspectionMap, e.Row, this, “ddlInspectionPageSelector”);
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
int InspectionQuestionId = 0;
if (int.TryParse(Convert.ToString(grdInspectionMap.DataKeys[e.Row.RowIndex].Values[“InspectionQuestionId”]), out InspectionQuestionId))
{
string AnswerType = Convert.ToString(grdInspectionMap.DataKeys[e.Row.RowIndex].Values[“AnswerType”]);
int OptionCount = Convert.ToInt32(grdInspectionMap.DataKeys[e.Row.RowIndex].Values[“OptionCount”].ToString());
LinkButton lnkbtnOption = (LinkButton)e.Row.FindControl(“lnkbtnOption”);
if (lnkbtnOption != null)
{
if (OptionCount > 0 && AnswerType == “Tabular”)
lnkbtnOption.Text = “Update Option”;
else if (AnswerType == “Tabular”)
lnkbtnOption.Text = “Add Option”;
else
lnkbtnOption.Text = “”;
lnkbtnOption.Command += new CommandEventHandler(lnkbtnOption_Click);
lnkbtnOption.CommandArgument = InspectionQuestionId.ToString();
lnkbtnOption.CommandName = “AddNewOption”;
lnkbtnOption.Attributes.Add(“style”, “text-decoration: none;”);
}
}
else
{
LinkButton lnkbtnOption = (LinkButton)e.Row.FindControl(“lnkbtnOption”);
if (lnkbtnOption != null)
lnkbtnOption.Text = “”;
}
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
AddNewGridRow = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell th = new TableHeaderCell();
th.ColumnSpan = e.Row.Cells.Count;
if (grdInspectionMap.EditIndex == -1)
{
th.HorizontalAlign = HorizontalAlign.Right;
LinkButton btnAddNew = new LinkButton();
btnAddNew.Text = “Add New”;
btnAddNew.ForeColor = System.Drawing.Color.Red;
btnAddNew.Command += new CommandEventHandler(btnAddFeedback_Click);
btnAddNew.CommandName = “AddNew”;
th.Controls.Add(btnAddNew);
}
AddNewGridRow.Cells.Add(th);
grdInspectionMap.Controls[0].Controls.Add(AddNewGridRow);
e.Row.BackColor = grdInspectionMap.FooterStyle.BackColor;
e.Row.Font.Bold = true;
}

}

protected void lnkbtnOption_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == “AddNewOption”)
{
divQuestionOptionsMapping.Visible = true;
hfInspectionQuestionID.Value = e.CommandArgument.ToString();
BindgrdQuestionOptionsMapping();
}
}

protected void ddlInspectionPageSelector_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlPageSelector = (DropDownList)grdInspectionMap.TopPagerRow.FindControl(“ddlInspectionPageSelector”);
grdInspectionMap.PageIndex = ddlPageSelector.SelectedIndex;
BindGrid();
}

protected void grdInspectionMap_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
GridViewRow row = (GridViewRow)grdInspectionMap.Rows[e.RowIndex];
int RowID = Convert.ToInt32(grdInspectionMap.DataKeys[e.RowIndex].Values[“InspectionQuestionId”].ToString());
var DeleteInspectionOption = sidb.InspectionQuestionOptionMasters.Where(_ => _.InspectionQuestionId.Equals(RowID)).ToList();
if (DeleteInspectionOption.Any())
{
sidb.InspectionQuestionOptionMasters.DeleteAllOnSubmit(DeleteInspectionOption);
sidb.SubmitChanges();
}
var DeleteInspectionQuestion = sidb.InspectionQuestionMasters.Where(_ => _.InspectionQuestionId.Equals(RowID)).ToList();
if (DeleteInspectionQuestion.Any())
{
sidb.InspectionQuestionMasters.DeleteAllOnSubmit(DeleteInspectionQuestion);
sidb.SubmitChanges();
}
grdInspectionMap.EditIndex = -1;
BindGrid();
divQuestionOptionsMapping.Visible = false;
}
catch (Exception ex)
{
(new MailClass()).SendErrorMail(ex, “ADMIN_InstectionFormMapping::grdInspectionMap_RowDeleting”);
}
}

protected void grdInspectionMap_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int InspectionQuestionId = Convert.ToInt32(grdInspectionMap.DataKeys[e.RowIndex].Values[“InspectionQuestionId”].ToString());
string txtInspectionQuestion = ((TextBox)grdInspectionMap.Rows[e.RowIndex].FindControl(“txtInspectionQuestion”)).Text.Trim();
string Priority = ((DropDownList)grdInspectionMap.Rows[e.RowIndex].FindControl(“ddlPriority”)).Text.Trim();
string AnswerType = ((DropDownList)grdInspectionMap.Rows[e.RowIndex].FindControl(“ddlAnswerType”)).SelectedItem.Value;
bool ActiveStatus = false;
string Status = ((DropDownList)grdInspectionMap.Rows[e.RowIndex].FindControl(“ddlStatus”)).SelectedValue;
if (Status == “1”)
ActiveStatus = true;

var EditableInspectionQuestion = sidb.InspectionQuestionMasters.Where(_ => _.InspectionQuestionId.Equals(InspectionQuestionId)).SingleOrDefault();
if (EditableInspectionQuestion != null)
{
EditableInspectionQuestion.InspectionQuestion = txtInspectionQuestion;
EditableInspectionQuestion.Priority = int.Parse(Priority == “” ? “0” : Priority);
EditableInspectionQuestion.AnswerType = AnswerType;
EditableInspectionQuestion.Status = ActiveStatus;
sidb.SubmitChanges();
}

grdInspectionMap.EditIndex = -1;
BindGrid();
divQuestionOptionsMapping.Visible = false;
}
protected void grdInspectionMap_RowEditing(object sender, GridViewEditEventArgs e)
{
grdInspectionMap.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void grdInspectionMap_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdInspectionMap.EditIndex = -1;
BindGrid();
}

protected void btnAddFeedback_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == “AddNew”)
{
BindGrid();
grdInspectionMap.FooterRow.Visible = true;
AddNewGridRow.Visible = false;
}
else if (e.CommandName == “Save”)
{
InsertNewFeedBackMapping((TableRow)grdInspectionMap.FooterRow);
}
}

private void InsertNewFeedBackMapping(TableRow htmlTableRow)
{

string txtInspectionQuestion = ((TextBox)htmlTableRow.FindControl(“txtInspectionQuestion”)).Text.Trim();
string Priority = ((DropDownList)htmlTableRow.FindControl(“ddlPriority”)).SelectedValue;
string AnswerType = ((DropDownList)htmlTableRow.FindControl(“ddlAnswerType”)).SelectedValue;
bool ActiveStatus = false;
string Status = ((DropDownList)htmlTableRow.FindControl(“ddlStatus”)).SelectedValue;
if (Status == “1”)
ActiveStatus = true;

InspectionQuestionMaster obj = new InspectionQuestionMaster();
obj.InspectionQuestion = txtInspectionQuestion;
obj.Priority = int.Parse(Priority);
obj.AnswerType = AnswerType;
obj.Status = ActiveStatus;
sidb.InspectionQuestionMasters.InsertOnSubmit(obj);
sidb.SubmitChanges();
grdInspectionMap.EditIndex = -1;
BindGrid();
}

#endregion

#region Bind Question Answer
GridViewRow AddNewQuestionOptionsMappingRow;
private void BindgrdQuestionOptionsMapping()
{
int InspectionQuestionId = 0;
grdQuestionOptionsMapping.Visible = false;
divQuestionOptionsMapping.Visible = true;
if (!string.IsNullOrEmpty(hfInspectionQuestionID.Value))
{
InspectionQuestionId = int.Parse(hfInspectionQuestionID.Value);
var FeedbackQueestion = sidb.InspectionQuestionMasters.Where(_ => _.InspectionQuestionId.Equals(InspectionQuestionId)).ToList();
if (FeedbackQueestion.Any())
{
lblQuestion.Text = FeedbackQueestion.First().InspectionQuestion;
}
}
else
return;

try
{
grdQuestionOptionsMapping.Visible = true;

var InspectionTabularOption = sidb.InspectionQuestionOptionMasters.Where(_ => _.InspectionQuestionId.Equals(InspectionQuestionId)).ToList();
if (InspectionTabularOption.Any())
{
grdQuestionOptionsMapping.DataSource = InspectionTabularOption;
grdQuestionOptionsMapping.DataBind();
}
else
{
DataTable dt1 = new DataTable();
dt1.Columns.Add(“InspectionQuestionOptionId”, typeof(String));
dt1.Columns.Add(“InspectionQuestionId”, typeof(String));
dt1.Columns.Add(“Row”, typeof(String));
dt1.Columns.Add(“Col”, typeof(String));
dt1.Rows.Add(dt1.NewRow());
dt1.Rows[0][0] = “false”;
dt1.Rows[0][1] = “NO RESULT FOUND!”;
grdQuestionOptionsMapping.DataSource = dt1;
grdQuestionOptionsMapping.DataBind();
}
}
catch (Exception ex)
{
(new MailClass()).SendErrorMail(ex, “ADMIN_InstectionFormMapping::BindgrdQuestionOptionsMapping”);
}
}
protected void grdQuestionOptionsMapping_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdQuestionOptionsMapping.EditIndex = -1;
grdQuestionOptionsMapping.PageIndex = e.NewPageIndex;
BindgrdQuestionOptionsMapping();
}
protected void grdQuestionOptionsMapping_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
SetPagerButtonStates(grdQuestionOptionsMapping, e.Row, this, “ddlQuestionOptionsMappingPageSelector”);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
AddNewQuestionOptionsMappingRow = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell th = new TableHeaderCell();
th.ColumnSpan = e.Row.Cells.Count;
if (grdQuestionOptionsMapping.EditIndex == -1)
{
th.HorizontalAlign = HorizontalAlign.Right;
LinkButton btnAddNew = new LinkButton();
btnAddNew.Text = “Add New”;
btnAddNew.ForeColor = System.Drawing.Color.Red;
btnAddNew.Command += new CommandEventHandler(btnAddNewTemplateITKMapping_Click);
btnAddNew.CommandName = “AddNew”;
th.Controls.Add(btnAddNew);
}
AddNewQuestionOptionsMappingRow.Cells.Add(th);
grdQuestionOptionsMapping.Controls[0].Controls.Add(AddNewQuestionOptionsMappingRow);
e.Row.BackColor = grdQuestionOptionsMapping.FooterStyle.BackColor;
e.Row.Font.Bold = true;
}

}
protected void ddlQuestionOptionsMappingPageSelector_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlPageSelector = (DropDownList)grdQuestionOptionsMapping.TopPagerRow.FindControl(“ddlQuestionOptionsMappingPageSelector”);
grdQuestionOptionsMapping.PageIndex = ddlPageSelector.SelectedIndex;
BindgrdQuestionOptionsMapping();
}
protected void grdQuestionOptionsMapping_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
int InspectionQuestionId = Convert.ToInt32(grdQuestionOptionsMapping.DataKeys[e.RowIndex].Values[“InspectionQuestionId”].ToString());
int InspectionQuestionOptionId = Convert.ToInt32(grdQuestionOptionsMapping.DataKeys[e.RowIndex].Values[“InspectionQuestionOptionId”].ToString());

var DeleteOption = sidb.InspectionQuestionOptionMasters.Where(_ => _.InspectionQuestionId.Equals(InspectionQuestionId) && _.InspectionQuestionOptionId.Equals(InspectionQuestionOptionId)).ToList();
if (DeleteOption.Any())
{
sidb.InspectionQuestionOptionMasters.DeleteAllOnSubmit(DeleteOption);
sidb.SubmitChanges();
}

// lblQuestionMappingMsg.Text = “Option Deleted Successfully”;
grdQuestionOptionsMapping.EditIndex = -1;
BindgrdQuestionOptionsMapping();
}
catch (Exception ex)
{
(new MailClass()).SendErrorMail(ex, “ADMIN_InstectionFormMapping::grdQuestionOptionsMapping_RowDeleting”);
}
}

protected void grdQuestionOptionsMapping_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int InspectionQuestionId = Convert.ToInt32(grdQuestionOptionsMapping.DataKeys[e.RowIndex].Values[“InspectionQuestionId”].ToString());
int InspectionQuestionOptionId = Convert.ToInt32(grdQuestionOptionsMapping.DataKeys[e.RowIndex].Values[“InspectionQuestionOptionId”].ToString());
string txtQuistionRow = ((TextBox)grdQuestionOptionsMapping.Rows[e.RowIndex].FindControl(“txtQuistionRow”)).Text.Trim();
string txtQuistionCol = ((TextBox)grdQuestionOptionsMapping.Rows[e.RowIndex].FindControl(“txtQuistionCol”)).Text.Trim();

if (!sidb.InspectionQuestionOptionMasters.Where(_ => _.InspectionQuestionId.Equals(InspectionQuestionId) && _.InspectionQuestionOptionId.Equals(InspectionQuestionOptionId) && _.Row.Equals(txtQuistionRow) && _.Col.Equals(txtQuistionCol)).Any())
{
var lstInspectionOptionUpdates = sidb.InspectionQuestionOptionMasters.Where(_ => _.InspectionQuestionId.Equals(InspectionQuestionId) && _.InspectionQuestionOptionId.Equals(InspectionQuestionOptionId)).ToList();
lstInspectionOptionUpdates.First().Row = txtQuistionRow;
lstInspectionOptionUpdates.First().Col = txtQuistionCol;
sidb.SubmitChanges();
}
grdQuestionOptionsMapping.EditIndex = -1;
BindgrdQuestionOptionsMapping();
}
protected void grdQuestionOptionsMapping_RowEditing(object sender, GridViewEditEventArgs e)
{
grdQuestionOptionsMapping.EditIndex = e.NewEditIndex;
BindgrdQuestionOptionsMapping();
}
protected void grdQuestionOptionsMapping_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdQuestionOptionsMapping.EditIndex = -1;
BindgrdQuestionOptionsMapping();
}

protected void btnAddNewTemplateITKMapping_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == “AddNew”)
{
BindgrdQuestionOptionsMapping();
grdQuestionOptionsMapping.FooterRow.Visible = true;
AddNewQuestionOptionsMappingRow.Visible = false;
}
else if (e.CommandName == “Save”)
{
InsertNewTemplateDepITKMap((TableRow)grdQuestionOptionsMapping.FooterRow);
}
}

private void InsertNewTemplateDepITKMap(TableRow htmlTableRow)
{
int InspectionQuestionId = 0;
if (!string.IsNullOrEmpty(hfInspectionQuestionID.Value))
{
InspectionQuestionId = int.Parse(hfInspectionQuestionID.Value);
string txtQuistionRow = ((TextBox)htmlTableRow.FindControl(“txtQuistionRow”)).Text.Trim();
string txtQuistionCol = ((TextBox)htmlTableRow.FindControl(“txtQuistionCol”)).Text.Trim();

InspectionQuestionOptionMaster obj = new InspectionQuestionOptionMaster();
obj.InspectionQuestionId = InspectionQuestionId;
obj.Row = txtQuistionRow;
obj.Col = txtQuistionCol;
sidb.InspectionQuestionOptionMasters.InsertOnSubmit(obj);
sidb.SubmitChanges();
// lblQuestionMappingMsg.Text = “Option added successfully”;
BindGrid();

}
else
return;
grdQuestionOptionsMapping.EditIndex = -1;
BindgrdQuestionOptionsMapping();
}
#endregion
private void SetPagerButtonStates(GridView gridView, GridViewRow gvPagerRow, Page page, String DropDownName)
{
int pageIndex = gridView.PageIndex;
int pageCount = gridView.PageCount;
Button btnFirst = (Button)gvPagerRow.FindControl(“btnFirst”);
Button btnPrevious = (Button)gvPagerRow.FindControl(“btnPrevious”);
Button btnNext = (Button)gvPagerRow.FindControl(“btnNext”);
Button btnLast = (Button)gvPagerRow.FindControl(“btnLast”);
btnFirst.Enabled = btnPrevious.Enabled = (pageIndex != 0);
btnNext.Enabled = btnLast.Enabled = (pageIndex < (pageCount – 1));
DropDownList ddlPageSelector = (DropDownList)gvPagerRow.FindControl(DropDownName);
ddlPageSelector.Items.Clear();
for (int i = 1; i <= gridView.PageCount; i++)
{
ddlPageSelector.Items.Add(i.ToString());
}
ddlPageSelector.SelectedIndex = pageIndex;
}

class clsInspectionQuestionData
{
public int InspectionQuestionId { get; set; }
public string InspectionQuestion { get; set; }
public int? Priority { get; set; }
public bool? Status { get; set; }
public string AnswerType { get; set; }
public int OptionCount { get; set; }
}

class clsAnswerType
{
public string AnswerType { get; set; }
public string AnswerValue { get; set; }
}