technology News

Showing posts about "net"

Format String using String.Format method in C#.net

Posted on August 29, 2012 01:25 AM

Orignal Source:http://codingvilla.com/string-format-method-csharp-single-article-611.aspx


String.Format Function:

 

String.Format is a static method and by using String.Format method you can change the format of different data types into string for example you can change the format of numerical or Boolean values into string. String.Format takes a format string and argument will return a string. Formatting strings are useful.

 

The syntax of Format string is

 

string string.format(string format,Object arg0)

 

It takes an argument and format string as parameter.

 

 

Format String using String.Format method:

 

To understand that how we can use the String.Format method let us consider this example declare a float type number and pass it to the String.Format method with an argument.

 

To demonstrate make a console application and write the following code.

 

 

Code:

 

classFormat_String

    {

staticvoid Main(string[] args)

        {

floatmyFloat = 1234.56789f;

string myString8 = String.Format("{0, 10:f3}", myFloat);

Console.WriteLine("String.Format("{0, 10:f3}", myFloat) = " + myString8);

 

 

Console.ReadLine();

        }

    }

 

RadioButtonList control in Asp.net 3.5

Posted on July 28, 2012 05:37 AM

RadioButtonList control in Asp.net 3.5

RadioButtonList control:This control enables the user to select only one list item at a time. Therefore, whenever we want to get a single input from user from a list of items, we can use RadioButtonList control. This control displays a list of radio button either horizontally or vertically.Common Properties: 

Property

Description

CellPadding

It is the space between border and the content of the table cell.

CellSpacing

It is the space between table cells.

RepeatColumns

These are number of columns to use when displaying the radio button group.

RepeatDirection

This specifies whether the radio button group should be repeated horizontally or vertically.

RepeatLayout

This is the layout of Radio button group.

Runat

This property specifies that the control is a server control.

TextAlign

This specifies on which side of radio button text should appear.

 A Simple Implementation:

 Following is the step by step procedure for the Implementation of a RadioButtonList Control.Step1:  Open visual studio and create a website.Step2:  Open design view of .aspx page and drag a RadioButtonList control, a button and a label on that page.Step3:  Now choose and configure data source from where you want RadioButtonList to fetch data. Now your code at .aspx page will be like below.

<body>

    <form id="form1" runat="server">

    <div>   

        <asp:RadioButtonList ID="RadioButtonList1" runat="server"

            DataSourceID="SqlDataSource1" DataTextField="CountryName"        DataValueField="CountryID">

        </asp:RadioButtonList>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"

           ConnectionString="<%$ ConnectionStrings:MoviesConnectionString%>"

            SelectCommand="SELECT [CountryID], [CountryName] FROM [Country]">

        </asp:SqlDataSource>   

    </div>

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click"Text="Submit" />

    <p>

        <asp:Label ID="Label1" runat="server" Text="lblMovie"></asp:Label>

    </p>

    </form>

</body> 

In the above code I have selected data from Country table of movies database.

 Step4: Now in your code-behind section write following code for displaying selected data in a label.     

protected void Button1_Click(object sender, EventArgs e)

    {

        Label1.Text = RadioButtonList1.SelectedItem.Text;

    }

 Step5: Now press F5. You can see the RadioButtonList is displayed on screen.

 

 

Purpose of 3-tier architecture in Asp.NET 3.5

Posted on July 28, 2012 04:50 AM

Purpose of 3-tier architecture in Asp.NET 3.5

Introduction of 3-tier Architecture:It is basically a client server architecture which is based on three main layers. These layers are separated from each other. However, by creating layers we can place code on separate layer which is reusable for specific functions. That layer can directly communicate with its adjacent layer for performing specific tasks. For example, if in an application, the presentation layer needs to retrieve information from a backend database, it will use other layers for performing that task rather than embedding database calls within itself.Structure of 3-tier Architecture:3-tier architecture will consist of following layers.The Presentation Layer: This layer contains user interface (UI) elements of the application. Application could be a desktop based or web based application. That layer contains .aspx pages or windows forms which provide an interface for user to input data.

The Business Logic Layer: This tier receives requests from presentation layer and returns the result based on its business logics. This layer contains classes for performing various business and mathematical functions such as calculating aggregate values. This layer has no concern with UI controls and databases. Its sub layer (data access) will supply it its needed information from database.Data Access Layer: This layer directly interacts with database and store and retrieves information. On receiving request from upper layer (Business Logic) data access layer retrieves and transfer data from data base. This layer retrieves data by executing SQL stored procedures.

 

  

               

