vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, October 29, 2012

How to format string with leading zeros in C#


Here I share bit of useful code as we need to use normal project.
Consider a case we have string with single digit,we need to format string with adding leading zeros.
In that case you can use the below code to format string with leading zeros.
You can add leading zeros by using two different type of method code:

First Method:
   string srt = "45";
        //here i have string as two digit.now its going to format by adding zeros with 3 didgit
        Response .Write (srt.PadLeft  (3,'0'));

Output:
045

Second Method:
Same output we can get by using different code …
Response.Write (String.Format("{0:D3}",45));

Output:
045
Take another example to add leading zeros, consider we need to display  Time format.
We use Date time to get the current time.
Response.Write(String.Format("{0:D2}:{1:D2}:{2:D2}", DateTime.Now.Hour, DateTime.Now.Minute,DateTime.Now.Second));

Output:
09:07:44
I hope you like this article,Post your comments here..

Share this article with your friends using share button


Saturday, October 27, 2012

DataPager control in asp.net


The DataPager control is designed to display the navigation for paging to the end user and
to coordinate data paging with any databound control.

Normally datapager control can be used to display the page number size in listview or any data control.
It’s have more option to custumize the page, because it’s completely separate control give the total freedom over where to place it on your webpage.

For an example here I used datapager control for listview.Its completely separate control from listview.
So we need to provide the connection between ListView and Datapager control.For that in datapager control have an option PagedControlID ="ListView1".Here we mention the listview control name to use for paging.

In an previous article I wrote about listview controls.Here I just linked the two articles to give paging for ListView.



Code:
<div>
        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="5"
            OnPreRender="paging_render">
            <Fields>
                <asp:NextPreviousPagerField ShowFirstPageButton="true" ShowLastPageButton="true" />
                <asp:NumericPagerField ButtonCount="3" />
            </Fields>
        </asp:DataPager>
    </div>

On prerender we need to mention function name to bind the data in datapager control.so I have written a function on server side in the name of paging_render.so I declared it on the datapager.
Server Code:
protected void paging_render(object sender, EventArgs e)
    {
        //Load data is nothing to bind the data from database to listview.
        //****www.Dotnetcode.in******
        loaddata();
    }


I hope you like ..Post your comments here

Friday, October 26, 2012

How to insert,edit and delete data in Listview control in asp.net


Here I would like to discuss about to insert,edit and delete data in ListView controls in ASP.Net.
In previous about I discuss about grdiview control and datalist controls design and the process of update, insert and delete operation.

But Listview control started to use from the framework 3.5.It has some own advantage to compare with other data controls like insert new record, delete and update the previous record process.
In design process we can customize listview control for our own types,it has more advantage when compared with other controls.

Templates in ListView:
InsertItemTemplate: Defines the presentation for a new item being inserted into the data set.
ItemSeparatorTemplate: Defines the content to display between individual data items.
ItemTemplate: Defines the data to display for single items.
LayoutTemplate: Defines the main presentation features for the control. This root template contains placeholders that are replaced by what is defined by ItemTemplate and GroupTemplate.
SelectedItemTemplate: Specifies the content to display when users select items.
AlternatingItemTemplate: Defines how alternating items are displayed.
EditItemTemplate: Defines the presentation for an item when it is edited.
EmptyDataTemplate: Specifies the content to display for empty individual items.
EmptyItemTemplate: Specifies the content to display for empty group items.
GroupSeparatorTemplate: Specifies the content to display between groups.
GroupTemplate: Defines the content for groups. This template is a placeholder for the data displayed for individual group items (when data is grouped).
In sample code I have do the process of insert image and data in to database using ListView controls in asp.net. 

