Thursday, July 15, 2010

Exceptions in .NET Framework

A variety of exceptions are defined in .NET Framework under the System namespace.
These Exceptions are thrown when certain errors are found in your C# code or in the methods you call.
All of these exceptions can be caught using a catch block in C#.

Here are some of the common Exceptions:

  EXCEPTION CLASS   CAUSE OF EXCEPTION
SystemException It is used as a base class for other exceptions. Occurs on a failed run time check
AccessException Occurs when failed to access a type member (method or a field)
ArgumentException Occurs when an Argument to a method is invalid
ArgumentNullException Occurs when a null argument is passed to a method which does not accept it
ArgumentOutofRangeException Occurs when argument value is out of range
ArithmeticException Occurs due to arithmetic over-flow or under-flow
ArrayTypeMismatchException Occurs when you attempt to store different type of object in an array
BadImageFormatException Occurs when the image is in the wrong format
CoreException Its the BaseClass for the Exceptions thrown by runtime
DivideByZeroException Occurs when you attempt to divide by zero
FormatException Occurs if the format of an argument is wrong
IndexOutofRangeException Occurs when an array index is out of range
InvalidCastException Occurs when you attempt to cast to an invalid class
InvalidOperationException Occurs if a method is called at an invalid time
NotFiniteNumberException Occurs when a number is invalid
NotSupportedException Occurs if a method is not implemented by a class
NullReferenceException Occurs if you attempt to use an unassigned reference
OutofMemoryException Occurs if the memory is not enough for execution
StackOverflowException Occurs when stack overflows

Tuesday, February 9, 2010

Tab Control Using MultiView in ASP.net C#

Here I have used Menu for Tab Buttons and MultiView for Tab contents.


Add the fallowing code in the page load event and menu item click event.



protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            MultiView1.ActiveViewIndex = 0;
        
    }
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        MultiView1.ActiveViewIndex = Int32.Parse(Menu1.SelectedValue);
    }







<div>
        <asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick" Orientation="Horizontal"
        style="left: 1px; position: relative; top: 30px" Height="38px" Width="127px" >
            <StaticMenuStyle HorizontalPadding="0px" VerticalPadding="0px" />
            <StaticSelectedStyle BackColor="#C0C0FF" BorderColor="#C0C0FF" />
           
            <Items>
                <asp:MenuItem Text="tab1" Value="0" Selected="True"></asp:MenuItem>
                <asp:MenuItem Text="tab2" Value="1"></asp:MenuItem>
                <asp:MenuItem Text="tab3" Value="2"></asp:MenuItem>
            </Items>
            <StaticHoverStyle BackColor="Silver" />
            <StaticMenuItemStyle BorderColor="#404040" BorderStyle="Solid" BorderWidth="1px" />
        </asp:Menu>
         </div>
        <div style="width: 429px; height: 191px; background-color: #C0C0FF;">
            <asp:MultiView ID="MultiView1" runat="server">
                <asp:View ID="View1" runat="server">
                   <br /> CONTENTS OF TAB1 (view1).</asp:View>
                <asp:View ID="View2" runat="server">
                    <br />
                    CONTENTS OF TAB2(view2)<br />
                </asp:View>
                <asp:View ID="View3" runat="server">
                    <br />
                    CONTENTS OF TAB3(view3)</asp:View>
            </asp:MultiView>
        </div>




Friday, February 5, 2010

Display the Cell Data of a Row in Textboxes using DropdownList for Identifying the Row

Here the dropdownlist is used to select a row through the ID column.
The selected row's cell data will be displayed on textboxes.
The database table I have used is as shown: (Table name: details1).

Bind the Dropdownlist with the ID column of the table either by using the fallowing code or by the smart task menu >> ConfigureDataSource wizard.








protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con1 = new SqlConnection("your connectionstring");
            con1.Open();
            SqlDataAdapter da1 = new SqlDataAdapter("select id from details1", con1);
            DataSet ds1 = new DataSet();
            da1.Fill(ds1);
            Dropdownlist1.DataSource = ds1;
            Dropdownlist1.DataTextField = "id";
            Dropdownlist1.DataBind();
            con1.Close();
        }
protected void  DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

        SqlConnection con1 = new SqlConnection("your connectionstring");
        con1.Open();
        SqlDataAdapter da1 = new SqlDataAdapter("select name,designation,place from details1 where   id=@id", con1);
        da1.SelectCommand.Parameters.AddWithValue("@id", DropDownList1.Text);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
        con1.Close();
        DataRow row1 = dt1.Rows[0];
        txtname.Text = row1["name"].ToString();
        txtdsgn.Text = row1["designation"].ToString();
        txtplace.Text = row1["place"].ToString();

    }

Monday, February 1, 2010