How to perform And Operation on byte in C#.net

Posted on July 27, 2012 02:21 AM

How to perform And Operation on byte in C#.net

Byte data type:

 

In C# byte data type represents an 8-bit unsigned integer.

 

Perform and operation on Byte:

 

Perform and operation on Byte. To perform and operation on byte in C#.net is very simple C#.net provides a function Byte.Parse(). Which convert string representation of a number to its system.Byte equivalent. And then and operation can be operated on it.

 

To demonstrate make a window from application, and drag a button on the form

 

Now write the following code on the Button Click event.

 

Code:

private void button1_Andoperation_Click(object sender, EventArgs e) {

            byte mask = 0xFE;

            Byte byteValue = Byte.Parse("12");

            Console.WriteLine("{0} And {1} = {2}", byteValue, mask, byteValue & mask);

}

Now write the following code in Form load event.

 

Code:

        private void Form1_Load(object sender, EventArgs e){

            this.Text = "Coding villa Byte And Operation";

        }

This is a simple article that tells you how to perform And operation on byte in C#.net.

BulletedList Control in Asp.Net 3.5

Posted on July 27, 2012 02:07 AM
BulletedList Control in Asp.Net 3.5
BulletedList.aspx file: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="BulletedList.aspx.cs" Inherits="BulletedList" %>   <!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">   <body>     <h3>BulletedList</h3>     <form id="form1" runat="server">       <p>Click on one item plz.</p>        <asp:BulletedList id="Bulletedlist"        BulletStyle="Disc"       DisplayMode="LinkButton"       runat="server" onclick="Bulletedlist_Click">         <asp:ListItem Value="http://www.codingvilla.com">Coding Villa</asp:ListItem>         <asp:ListItem Value="http://www.smspunch.com/">Sms Punch</asp:ListItem>         <asp:ListItem Value="http://www.intellisoftsolutions.com">Intellisoft Solutions</asp:ListItem>              </asp:BulletedList>       <asp:Label id="Message"        Font-Size="12"       Width="168px"        Font-Bold="True"        runat="server"       AssociatedControlID="Bulletedlist"/>      </form>   </body> </html> Code behind file: using System; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls;   public partial class BulletedList : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {       }     protected void Bulletedlist_Click(object sender, BulletedListEventArgs e)     {         switch (e.Index)         {             case 0:                 Message.Text = "You clicked on Coding Villa";                 break;             case 1:                 Message.Text = "You clicked on Sms Punch";                 break;             case 2:                 Message.Text = "You clicked on Intellisoft Solutions";                 break;             default:                 throw new Exception("You did not click a valid list item");                 break;                         }     } }

FormView Control in ASP.NET 3.5