Design code:
Note:
Inside listview you have to add placeholder inside layouttemplate for holding edititemtemlate and inseritemtemplate.
Also you have to mention insert item positon in the listview.here I have mention insertitempostion as last .so the new item template have displayed in the last postion.
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="PlaceHolder1" InsertItemPosition="LastItem"
            OnItemCanceling="ListView1_ItemCanceling" OnItemDeleting="ListView1_ItemDeleting"
            OnItemEditing="ListView1_ItemEditing" OnItemInserting="ListView1_ItemInserting"
            DataKeyNames="Number" OnItemUpdating="ListView1_ItemUpdating">
            <LayoutTemplate>
                <table cellpadding="0" cellspacing="0" width="100%" class="ItemCSS">
                    <tr>
                        <td width="25%" style="border-bottom: 1px solid #A13EA1;">
                            Number
                        </td>
                        <td width="25%" style="border-bottom: 1px solid #A13EA1;">
                            Name
                        </td>
                        <td width="25%" style="border-bottom: 1px solid #A13EA1;">
                            Address
                        </td>
                        <td width="25%" style="border-bottom: 1px solid #A13EA1;">
                            City
                        </td>
                    </tr>
                    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td style="border-bottom: 2px dotted Gray;">
                        <%#Eval("Number")%>
                    </td>
                    <td style="border-bottom: 2px dotted Gray;">
                        <%#Eval("Name")%>
                    </td>
                    <td style="border-bottom: 2px dotted Gray;">
                        <%#Eval("Address")%>
                    </td>
                    <td style="border-bottom: 2px dotted Gray;">
                        <asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" />
                        <span onclick="return confirm('Are you sure to delete?')">
                            <asp:LinkButton ID="lnkDelete" runat="server" Text="Delete" CommandName="Delete"
                                ForeColor="Brown" />
                    </td>
                </tr>
            </ItemTemplate>
            <EditItemTemplate>
                <tr>
                    <td height="50">
                        <asp:TextBox ID="TextBox3" runat="server" Text=' <%#Eval("Number")%>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox2" runat="server" Text=' <%#Eval("Name")%>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox1" runat="server" Text=' <%#Eval("Address")%>'></asp:TextBox>
                    </td>
                    <td>
                        <span onclick="return confirm('Are you sure to update?')">
                            <asp:LinkButton ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
                        </span>
                        <asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
                    </td>
                </tr>
            </EditItemTemplate>
            <InsertItemTemplate>
                <tr>
                    <td width="25%" height="50">
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </td>
                    <td width="25%">
                        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                    </td>
                    <td width="25%">
                        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                    </td>
                    <td width="25%">
                        <span onclick="return confirm('Are you sure to update?')">
                            <asp:LinkButton ID="btnUpdate" runat="server" Text="Insert" CommandName="Insert" />
                        </span>
                        <asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel"
                            ForeColor="Brown" />
                    </td>
                </tr>
            </InsertItemTemplate>
            <EmptyDataTemplate>
                <hr />
                No Records Found.<hr />
                <asp:LinkButton ID="lnkInsert" runat="server" Text="Insert Records" CommandName="Insert" />
            </EmptyDataTemplate>
        </asp:ListView>

C# Code:
Note:
Initially you must have to assign your connection string to run sqlcommand on the listview.
Here I have declared connection string on CONSTR variable.
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loaddata();
        }
    }
    void loaddata()
    {
        query = "select * from phone";

        using (sqlcon = new SqlConnection(constr))
        {
            sqlcmd = new SqlCommand(query, sqlcon);
            sqlcon.Open();
            sqlda = new SqlDataAdapter(sqlcmd);
            DataSet ds=new DataSet();
            sqlda.Fill(ds);
            ListView1.DataSource = ds;
            ListView1.DataBind();
                                }
           }
    protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        //when press edit button on listview
        ListView1.EditIndex = e.NewEditIndex;
        loaddata();
    }
    protected void ListView1_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        //when press Cancel button on listview
        ListView1.EditIndex = -1;
        loaddata();
    }
    protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
        TextBox  number,name,adress;
        number  =(TextBox)e.Item.FindControl("TextBox1");
          name =(TextBox)e.Item.FindControl("TextBox4");
          adress =(TextBox)e.Item.FindControl("TextBox5");

          query = "insert into phone (Number,Name,Address) values('" + number.Text  + "','" + name.Text  + "','" + adress.Text + "')";
          using (sqlcon = new SqlConnection(constr))
          {
              sqlcmd = new SqlCommand(query, sqlcon);
              sqlcon.Open();
              sqlcmd.ExecuteNonQuery();
              loaddata();
        }
    }
    protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
       string id;
        id = ListView1.DataKeys[e.ItemIndex].Value.ToString();

        query ="delete from phone where Number='"+ id +"'";
        using (sqlcon = new SqlConnection(constr))
        {
            sqlcmd = new SqlCommand(query, sqlcon);
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            loaddata();
                    }
            }
    protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int id;
        id = (int)ListView1.DataKeys[e.ItemIndex].Value;
        TextBox number, name, adress;
        number =(TextBox) ListView1.Items[e.ItemIndex].FindControl("TextBox1");
        name = (TextBox)ListView1.Items[e.ItemIndex].FindControl("TextBox1");
        adress = (TextBox)ListView1.Items[e.ItemIndex].FindControl("TextBox1");

        //Here you can write the update qurey to update the value
    }

I hope you like this article….
Post your valuable comments …