Tampilkan postingan dengan label c#. Tampilkan semua postingan
Tampilkan postingan dengan label c#. Tampilkan semua postingan

Basic and Beginner .NET LINQ

Posted On // Leave a Comment


 class Program
    {
        static void Main(string[] args)
        {
            List<Car> ListOfCar = new List<Car>()
            {
                new Car {name = "Toyota"    , owner = "Alex" , model = 1992},  
                new Car {name = "Mitsubishi", owner = "Jeff" , model = 2005},  
                new Car {name = "Land Rover", owner = "Danny", model = 2001},  
                new Car {name = "BMW"       , owner = "Danny", model = 2006},  
                new Car {name = "Subaru"    , owner = "Smith", model = 2003}  
            };
            
            ///BASIC LINQ SINTAX
            ///
            IEnumerable<Car> QueryResult = from car in ListOfCar
                                           select car;

            foreach (Car car in QueryResult)
                Console.WriteLine(car.name);


            ///PROJECTION
            ///based on an existing object type
            IEnumerable<CarOwner> QueryResult = from car
                                                in ListOfCar
                                                select new CarOwner { owner_name = car.owner };
            foreach (CarOwner owner in QueryResult)
                Console.WriteLine(owner.owner_name);

            ///Projections using Anonymous Types
            ///type of the variable is unknown until. it�s assigned for the first time.
            ///
            var QueryResult = from car in ListOfCar
                              select new
                              {
                                  car_name = car.name,
                                  owner_name = car.owner
                              };
            Console.WriteLine("PROJECTION ANONYMOUS TYPE");
            foreach (var entry in QueryResult)
                Console.WriteLine(entry.car_name);

            ///REFINING DATA
            ///select by conditions
            ///
            IEnumerable<Car> QueryResult = from car in ListOfCar
                                           where car.name == "Subaru"
                                           select car;
            foreach (Car car in QueryResult)
                Console.WriteLine(car.name);

            ///ORDER BY
            ///
            IEnumerable<Car> QueryResult = from car in ListOfCar
                                           orderby car.model descending
                                           select car;
            foreach (Car car in QueryResult)
                Console.WriteLine(car.model + "," + car.name);

            ///GROUPING
            ///
            var QueryResult = from car in ListOfCar
                              group car by car.owner into carOnwerGroup
                              select carOnwerGroup.Key;
            
            ///JOINS
            ///join LINQ
            ///

            List<Car> ListOfCars = new List<Car>()  
            {  
                new Car {name = "Mitsubishi", owner = "Jeff" , model = 2005},  
                new Car {name = "Land Rover", owner = "Danny", model = 2001},  
                new Car {name = "Subaru"    , owner = "Smith", model = 2003},  
                new Car {name = "Toyota"    , owner = "Alex" , model = 1992},  
                new Car {name = "BMW"       , owner = "Danny", model = 2006},  
            };

            List<CarOwner> ListOfCarOwners = new List<CarOwner>()  
            {  
                new CarOwner {owner_name = "Danny", age = 22},  
                new CarOwner {owner_name = "Jeff" , age = 35},  
                new CarOwner {owner_name = "Smith", age = 19},  
                new CarOwner {owner_name = "Alex" , age = 40}  
            };
            var QueryResult = from car in ListOfCars
                              join carowner in ListOfCarOwners on car.name equals carowner.owner_name
                              select new
                              {
                                  name = car.name,
                                  owner = car.owner,
                                  owner_age = carowner.age
                              };
            foreach (var entry in QueryResult)                
                Console.Write(entry.name + "," + entry.owner + "," + entry.owner_age);




            Console.Read();
        }
    }

    class Car
    {
        public string name { get; set; }
        public string owner { get; set; }
        public int model { get; set; }
    }
    class CarOwner
    {
        public string owner_name { get; set; }
        public int age { get; set; }
    }
full article http://net.tutsplus.com/tutorials/asp-net/net-linq-from-scratch/
[Read more]

Dynamic File Upload ASP.NET Using JavaScript

Posted On // Leave a Comment

JavaScript

var counter = 0;
function AddFileUpload()
{
     var div = document.createElement('DIV');
     div.innerHTML = '<input id="file' + counter + '" name = "file' + counter + '" type="file" /><input id="Button' + counter + '" type="button" value="Remove" onclick = "RemoveFileUpload(this)" />';
     document.getElementById("FileUploadContainer").appendChild(div);
     counter++;
}
function RemoveFileUpload(div)
{
     document.getElementById("FileUploadContainer").removeChild(div.parentNode); 
}

ASPX

<form id="form1" runat="server" enctype="multipart/form-data" method = "post">
    <span style ="font-family:Arial">Click to add files</span>&nbsp;&nbsp;
    <input id="Button1" type="button" value="add" onclick = "AddFileUpload()" />
    <br /><br />
    <div id = "FileUploadContainer">
        <!--FileUpload Controls will be added here -->
    </div>
    <br />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
