Thursday, January 12, 2012

How to find Current GridViewRow in TextBox text change event

Hello Friends,

Now I am going to explain to find a GridViewRow in TextBox TextChanged Event inside GridView

Some times we had a TextBox or CheckBox or any other control inside GridView and we have to find the GridViewRow when the TextChanged or CheckedChanged or concerned Event fires

The following code show us how to do that

protected void txtTest_TextChanged(object sender, EventArgs e)
    {
       // code to find text box
        TextBox txtTextBox = ((TextBox)(sender));
        // code to find GridViewRow using naming container (bubble Event)
        GridViewRow grdRow = ((GridViewRow)(txtTextBox.NamingContainer));
  
       // code to find Label inside GridViewRow
        Label lblTest = grdRow .FindControl("lblTest") as Label;
      
    }


in same way you can find gridview in any event inside gridview

  protected void chkApproved_CheckedChanged(object sender, EventArgs e)
    {
        try
        {
            CheckBox chk = ((CheckBox)(sender));
            GridViewRow gv1 = ((GridViewRow)(chk.NamingContainer));
        }
        catch (Exception ex)
        {
            // handle your exception
        }
     }    

Finding DataListItem in ItemCommand Event of DataList

Dear Friends,

In this post I am going to show how to get the DataLIstItem in ItemCommand Event of a DataList

Here is code for this purpose

protected void dlTest_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "FindPersonDetail")
{
DataListItem dlItem = (DataListItem)((Control)e.CommandSource).Parent;
string Empname = ((Label)(item.Controls[1].FindControl("lblEmpName"))).Text;
}

In this code, if the command name is FindPersonDetail then I find DataLIstItem of Datalist of the selected Item using Bubble Event. then I find My Label using that DatalistItem.
You can bypass if condition if you not require that.

Hope this code help you...

Wednesday, January 11, 2012

return a table from Function in Oracle

Hello Guys,

Today I am going to explain how to write a function in oracle which return a table as result set.

We have to follow 3 steps to do so.

Step 1: We are going to create a object type that contains the fields that are going to be returned from function.

CREATE OR REPLACE TYPE emp_type as object 
(
id                     number,
name               varchar2(30),
designation      varchar2(50),
salary              number(16,4)
);
/
execute this statement

Step 2: Now we create a nested table type from above Type


CREATE OR REPLACE TYPE emp_type_Table as table of  emp_type;

execute this statement

Step 3:  So It's time to do our real work i.e. create a function


CREATE OR REPLACE FUNCTION fn_get_emp_salary (Pyear  IN  number,
                                                P id   IN number)
   RETURN emp_type_Table
AS
   v_ret   emp_type_Table ;
BEGIN
   SELECT   CAST (
               MULTISET(
                                      -- your query goes here
                                       select id, name, designation, salary from emp 
                                       where id=Pid and year=Pyear
                              )
     INTO   v_ret
     FROM   DUAL;

   RETURN v_ret;
END fn_get_emp_salary ;
/

execute this function.


Now your function is ready and you can call it as shoen below


select * from table( fn_get_emp_salary(2012,25))

I call the function by sending parameter year as 2012 and id as 25


Hope this post help you in writing function in oracle.........

Sunday, May 15, 2011

Taj trip at This weekend

Dear all, 

This weekend I had to go to Gwalior for some reason on saturday and I planned to return on Sunday from there.
I went there with my wife and nephew (Rohan). 

On my return journey, I had a lot of time at Agra and Made my mind for a Taj Mahal Trip.

I uploaded them to my picassa album titled Taj Trip on 15 may

Wednesday, November 24, 2010

Calling server side code from client side

Today, I am going to explain you how to call a server side function/code from client side (JavaScript).

Ajax provide us the facility to use server side code from client side without postback.

You can call server side code by using webservice(.asmx) or using page methods in same aspx page.
The page methods are rendered as inline javascript.

If you are using asp.net 2.0 then you require
ASP.NET AJAX extensions for ASP.NET 2.0

you website need to be ajax enabled for using this functionality.

I just explaining this functionality by a simple Example

add a javascript file naming script.js in you website

and write below methos in your javascript file

function CallMe(src,dest)
 {   
     var ctrl = document.getElementById(src);
     // call server side method
     PageMethods.GetGroupName(ctrl.value, CallSuccess, CallFailed, dest);
 }

 // set the destination textbox value with the ContactName
 function CallSuccess(res, destCtrl)
 {   
     var dest = document.getElementById(destCtrl);
     dest.value = res;
 }

 // alert message on some failure
 function CallFailed(res, destCtrl)
 {
     alert(res.get_message());
 }



now move on to your aspx page
add  a reference of your javascript file in body tag of page


and paste below code 


<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
    <div>
        <asp:Label ID="lbl_Group_Code" runat="server" Text="Group Code"></asp:Label>
        <asp:TextBox ID="txt_Group_Code" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="txt_Group_Name" runat="server" ReadOnly="True"></asp:TextBox><br />
    </div>
    </form>





also remember to check following property of script manager in script manager tag
 EnablePageMethods="true" 
this property enables your code to use server side webmethods


now come to your aspx.cs page


write a public static method here as follows


   public static string GetGroupName(string GRP_CODE)
        {
            if (GRP_CODE == null || GRP_CODE.Length == 0)
                return String.Empty;
            OracleConnection conn = null;
            try
            {
                string connection = ConfigurationManager.ConnectionStrings["csTextile"].ConnectionString;
                conn = new OracleConnection(connection);
                string Oracle = "Select GRP_NAME from FA_GRP_MST where GRP_CODE = :GRP_CODE";
                OracleCommand cmd = new OracleCommand(Oracle, conn);
                cmd.Parameters.Add(":GRP_CODE", OracleType.VarChar).Value = GRP_CODE;
                conn.Open();
                string GRP_Name = Convert.ToString(cmd.ExecuteScalar());
                return GRP_Name;
            }
            catch (OracleException ex)
            {
                return "error";
            }
            finally
            {
                conn.Close();
            }
        }





Method should be public static


I use Oracle as database in my website so I use OracleConnection and Command etc...
replace Oracle with sql in your app.


now add following line before your method


   [System.Web.Services.WebMethod]


like this


   [System.Web.Services.WebMethod]
        public static string GetGroupName(string GRP_CODE)
        {
            if (GRP_CODE == null || GRP_CODE.Length == 0)
                return String.Empty;
            OracleConnection conn = null;
            try
            {
                string connection = ConfigurationManager.ConnectionStrings["csTextile"].ConnectionString;
                conn = new OracleConnection(connection);
                string Oracle = "Select GRP_NAME from FA_GRP_MST where GRP_CODE = :GRP_CODE";
                OracleCommand cmd = new OracleCommand(Oracle, conn);
                cmd.Parameters.Add(":GRP_CODE", OracleType.VarChar).Value = GRP_CODE;
                conn.Open();
                string GRP_Name = Convert.ToString(cmd.ExecuteScalar());
                return GRP_Name;
            }
            catch (OracleException ex)
            {
                return "error";
            }
            finally
            {
                conn.Close();
            }
        }




now add the following code in page load event


if (!Page.IsPostBack)
            {
                txt_Group_Code.Attributes.Add("onblur", "javascript:CallMe('" + txt_Group_Code.ClientID + "', '" + txt_Group_Name.ClientID + "')");
                txt_Group_Code.Attributes.Add("onchange", "javascript:CallMe('" + txt_Group_Code.ClientID + "', '" + txt_Group_Name.ClientID + "')");
            }

now test your code

write if any problem occurs

have a happy coding