-->

Friday, April 11, 2014

How to remove last character from the string in ASP.NET

If you want to remove last character from the string, you can use Remove method of the StringBuilder class.
In this method, you must specify the length of the string also specify how many character remove from the string. Lets take an example , we take a StringBuilder class object, append this object with some text. After assigning some string value into object, you can remove characters from the string object.
<form id="form1" runat="server">
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Click"
            Width="80px" />
   
    <asp:Label ID="Label1" runat="server" BackColor="#FFFF66" Text="Label"></asp:Label>
    </form>

Code behind code-


 protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder stringA = new StringBuilder();
        stringA.Append("it is a text. ");
        stringA.Append("it is another text.");

        Label1.Text = stringA.ToString();

        stringA.Remove(stringA.Length - 1, 1);

        Label1.Text += "<br /><br />after removing last character from stringbuilder....<br />";
        Label1.Text += stringA.ToString();
    }


Output-



Thursday, April 10, 2014

How to use LINQ Join operator in ASP.NET

Introduction

The Join operators in LINQ are used to join objects in one data source with objects that share a common attribute in another data source. The Join operators provided in LINQ are Join and GroupJoin. The Join clause implements an inner join, which is the type of join in which only those objects that have a match in other data set are returned. The GroupJoin clause joins two sequence based on a key selector functions and groups the results. The syntax of using the Join operator is:

Syntax in C#

public static IEnumerable<V> Join<T, U, K, V>( this IEnumerable<T> outer, IEnumerable<U> inner, Function<T, K> outerKeySeIector, Function<U, K> innerKeySelector, Function<T, U, V> resultSelector);

Lets take an simple example

<div>
        <asp:ListBox ID="ListBox1" runat="server" Height="151px" Width="134px"></asp:ListBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Join Operator example" 
            onclick="Button1_Click" />
    </div>
Code behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default9 : System.Web.UI.Page
{
    public class Customer
    {
        public int key;
        public string Name;
    }
    public class order
    {
        public int key;
        public string OrderNumber;


    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var customers = new List<Customer>()
        {
new Customer {key =1,Name ="jacob"},
new Customer {key=2,Name ="smith"}
        };
        var orders = new List<order>()
        {
            new order {key=1,OrderNumber ="Number 1"},
            new order {key =2,OrderNumber ="Number 2"}

        };

        var q = from c in customers
                join o in orders on c.key equals o.key
                select new { c.Name, o.OrderNumber };
        foreach (var item in q)
        {
            ListBox1.Items.Add(item.OrderNumber.ToString() + " " + item.Name);
        }
    }
}
Code generate the following output
How to use LINQ Join operator in ASP.NET

Wednesday, April 9, 2014

DropdownList with image in ASP.NET

Dropdownlist : We have already discuss about DropdownList, today we will learn how to take text item with image in DropdownList. There are following steps to design Dropdownlist with image.
Step-1 : Import JQuery file from desired resources, resource is

http://code.jquery.com/jquery-1.8.2.js
Step-2 : Attach jQuery link with <script> tag in Head section. Now your code look like


<head id="Head1" runat="server">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
</head>

Step-3 : Download another JQuery file from mentioned resources, also attach with <script> tag in head section. Resource is
http://dl.dropboxusercontent.com/u/40036711/Scripts/jquery.ddslick.min.js

