-->

Saturday, September 28, 2013

Formating on Label Control in ASP.NET

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:Label ID="Label1" runat="server" BackColor="#CCCC00"
            Text="Chnage back color using Back Color property"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" BorderColor="Red" BorderStyle="Solid"
            Text="Change Border Color Using BorderColor Property"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" BorderStyle="Dashed"
            Text="Change border style dashed using BorderStyle property"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label4" runat="server" BorderWidth="3px"
            Text="Border Width using BorderWidth Property"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label5" runat="server" ForeColor="#333300"
            Text="Chnage Forecolor using Forecolor property"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label6" runat="server" Font-Bold="True" Text="Change Font Bold"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label7" runat="server" Font-Strikeout="True"
            Text="For wrong type text "></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label8" runat="server" Font-Overline="True"
            Text="Overline text , line on the text"></asp:Label>
        <br />
        <br />
 
    </div>
    </form>
</body>
</html>

Output
Formating on Label Control in ASP.NET

Friday, September 27, 2013

Bind DropdownList using structure in ASP.NET

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Codebehind 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 Default7 : System.Web.UI.Page
{
    struct student
    {
       public string name;
       public int age;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        student[] s1 = new student[5];
        s1[0].name = "jacob";
        s1[1].name = "jacob lefore";
        s1[2].name = "my name is jacob";



        foreach (student item in s1)
        {
            DropDownList1.Items.Add(item.name);   
        }
        




    }
}

Output
Bind DropdownList using structure in ASP.NET

How to use DropDownList Control in ASP.NET

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.Items.Add("Apple");
        DropDownList1.Items.Add("mango");
        DropDownList1.Items.Add("orange");
     
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "your selected item is <br/>" + DropDownList1.SelectedItem.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="20px"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="186px"
            AutoPostBack="True">
        </asp:DropDownList><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Output
Example of Add item to DropdownList in ASP.NET

Example of Add item to DropdownList in ASP.NET

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.Items.Add("Apple");
        DropDownList1.Items.Add("mango");
        DropDownList1.Items.Add("orange");
     
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "your selected item is <br/>" + DropDownList1.SelectedItem.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="20px"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="186px"
            AutoPostBack="True">
        </asp:DropDownList><br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Output
Example of Add item to DropdownList in ASP.NET

How to Bind ListBox using Complex DataType in ASP.NET

Step-1: Define class properties in class
Step-2: Create a List<DataType> object in code file.
Step-3: Add fields to the list.
Step-4: Bind ListBox with list
Step-5: Assign DataMember to the ListBox.


Create a class file in ASP.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Fruits
/// </summary>
public class Fruits
{
    public string  Fruits_Name { get; set; }
    public decimal Fruits_price { get; set; }
 

Take a ListBox on Design window
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server"
            ></asp:ListBox>
    </div>
    </form>
</body>
</html>

Codebehind 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 Default5 : System.Web.UI.Page
{
    List<Fruits> listitem = new List<Fruits>();

    protected void Page_Load(object sender, EventArgs e)
    {
        listitem.Add(new Fruits() { Fruits_Name = "Apple", Fruits_price = 45.40M });
        listitem.Add(new Fruits() { Fruits_Name = "Mango", Fruits_price = 30.40M });
        listitem.Add(new Fruits() { Fruits_Name = "Orange", Fruits_price = 36.40M });
        listitem.Add(new Fruits() { Fruits_Name = "grapes", Fruits_price = 89.40M });
        ListBox1.DataSource = listitem;
        ListBox1.DataTextField = "Fruits_Name";
        ListBox1.DataBind();

    }
}

Output
How to Bind ListBox using Complex DataType in ASP.NET

Thursday, September 26, 2013

How to Bind DropDownList using Complex DataType in ASP.NET

Step-1: Define class properties in class
Step-2: Create a List<DataType> object in code file.
Step-3: Add fields to the list.
Step-4: Bind DropDownlist with list
Step-5: Assign DataMember to the DropDownList.


Create a class file in ASP.NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Fruits
/// </summary>
public class Fruits
{
    public string  Fruits_Name { get; set; }
    public decimal Fruits_price { get; set; }
 

Take a DropDownList on Design window
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Codebehind 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 Default5 : System.Web.UI.Page
{
    List<Fruits> listitem = new List<Fruits>();

    protected void Page_Load(object sender, EventArgs e)
    {
        listitem.Add(new Fruits() { Fruits_Name = "Apple", Fruits_price = 45.40M });
        listitem.Add(new Fruits() { Fruits_Name = "Mango", Fruits_price = 30.40M });
        listitem.Add(new Fruits() { Fruits_Name = "Orange", Fruits_price = 36.40M });
        listitem.Add(new Fruits() { Fruits_Name = "grapes", Fruits_price = 89.40M });
        DropDownList1.DataSource = listitem;
       DropDownList1.DataTextField = "Fruits_Name";
        DropDownList1.DataBind();

    }
}

Output
How to Bind DropDownList using Complex DataType in ASP.NET

Wednesday, September 25, 2013

How to Retrieve Data from SQL Database: Windows Forms

In my previous post, I have created a connection object and then get all the records from the database table. What if we want to bind our datagridview without writing a single line of code? So in this article we will do something like this.


 Just drag-n-drop a datagridview on the form and name it, if you want. A datagridview tasks pop-up menu appears automatically as shown:

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

Select the Choose Data Source drop-down list and then Add Project Data Source as shown:

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

Now Data Source Configuration wizard will open, select Database as your data source. The wizard will look like:

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

By clicking on Next button, It will prompt for the database model you want to use. Select dataset, it will determines the type of data objects our application will use.

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

In the next step we have to choose our connection that is used by the application use to connect to the database.

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

Click on New Connection and it will prompt for Choose Data Source dialog box having some more options to select.

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

Select Microsoft SQL Server which is used to connect to MS SQL Server, and the data provider should be .NET Framework Data Provider for SQL Server. Add Connection dialog box will appear as shown:

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

Write the server name as given “(LocalDb)\v11.0”, and select appropriate database name from the drop down list below. I have select StockDb here. Just click on Test Connection to check our connection succeeded or not.

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

Press ok and it will show Data Source Configuration wizard again with the specified connection and also connection string.

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

Choose the table name from the tree view shown. I have select Groups table because I want to show the records of groups table. Finish now with selecting the data set name. If you will forget the dataset name, it will used the by default name.

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

It will show the datagridview binding with the columns names of groups table as shown in the above image. Now run the project and look out the records bind to the datagridview

How to Retrieve Data From SQL Server Database and bind to DataGridView in Windows Forms

As I have said above, we have not wrote a single line of code.
© Copyright 2013 Computer Programming | All Right Reserved