Binding Dropdown list



Static from CS

  List<ListItem> li = new List<ListItem>();
                li.Add(new ListItem { Text = "Select", Value = "0" });
                li.Add(new ListItem { Text = "India", Value = "0" });
                li.Add(new ListItem { Text = "Pak", Value = "0" });
                li.Add(new ListItem { Text = "Ban", Value = "0" });
                li.Add(new ListItem { Text = "SL", Value = "0" });

                DropDownList1.DataSource = li;
                DropDownList1.DataBind();




Static from ASPX

<asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="Select" Value="0"></asp:ListItem>
        <asp:ListItem Text="India" Value="1"></asp:ListItem>
        <asp:ListItem Text="Pak" Value="2"></asp:ListItem>
        <asp:ListItem Text="Ban" Value="3"></asp:ListItem>
        <asp:ListItem Text="SL" Value="4"></asp:ListItem>
</asp:DropDownList>



Dynamic Binding

                         //dt is the DataTable obtained from SQL Server using ADO.NET
                DropDownList1.DataSource = dt;
                DropDownList1.DataTextField = "EmpName";
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataBind();



Insert or ADD

DropDownList1.Items.Insert(0, new ListItem { Text = "Select", Value = "0" });
DropDownList1.Items.Insert(1, new ListItem { Text = "India", Value = "1" });
DropDownList1.Items.Insert(2, new ListItem { Text = "Pak", Value = "2" });
DropDownList1.Items.Insert(3, new ListItem { Text = "Ban", Value = "3" });
DropDownList1.Items.Insert(4, new ListItem { Text = "SL", Value = "4" });
DropDownList1.Items.Insert(5, "AFG");

DropDownList1.Items.Add("India");
DropDownList1.Items.Add(new ListItem { Text = "China", Value = "5" });




Auto-generate serial no for Datalist, Repeater, Gridview

We can use DataItemIndex to generate serial no for GridView.


Now, we can’t use DataItemIndex for DataList or a Repeater control. 

For them there is another property called ItemIndex , which will show the anticipated result. 

 syntax for ItemIndex:
<ItemTemplate>
     <%#Container.ItemIndex+1 %>
</ItemTemplate>


IBRAAHIM ZAUQ

Lai hayat aye qaza le chali chale 
Na apni khushi aye na apni khushi chale
Behatar tu hai yahi ke na duniya se dil lage 
Par kya karen jo kam na bedillagi chale
Ho umr e khizr bhi tu kahenge bavaqt e marg 
Ham kya rahe yahan abhi aye abhi chale
Duniya ne kis ka rah e fana men diya hai sath 
Tum bhi chale chalo yun hi jab tak chali chale
Nazan na ho khirad pe jo hona hai wo hi ho 
Danish teri na kuch meri danishvari chale
Kam honge is bisat pe ham jaise badqimar 
Jo chal ham chale wo nihayat buri chale
Ja ki hava e shauq men hain is chaman se `zauq` 
Apni bala se bad e saba kahin chale


Stored Procedure to Get the Data from SQL Server

            
            SqlConnection con = new SqlConnection();
            SqlCommand dcmd = new SqlCommand();
            DataTable dt = new DataTable();
            string  strEmpID = "1";

            con.ConnectionString=
            ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString;

            con.Open();
            dcmd = new SqlCommand("SP_GetEmployeeByID", con);
            dcmd.CommandType = CommandType.StoredProcedure;
            dcmd.CommandTimeout = 0;
            try
            {
                dcmd.Parameters.AddWithValue("@EmpID", strEmpID);              
                SqlDataAdapter mySqlAdapter = new SqlDataAdapter(dcmd);               
                mySqlAdapter.Fill(dt);             
            }
            catch
            {
                throw;
            }
            finally
            {
                dcmd.Dispose();
                con.Close();
            }




While Loop in SQL Server

Example of WHILE Loop

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
END


Result Set:
1
2
3
4
5


Example of WHILE Loop with BREAK keyword

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
IF @intFlag = 4
BREAK;
END

Result Set:
1
2
3

Example of WHILE Loop with CONTINUE and BREAK keywords


DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
CONTINUE;
IF @intFlag = 4 -- This will never executed
BREAK;
END

Result Set:
1
2
3
4
5




Using CommandBuilder to perform DML Operations



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

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=master;Integrated Security=True");
    DataTable dt = new DataTable();
    SqlDataAdapter da;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            da = new SqlDataAdapter("select * from tb_Student", con);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            da.Fill(dt);
        }

    }


    protected void btnRegister_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtRollNo.Text == dt.Rows[i]["RollNO"].ToString())
            {
                Response.Write("<script>alert('Roll No Already Exists')</script>");
                return;

            }
        }

        DataRow dr = null;
        dr = dt.NewRow();
        dr["RollNO"] = txtRollNo.Text;
        dr["Name"] = txtStuName.Text;
        dr["Class"] = txtClass.Text;
        dr["Section"] = txtSection.Text;
        dr["Age"] = txtAge.Text;
        dt.Rows.Add(dr);
        dt.AcceptChanges();
        da.Update(dt);
     
        Response.Write("<script>alert('Student Added Successfully')</script>");
    
    }


  
 protected void btnDelete_Click(object sender, EventArgs e)
    {
        int k = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtRollNo.Text == dt.Rows[i]["RollNO"].ToString())
            {
                k = 1;
                DataRow dr = null;
                dr = dt.Select("RollNo=" + txtRollNo.Text)[0];
                if (dr != null)
                {
                    dr.Delete();
      dt.AcceptChanges();
                    da.Update(dt);
                    Response.Write("<script>alert('Deleted Successfully')</script>");
                
                    break;
                }

            }
         
        }
        if (k == 0)
        {
            Response.Write("<script>alert('There is no such RollNO to Delete')</script>");
   
        }     
     
    }




    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int k = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtRollNo.Text == dt.Rows[i]["RollNO"].ToString())
            {
                k = 1;
                DataRow dr = null;
                dr = dt.Select("RollNo=" + txtRollNo.Text)[0];
                if (dr != null)
                {

                    dr.BeginEdit();
                    dr["RollNO"] = txtRollNo.Text;
                    dr["Name"] = txtStuName.Text;
                    dr["Class"] = txtClass.Text;
                    dr["Section"] = txtSection.Text;
                    if (txtAge.Text != "")
                    {
                        dr["Age"] = (txtAge.Text);
                    }
                    dr.EndEdit();
      dt.AcceptChanges();

                    int res = da.Update(dt);
                    if (res > 0)
                    {
                        Response.Write("<script>alert('Updated Successfully')</script>");
                    }
                    break;
                }

            }
        }

        if (k == 0)
        {
            Response.Write("<script>alert('There is no such Roll No available')</script>");

        }
    }