Tuesday, July 9, 2013

How to call server side function in Java script?


In the ASP.NET any server side event fired means the entire page will refresh or resend to server and return back. In the form if you submit the one simple button event the full page will get refresh.  Then how to avoid this or how to make my page to update data without refresh   ?. For that we have one option called IcallbackEventHandler Interface. Using this we can achieve without post back or refresh the page.
First we need in add IcallbackEventHandler  interface in our aspx.cs page. The below code I have added IcallbackEventHandler in my main partial class.

public partial class Default : System.Web.UI.Page, System.Web.UI.IcallbackEventHandler
{
 protected void Page_Load(object sender, EventArgs e)
        {
            If (!IsPostBack)
            {

            }
//Register the call server function in C# code.
            ClientScriptManager cm = Page.ClientScript;
            String cbReference = cm.GetCallbackEventReference(this, "arg","ReceiveServerData", "");
            String callbackScript = "function CallServer(arg, context) {" +cbReference + "; }";
            cm.RegisterClientScriptBlock(this.GetType(),"CallServer", callbackScript, true);
        }


 //Java script will call this event
        public void RaiseCallbackEvent(String eventArgument)
        {

        }

//return the string to javascript function
        public string GetCallbackResult()
        {           
            string result = "JSON OBJECT";            
            return result;
        }


}
In the Default.aspx page you need to add latest Jquery script first. In the java script function we need to create below function for call the server side function in the Java script.

<script src="Scripts/jquery.min.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
  function CallServerData() {
                                   //Call the server side function
                                    CallServer("", "");
                          }

//Returning from the server side function.
function ReceiveServerData (arg, context) {
 
            var splitarg = arg.toString().split("~");
 
            var obj = eval(splitarg[0]);
 
 
            updateReports(obj, '#Reports');
        }
 
//Your custom code to update in the table.
function updateReports(data, id) {
            var obj = $(id + ' > tbody');
            obj.empty();
            var mystyle = "style='font-weight:bold'; font-size:11px;color:#000;";
 
 
            for (var j = 0; j < data.length - 1; j++) {  
                    obj.append("
"
 + data[j + 1][keys[i]] + "
" + str + "" + data[j][keys[i]] + "");
                }
            }
 
        }
</script>
In the HTML code to call the server side event like below
<asp:Button ID="btnGetData" runat="server" Text="Save" OnClientClick="return CallServerData();" />
The server side function returns the JSON string. So we need to fill that information to the HTML Table. The UpdateReports Javascript function will load JSON string to simple HTML Table.
 
<table width="900px" cellpadding="5" cellspacing="0" align="center" id="Reports">
                <thead>
                    <tr>
                        <th>
                             ID
                        </th>
                        <th>
                           Name
                        </th>
                        <th>
                            Class
                        </th>
                        <th>
                        </th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>

The above code will help you to get know Call server side function in the java script easily.

Wednesday, July 3, 2013

Select multiple fields in Generic List using LINQ query





The below code will explain the simple LINQ Query with multiple select column name.  Here I used to filter records from lstValue with ID have “100”.  The filter Linq Query will return the ID and Name values only. It will omit the Class in the result.

 public class KeyValue
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
    }

//Declaration of KeyValue Class
 public List<KeyValue> lstValue { get; set; }


// Filter in LINQ Query
var Result = lstValue.Select(c => new { c.ID, c. Name, c.Class });

 
 
            var result = from a in lstValue
                         where a.ID == "100"
                         select new { a.ID, a.Name };
 
 
Assign the Generic List values to asp.net grid view.
//grdDetails – Datagrid (Assign the value to Grid)
grdDetails.DataSource = result.ToList();




Friday, June 28, 2013

Substring in SQL or Split the column value in SQL




Some times in SQL we need to use substring or split the selected value. For that the below methods we can use to split the string in the SQL Server. The Below table we have first name+ last name separated with | simple.  The below query I wrote to get First name from the names column. I used two methods to get the results.

SELECT * FROM [dbo].[MyTable]
ID            Names
1              vijay | sekar
2              antony | amal
3              yuvan | karthick


Splitting string using SUBSTRING options in SQL:
 Syntax:   SUBSTRING ( expression ,start , length )

Query:
SELECT SUBSTRING(Names,0,CHARINDEX('|',Names)) AS Result FROM MyTable

Result
vijay
antony
yuvan


Splitting string using LEFT, RIHGHT characters in SQL:
Syntax:  LEFT ( character_expression , integer_expression )
Query:
SELECT Names,(LEFT(Names,CHARINDEX('|',Names) -1)) AS LeftResult,
(RIGHT(Names,CHARINDEX('|',Names) -1)) AS RightResult FROM MyTable

Result
Names                                  LeftResult           RightResult
vijay | sekar                       vijay                        sekar
antony | amal                    antony                 | amal
yuvan | karthick               yuvan                    rthick

Based on your requirement you can use the splitting string option in SQL. I hope this will help you to solve your split string in SQL.


How to get string length in SQL?
The below query will help you to get the single columns string length. This is the one of easy way to get string count in SQL.
SELECT Names,LEN(Names) AS NameCount FROM MyTable

Result:
Names                                  NameCount
vijay | sekar                       13
antony | amal                    13
yuvan | karthick               16

Monday, June 24, 2013

Writing Single select query for multiple filter in sql where condition

In the stored produces some time we need to use multiple filters. Some have value and some don’t have any value. For that we usually write different IF condition to execute the select statement. In SQL have simple way to use one select statement to use multiple where condition or multiple filter.




CREATE Procedure [dbo].[YourSP]
@Empid varchar(50),
@Edept varchar(50)

As
Begin


IF @ Empid = ''
Set @ Empid = null

IF @ Edept= ''
Set @ Edept = null


select * from Employee Where ((@Empid is null or EmpidID = @ Empid) and (@Edept is null or EDept=@Edept))

End