Tuesday, September 6, 2011

Get values of a check box to do a command in for loop


protected void Btn_ApproveChkd_Click(object sender, EventArgs e)
{
try
{
int itemChecked = 0;
foreach (GridViewRow Gitem in grdDispatchGrid.Rows)
{               
CheckBox ChkGrid = (CheckBox)Gitem.FindControl("ChkApprove");
if (ChkGrid.Checked == true)
{
itemChecked = 1;
break;
}
}
if (itemChecked == 1)

{
foreach (GridViewRow Gitem in grdDispatchGrid.Rows)

{                   
int _LeaveId = Convert.ToInt32(grdDispatchGrid.DataKeys[Gitem.RowIndex].Values["leave_id"].ToString());
con.Open();

string Qry = "ReqLeaveApproved";

SqlCommand cmd = new SqlCommand(Qry, con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@Emp_Id", SqlDbType.NChar);

cmd.Parameters["@Emp_Id"].Value = Session["_EmpId"];

cmd.Parameters.Add("@leave_id", SqlDbType.Int);

cmd.Parameters["@leave_id"].Value = _LeaveId;

cmd.Parameters.Add("@Status", SqlDbType.Int);

cmd.Parameters["@Status"].Value = "2";

cmd.ExecuteNonQuery();

con.Close();

}

Response.Redirect(
"FrmAdminApproval.aspx");

}

else if (itemChecked == 0)

{

}

}

catch (Exception Ex)

{

}

}

Javascript Funtion to Select All the Check Box in a Column of a Grid


<script type="text/javascript" language="javascript">
function SelectAll(CheckBox) {

// Give the Grid ID in the below line
TotalChkBx = parseInt('<%= this.grdDispatchGrid.Rows.Count %>');

var TargetBaseControl = document.getElementById('<%= this.grdDispatchGrid.ClientID %>');

// Give the ColumnId of a grid which must be checked

var TargetChildControl = "ChkApporove";

var Inputs = TargetBaseControl.getElementsByTagName("input");

for (var iCount = 0; iCount < Inputs.length; ++iCount)
{

if (Inputs[iCount].type == 'checkbox' && Inputs[iCount].id.indexOf(TargetChildControl, 0) >= 0)

Inputs[iCount].checked = CheckBox.checked;
}

}   
</script>


<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChkApporove" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" Text="All" onclick="SelectAll(this);" />
</HeaderTemplate>
</asp:TemplateField>

Write Data to A File Using FileStream and StreamWriter


using System.IO;

   private void Write2File(string msg, string filePath)
   {
      FileStream fs = 
new FileStream(filePath, FileMode.Append);
      StreamWriter sw = 
new StreamWriter(fs);
      sw.WriteLine(msg);
      sw.Flush();
      sw.Close();
      fs.Close();
   }

Asp.net Complete Tutorial

Asp.net Complete Tutorial

Grid on row Edit and Update ...




protected void GridViewEmpAttendance_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewEmpAttendance.EditIndex = e.NewEditIndex;
            SendEmpAndCenterToGetAttendance(dpd_CenterName.SelectedItem.Text.ToString(), dpd_emp.SelectedItem.Text.ToString());
        }

        protected void GridViewEmpAttendance_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewEmpAttendance.EditIndex = -1;
            SendEmpAndCenterToGetAttendance(dpd_CenterName.SelectedItem.Text.ToString(), dpd_emp.SelectedItem.Text.ToString());
        }

        protected void GridViewEmpAttendance_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label _ecode = (Label)GridViewEmpAttendance.Rows[e.RowIndex].FindControl("lbl_ecode");
            Label _Date = (Label)GridViewEmpAttendance.Rows[e.RowIndex].FindControl("lbl_date");
            DropDownList _EmpStatus = (DropDownList)GridViewEmpAttendance.Rows[e.RowIndex].FindControl("DStatusEdit");
            string ecode = _ecode.Text.ToString();
            string status = _EmpStatus.SelectedValue.ToString();
            string dat = _Date.Text.ToString();
            MtdUpdateAttendance(ecode, dat, status);
            GridViewEmpAttendance.EditIndex = -1;
            SendEmpAndCenterToGetAttendance(dpd_CenterName.SelectedItem.Text.ToString(), dpd_emp.SelectedItem.Text.ToString());
        }

        protected void GridViewEmpAttendance_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                DropDownList DDLStatus = (DropDownList)e.Row.FindControl("DStatusEdit");
                DDLStatus.DataSource = StatusBind();
                DDLStatus.DataTextField = "Status";
                DDLStatus.DataValueField = "Status";
                DDLStatus.DataBind();
                DDLStatus.Items.Insert(0, new ListItem("Select", "0"));
                DDLStatus.SelectedValue = Convert.ToString(GridViewEmpAttendance.DataKeys[e.Row.RowIndex]["status"].ToString());
            }
        }

Execute Stored Procedure with Parameters





protected void CallSPwithParams(string passedparameter)
{
using (SqlConnection sqlcon = new SqlConnection(dbconn))
 {
  string updatequerry = "StoredProcedureName";
  SqlCommand cmd = new SqlCommand(updatequerry, sqlcon);
  cmd.CommandType = CommandType.StoredProcedure;
  cmd.Parameters.AddWithValue("@sqlparam", passedparameter );
  sqlcon.Open();
  cmd.ExecuteNonQuery();
  sqlcon.Close();
               
  }
}