Step-4 : Create JQuery function, also bind DropDownlist with Text, description and Images.
Like
    $(function () {
        var dropdownlist = [
{
    text: "mango",
    value: 1,
    description: "summer fruit",
    imageSrc: "image001.jpg"
}
Similarly again. insert multiple item into the list.

Step-5 : Now call ddslick ( ) method , which is contain DropdownList parameters, such as Data, imagePosition, width etc in same scripting function.

 $('#drop').ddslick({
            data: dropdownlist,
            width: 200,
            imagePosition: "right"
            
        });

Step-6 : End your Jquery function
Step-7 : Take a <div> tag with drop id.

Now your code is 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>DropdownList with image</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="jquery.ddslick.min.js"  type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var dropdownlist = [
{
    text: "mango",
    value: 1,
    description: "summer fruit",
    imageSrc: "image001.jpg"
},
{
    text: "Apple",
    value: 2,
    description: "summer as well as winter fruit",
    imageSrc: "image002.jpg"
}

];
        $('#drop').ddslick({
            data: dropdownlist,
            width: 200,
            imagePosition: "right"
            
        });
    });
</script>
</head>
<body>
<form id="form1">
<div>
<div id="drop"></div>
</div>
</form>
</body>
</html>
Dropdownlist with image icon

How to check two string are equal in ASP.NET

Introduction

If you want to compare two string object then we can use compare( ) method of the string class. Using this method you can compare characters through casing rules and the alphabetic order. It returns 32-bit signed integer value. If returning value is equal to zero then both string are equal otherwise string is greater or less.
Suppose, your string returns less than zero value it means your first string is less than to string b.

Lets take an simple example

Source code
<div>
        <asp:Button ID="Button1" runat="server" Text="String Compare" onclick="Button1_Click" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>

Business logic code

#region stringcompare

    protected void Button1_Click(object sender, EventArgs e)
    {
        string a = "hello";
        string b = "hello";
        int c = string.Compare(a, b);
        if (c == 0)
        {
            Label1.Text = "Both string are equal";

        }
        else
        {
            if (c < 0)
            {
                Label1.Text = "string a is less than string b";

            }
            else
            {
                Label1.Text = "string a is greater than string b";
            }

        }
    }

    #endregion 

Code generate the following output
How to check two string are equal in ASP.NET

Friday, April 4, 2014

How to fix Incorrect syntax near the keyword from in ASP.NET

Today i facing a new problem that is
Incorrect syntax near the keyword from
I am receiving this error message when i retrieve the record from the database table, this code is successfully run in visual studio 2010 or earlier but not successfully run in vs 2012 or higher. So I think about that error and solve the problem that is

Incorrect syntax near the keyword 'from'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'from'.

Solution 

use brackets around the table like

Select * from [products]

If you use aggregate operators  query then should use '()' like

Select count (*) from [products]

Wednesday, April 2, 2014

Example of AppendFormat method in ASP.NET

 <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Height="42px" onclick="Button1_Click"
        Text="Click" Width="84px" BackColor="#990000" ForeColor="White" />
    <div style="width: 110px; height: 37px; margin-bottom: 28px">
   
        <asp:Label ID="Label1" runat="server" Text="Label" BackColor="#CCFF33"
            BorderColor="Black"></asp:Label>
   
    </div>
    <p>
        &nbsp;</p>
    </form>

code behind code -

 protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder stringb = new StringBuilder();
        int intgerValue = 110;
        string stringValue = "Apple";
        Boolean BooleanValue = true;
        stringb.AppendFormat("intger Value: {0}.string Value: {1}  and Boolean Value: {2}", intgerValue, stringValue, BooleanValue);
        Label1.Text = stringb.ToString();  
  
    }


Output-

Tuesday, April 1, 2014

How to pass ampersand in query string parameter in ASP.NET

According to our previous article how to use query string in asp.net, we use query string parameter for searching value from the database.Now, If you want to pass parameter value with special character like "&" . Let's take an simple example,  how to pass & in QueryString parameter.

I-method

Response.Redirect("~/Default3.aspx?userName=" + Server.UrlEncode("you&me") + "&Password=jacob");

Here, In above mentioned code your username contains & symbol between you&me and we use & for separates the field.

II-method

~/Default3.aspx?userName=you%26me&password=jacob

Here, we use %26, in the place of & symbol. Any browser consider & symbol as %26.

Complete Code

   Default2.aspx code

<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>

Default2.aspx.cs code

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default3.aspx?userName=" + Server.UrlEncode("you&me") + "&Password=jacob");
    }

Now, your button click event call another webform, where you bind  gridview through QueryString
Default3.aspx code

<form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="sno" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="sno" HeaderText="sno" InsertVisible="False" 
                    ReadOnly="True" SortExpression="sno" />
                <asp:BoundField DataField="userName" HeaderText="userName" 
                    SortExpression="userName" />
                <asp:BoundField DataField="Password" HeaderText="Password" 
                    SortExpression="Password" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT * FROM [user] WHERE (([userName] = @userName) AND ([Password] = @Password))">
            <SelectParameters>
                <asp:QueryStringParameter Name="userName" QueryStringField="userName" 
                    Type="String" />
                <asp:QueryStringParameter Name="Password" QueryStringField="Password" 
                    Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    
    </div>

    </form>

 Code Generate the following output

How to pass ampersand in query string parameter in ASP.NET

How to pass ampersand in query string parameter in ASP.NET
© Copyright 2013 Computer Programming | All Right Reserved