Posted on July 27, 2012 01:53 AM
FormView Control in ASP.NET 3.5
FormView.aspx File code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FormView.aspx.cs" Inherits="FormView" %>   <!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>FormView Control</title> </head> <body>     <form id="form1" runat="server">     <div>     <asp:FormView ID="FormView1" runat="server"              DataKeyNames="Product_ID" DataSourceID="SqlDataSource1" AllowPaging="True"              CellPadding="4" ForeColor="#333333">         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />         <RowStyle BackColor="#EFF3FB" />         <EditItemTemplate>             Product_ID:             <asp:Label ID="Product_IDLabel1" runat="server"                  Text='<%# Eval("Product_ID") %>' />             <br />             Product_Name:             <asp:TextBox ID="Product_NameTextBox" runat="server"                  Text='<%# Bind("Product_Name") %>' />             <br />             Supplier_ID:             <asp:TextBox ID="Supplier_IDTextBox" runat="server"                  Text='<%# Bind("Supplier_ID") %>' />             <br />             Color:             <asp:TextBox ID="ColorTextBox" runat="server" Text='<%# Bind("Color") %>' />             <br />             column1:             <asp:TextBox ID="column1TextBox" runat="server" Text='<%# Bind("column1") %>' />             <br />             <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"                  CommandName="Update" Text="Update" />             &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"                  CausesValidation="False" CommandName="Cancel" Text="Cancel" />         </EditItemTemplate>         <InsertItemTemplate>             Product_ID:             <asp:TextBox ID="Product_IDTextBox" runat="server"                  Text='<%# Bind("Product_ID") %>' />             <br />             Product_Name:             <asp:TextBox ID="Product_NameTextBox" runat="server"                  Text='<%# Bind("Product_Name") %>' />             <br />             Supplier_ID:             <asp:TextBox ID="Supplier_IDTextBox" runat="server"                  Text='<%# Bind("Supplier_ID") %>' />             <br />             Color:             <asp:TextBox ID="ColorTextBox" runat="server" Text='<%# Bind("Color") %>' />             <br />             column1:             <asp:TextBox ID="column1TextBox" runat="server" Text='<%# Bind("column1") %>' />             <br />             <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"                  CommandName="Insert" Text="Insert" />             &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"                  CausesValidation="False" CommandName="Cancel" Text="Cancel" />         </InsertItemTemplate>                  <ItemTemplate>             Product_ID:             <asp:Label ID="Product_IDLabel" runat="server"                  Text='<%# Eval("Product_ID") %>' />             <br />             Product_Name:             <asp:Label ID="Product_NameLabel" runat="server"                  Text='<%# Bind("Product_Name") %>' />             <br />             Supplier_ID:             <asp:Label ID="Supplier_IDLabel" runat="server"                  Text='<%# Bind("Supplier_ID") %>' />             <br />             Color:             <asp:Label ID="ColorLabel" runat="server" Text='<%# Bind("Color") %>' />             <br />             column1:             <asp:Label ID="column1Label" runat="server" Text='<%# Bind("column1") %>' />             <br />                     </ItemTemplate>                  <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />         <EditRowStyle BackColor="#2461BF" />              </asp:FormView>              <asp:SqlDataSource ID="SqlDataSource1" runat="server"              ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>"              ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>"              SelectCommand="SELECT [Product ID] AS Product_ID, [Product Name] AS Product_Name, [Supplier ID] AS Supplier_ID, [Color], [Price (SRP)] AS column1 FROM [Product]">         </asp:SqlDataSource>     </div>     </form> </body> </html>

A CAPTCHA Image / control in ASP.NET 3.5

Posted on July 27, 2012 01:34 AM

A CAPTCHA Image / control in ASP.NET 3.5

By Using the code:

 

CaptchaHandler.ashx file code:

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

 

using System;

using System.Web;

using System.Drawing;

using System.IO;

using System.Web.SessionState;

 

public class CaptchaHandler : IHttpHandler, IReadOnlySessionState

{

    public void ProcessRequest (HttpContext context) {

        Bitmap bmp = new Bitmap(200, 50);

        Graphics grps = Graphics.FromImage(bmp);

        grps.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        grps.FillRectangle(Brushes.Green, 0, 0, 100, 20);

        grps.DrawString(context.Session["CaptchaHandler"].ToString(), new Font("Arial", 10), new SolidBrush(Color.Red), 0, 0);

        MemoryStream mstream = new MemoryStream();

        bmp.Save(mstream, System.Drawing.Imaging.ImageFormat.Png);

        byte[] bmpBytes = mstream.GetBuffer();

        bmp.Dispose();

        mstream.Close();

        context.Response.BinaryWrite(bmpBytes);

        context.Response.End();

    }

    public bool IsReusable {

        get {

            return false;

        }

    }

}

.cs file code:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

 

public partial class Default13 : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            SetVerTxt();

        }

    }

    public void SetVerTxt()

    {

        Random rand = new Random();

        int n = rand.Next();

        Session["CaptchaHandler"] = n.ToString();

    }

    protected void CValidate(object source, ServerValidateEventArgs args)

    {

        if (Session["CaptchaHandler"] != null)

        {

            if (TextBox1.Text != Session["CaptchaHandler"].ToString())

            {

                SetVerTxt();

                args.IsValid = false;

                return;

            }

        }

        else

        {

            SetVerTxt();

            args.IsValid = false;

            return;

        }

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        if (!Page.IsValid)

        {

            return;

        }

        Response.Write("Verified!");

        SetVerTxt();

    }

}

Default.aspx

This is a very simple sample web form that contains only a few basic elements

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

 

<!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>Untitled Page</title>

</head>

<body>

   <form id="form1" runat="server">

<div>

      <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1"

            ErrorMessage="Wrong Verification Code!" OnServerValidate="CValidate"></asp:CustomValidator>      

     <asp:Image ID="imageCaptcha" ImageUrl="CaptchaHandler.ashx" runat="server" />

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  ControlToValidate="TextBox1"

          ErrorMessage="Enter Code From Picture Plz!" ></asp:RequiredFieldValidator>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" /></div>

 

</form>

</body>

</html>

Technology Blogs
Close