</form>

Code Behind

protected void btnUpload_Click(object sender, EventArgs e)
{
    for (int i = 0; i < Request.Files.Count; i++)
    {
        HttpPostedFile PostedFile = Request.Files[i];
        if (PostedFile.ContentLength > 0)
        {
            string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
            PostedFile.SaveAs(Server.MapPath("Files\\") + FileName);
        }
    }
}
download source code :
 source : www.aspsnippets.com

[Read more]

Menambahkan Pane pada ASP.NET Ajax Accordion Melalui CodeBehind

Posted On // Leave a Comment
Tampilan disamping adalah contoh penggunaan control ajax accordion untuk halaman FAQ.
Kita bisa menambahkan pane secara dinamis melalui CodeBehind. berikut adalah kode nya.







<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="View.aspx.cs" Inherits="AspCs.faq.View" %>
<%@ Register
    Assembly="AjaxControlToolkit"
    Namespace="AjaxControlToolkit"
    TagPrefix="ajax" %>




    View FAQ
   


   
   

   
            runat="server"
        SelectedIndex="0"
        HeaderCssClass="AccordionHeader"
        HeaderSelectedCssClass="AccordionHeaderSelected"
        ContentCssClass="AccordionContent"
        AutoSize="None"
        FadeTransitions="true"
        TransitionDuration="250"
        FramesPerSecond="40"
        RequireOpenedPane="true"
        SuppressHeaderPostbacks="true">  
       
           
       

          
   



Kode Pada CodeBehind
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using DataAccessFacade.TMIFaqDao;
using BusinessObject.TMIFaq;

namespace AspCs.faq
{
    public partial class View : System.Web.UI.Page
    {
        QuestionDao qDao = new QuestionDao();        
        
        protected void Page_Load(object sender, EventArgs e)
        {

            List lq = qDao.Select("categoryId", "1");
            foreach (Question q in lq)
            {
                Label lblTitle = new Label();
                Label lblContent = new Label();
                lblTitle.Text = q.Questions;
                lblContent.Text = q.Answer;

                
                AjaxControlToolkit.AccordionPane pane = new AjaxControlToolkit.AccordionPane();
                pane.ID = "pane1" + q.Id;
                pane.HeaderContainer.Controls.Add(lblTitle);
                pane.ContentContainer.Controls.Add(lblContent);

                accordionFAQ.Panes.Add(pane);
            }

        }
    }
}

[Read more]

Get Only Current Page Data From ASP.NET Gridview

Posted On // Leave a Comment
Sometimes we want to get current page data from gridview. In my case, I want to update current view data as read. Here is the code:


private void UpdateAsRead()
    {
        //get header
        foreach (DataControlField col in gvForm.Columns)
        {
            //Response.Write(col.HeaderText);
        }
        //get data
        foreach (GridViewRow row in gvForm.Rows)
        {
            int i = 1;
            foreach (TableCell cell in row.Cells)
            {
                //get all rows
                if (i == 1)
                {
                    GeneralDao gen = new GeneralDao();
                    //Response.Write(cell.Text + "
");

                    string strSQL = @"UPDATE TMIForm SET ldate ='" + gen.GetDateTime() + @"', luserid='" + GetUsername + @"'
                                        WHERE id = '" + cell.Text + @"'";
                    frmDao.Update(strSQL);
                }
                i += i;

                if (i == 7) { i = 1; }

            }
        }  
    }
[Read more]

Disabling the right click on web page

Posted On // Leave a Comment

Method 1

In this method we add a javascript method, in this we check if click is right click or left click if it is right click then a message is displayed like "Right click disabled"



But in this solution if you click the right click it displays the message
“Right click disabled”.

If we want to remove the messegebox then this solution do not work.

  I am having another simple solution to achieve the same result and also it does not show the messegebox.
Method 2

          In this method we set the oncontextmenu="return false" in the body part of the page
so body part will look like


...

So whenever user clicks the right mouse button nothing will happen, no message no context menu.

 Sometimes there might be requirement to disable the right click on specific control to achive this we have to follow these steps.
1.      remove added code in body (i.e. oncontextmenu=”return false”)
2.      Add one table and in its row add the control e.g. datagrid control
3.      put the oncontextmenu=”return false” in the for that control

So code will look like this


 

   
   
     ---
   
 



So by this method context menu on the right click of right mouse click can be disabled.

source: http://www.codeproject.com/Articles/11222/Disabling-the-right-click-on-web-page
[Read more]

Get ListBox Multiple Selected Value

Posted On // Leave a Comment
Get ListBox multiple selected values
private string GetSelectedCategories()
    {
        string strItem=string.Empty;
        for (int i = 0; i < lbCategories.Items.Count; i++)
        {
            if (lbCategories.Items[i].Selected)
            {
                strItem += lbCategories.Items[i].Text;
            }
        }
        return strItem;
    }
[Read more]