Nesting 3 DropdownLists in ASP.net C#

  •   Add 3 DropdownList controls to .aspx page.
  • Enable AutoPostback for the 1st and 2nd Dropdownlists.
public partial class Default : System.Web.UI.Page
{
    protected string[] d1 = { "select","eng", "mba"};

    protected string[] eng = { "select","ec","cs" };
    protected string []mba = { "select","fin", "hr" };
   

    protected string []ec = { "ec1", "ec2"};
    protected string []cs = { "cs1", "cs2"};

   
    protected string []fin = { "fin1", "fin2"};
    protected string []hr = { "hr1", "hr2"};

  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = d1;
            DropDownList1.DataBind();
        }
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownList1.SelectedIndex == 1)
        {
            DropDownList2.DataSource = eng;
            DropDownList2.DataBind();
        }
        else if (DropDownList1.SelectedIndex == 2)
        {
            DropDownList2.DataSource = mba;
            DropDownList2.DataBind();
        }
       
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        switch (DropDownList2.SelectedIndex)
        {
            case 1:
                {

                    if (DropDownList1.SelectedIndex == 1)
                    {
                        DropDownList3.DataSource = ec;
                        DropDownList3.DataBind();
                       
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        DropDownList3.DataSource = fin;
                        DropDownList3.DataBind();
                       
                    }
                    break;
                }
            case 2:
                {
                    if (DropDownList1.SelectedIndex == 1)
                    {
                        DropDownList3.DataSource = cs;
                        DropDownList3.DataBind();
                       
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        DropDownList3.DataSource = hr;
                        DropDownList3.DataBind();
                       
                    }
                }
                break;
         }

    }
}

NESTED DROPDOWNLISTS IN ASP.NET C#(c sharp)

Nesting Dropdownlists is a method of controlling list items of a Dropdownlist through selected index of other DropdownList.
For example let's take two DropdownLists where the first acts as a parent to the second.

Let's assume to list Movie names in the first DropdownList and to list in the second DropdownList the Theatres showing the selected Movie.

  • Add two DropdownList controls to the .aspx page and enable AutoPostback in the properties window of first DropdownList.
  • Add the fallowing code:

public partial class Default : System.Web.UI.Page
{
    // First DropdownList items
    protected string[] Movies ={ "select a movie", "Movie_1", "Movie_2", "Movie_3" };

    // Second DropdownList items. One among these to be choosen.
    protected string[] Movie_1={"Theatre_1","Theatre_2","Theatre_3"};
    protected string[] Movie_2 ={ "Theatre_4", "Theatre_5" };
    protected string[] Movie_3 ={ "Theatre_5", "Theatre_6", "Theatre_7" };
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = Movies;
            DropDownList1.DataBind();
        }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == 1)
        {
            DropDownList2.DataSource = Movie_1;
            DropDownList2.DataBind();
        }
        if (DropDownList1.SelectedIndex == 2)
        {
            DropDownList2.DataSource = Movie_2;
            DropDownList2.DataBind();
        }
        if (DropDownList1.SelectedIndex == 3)
        {
            DropDownList2.DataSource = Movie_3;
            DropDownList2.DataBind();
        }
    }
}


Wednesday, January 27, 2010

Getting Data From Selected Cell of a GridView in ASP.net

To display or fetch data of a cell from GridView use the fallowing steps:

1.Bind the Gridview with Data.
2. Enable Selection in Gridview by using its smart task panel.
3.Double click on the GridView so that it generates the fallowing event:


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


4. Now write the fallowing code inside the SelectedIndexChanged event


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        Label1.Text = row.Cells[3].Text;
    }


The first line fetches the Data of the Selected Row from the GridView and
the second line displays the 3rd cell data on a Label.

5. Specify the Index of the Cell in Cells[] which you want to display.

6.When you Run the page Select Button is generated with each Row. Press the Select button of the corresponding Row and you will see the respective Cell data on the Label.

































Here in the example you see the design page with GridView and a Label.
In the next picture you see the Contact column of 3rd row being displayed on the Label.

Monday, January 25, 2010

Setting Breakpoints and Watching Variables in ASP.Net

In Visual Studio you can set a Breakpoint to stop the project at a particular line of code.
Once you set the Breakpoint to a line and you run the project, it will stop the project  before executing the line and shows the particular line in the code editor.

To set a Breakpoint click the gray margin to the left of the line where you want to give a break.
The watch window opens only when the debugger is in running mode or break mode.
So open the watch window when the projects halts at the breakpoint through
Debug >> Windows >> Watch  and select a watch window.




You can view the value of the variables by just pointing the mouse over the variable and also using the Watch.
If the variable is complex type like arrays or an object you should use the watch window.
To add the variables into watch window just select the variable and drag it to the watch window.
Click the +  sign to view the subitems such as array elements or object properties.