on Leave a Comment

Update,Delete and Insert Data into FormView

now, i will explain how to data bind in formview and update,delete,cancle event .
here,aspx page


<asp:FormView ID="fv" runat="server" AllowPaging="True" OnPageIndexChanging="fv_PageIndexChanging">
        <HeaderTemplate>
            <table>
                <tr>
                    <td width="100px">
                        Id:
                    </td>
                    <td width="100px">
                        Name:
                    </td>
                    <td width="100px">
                        Marks:
                    </td>
                </tr>
            </table>
        </HeaderTemplate>
        <ItemTemplate>
            <table>
                <tr>
                    <td width="100px">
                        <asp:Label ID="lblid" runat="server" Text='<%#Eval("sid") %>'></asp:Label>
                    </td>
                    <td width="100px">
                        <asp:Label ID="lblname" runat="server" Text='<%#Eval("sname") %>'></asp:Label>
                    </td>
                    <td width="100px">
                        <asp:Label ID="lblmarks" runat="server" Text='<%#Eval("marks") %>'></asp:Label>
                    </td>
                </tr>
            </table>           
            <asp:LinkButton ID="btnedit" runat="server" OnClick="btnedit_Click">Edit</asp:LinkButton>
            <asp:LinkButton ID="btninsert" runat="server" onclick="btninsert_Click">Insert</asp:LinkButton>
            <asp:LinkButton ID="btndelete" runat="server" onclick="btndelete_Click">Delete</asp:LinkButton>
        </ItemTemplate>
        <EditItemTemplate>
            <table>
                <tr>
                    <td width="100px">
                        <asp:Label ID="lblid" runat="server" Text='<%#Eval("sid")%>'></asp:Label>
                    </td>
                    <td width="100px">
                        <asp:TextBox ID="txtname" runat="server" Text='<%#Bind("sname") %>'></asp:TextBox>
                    </td>
                    <td width="100px">
                        <asp:TextBox ID="txtmarks" runat="server" Text='<%#Bind("marks") %>'></asp:TextBox>
                    </td>
                </tr>
            </table>           
            <br>           
            </br>
            <asp:LinkButton ID="btnupdate" runat="server" OnClick="btnupdate_Click">Update</asp:LinkButton>
            <asp:LinkButton ID="btncancle" runat="server" OnClick="btncancle_Click">Cancle</asp:LinkButton>     
                  
        </EditItemTemplate>
    </asp:FormView>

After that write the following code aspx.cs page

using System;
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
{
     static SqlConnection con = new SqlConnection("Data Source -PC\\SQLEXPRESS;Initial Catalog;Integrated Security=True;Pooling=False");
     static SqlDataAdapter da = new SqlDataAdapter("select *from student",con);
     SqlCommandBuilder cmdb = new SqlCommandBuilder(da);
     DataTable dt = new DataTable();

      

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            da.Fill(dt);
            ViewState["dt"] = dt;
            bind();
        }
        else
        {
            dt = (DataTable)ViewState["dt"];
        }
    }

    protected void bind()
    {
        fv.DataSource = dt;
        fv.DataBind();
    }


    protected void fv_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {
        fv.PageIndex = e.NewPageIndex;
        bind();
    }
    protected void btnedit_Click(object sender, EventArgs e)
    {
        fv.ChangeMode(FormViewMode.Edit);
        bind();
    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        DataRow[] dr;
        dr = dt.Select("sid="+((Label)fv.FindControl("lblid")).Text);
        dr[0][1] = ((TextBox)fv.FindControl("txtname")).Text;
        dr[0][2] = ((TextBox)fv.FindControl("txtmarks")).Text;
        da.Update(dt);
        fv.ChangeMode(FormViewMode.ReadOnly);
        bind();

    }
    protected void btncancle_Click(object sender, EventArgs e)
    {
        fv.ChangeMode(FormViewMode.ReadOnly);
        bind();
    }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        DataRow[] dr;
        dr = dt.Select("sid="+((Label)fv.FindControl("lblid")).Text);
        dr[0].Delete();
        da.Update(dt);
        bind();
    }
    protected void btninsert_Click(object sender, EventArgs e)
    {
        int max=0;
        for (int i = 0; i < fv.PageCount; i++)
        {
            fv.PageIndex = i;
            bind();

            int h =Convert.ToInt32(((Label)fv.FindControl("lblid")).Text);
            if(max<h)
            {
             max = h;
            }
        }
        DataRow dr;
        dr = dt.Rows.Add();
        fv.PageIndex = fv.PageCount;
        bind();
        dr[0] = max + 1;
        fv.ChangeMode(FormViewMode.Edit);
        bind();
        da.Update(dt);      
      
    }
  

}

on Leave a Comment

How to Bind Data in Datalist

Here,aspx  page  design
<div>
        <asp:DataList ID="dl" runat="server">
            <HeaderTemplate>
                <table>
                    <tr>
                        <td width="100px">
                            Id:
                        </td>
                        <td width="100px">
                            Name:
                        </td>
                        <td width="100px">
                            Marks:
                        </td>
                    </tr>
                </table>
            </HeaderTemplate>
            <ItemTemplate>
                <table>
                    <tr>
                        <td width="100px">
                            <asp:Label ID="lblid" runat="server" Text='<%#Eval("sid") %>'></asp:Label>
                        </td>
                        <td width="100px">
                            <asp:Label ID="lblname" runat="server" Text='<%#Eval("sname") %>'></asp:Label>
                        </td>
                        <td width="100px">
                            <asp:Label ID="lblmarks" runat="server" Text='<%#Eval("marks") %>'></asp:Label>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    </div>
Now add the following namespaces in codebehind and After that write the following code
using System;
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 
{
     static SqlConnection con = new SqlConnection("Data Source=\\SQLEXPRESS;Initial Catalog;Integrated Security=True;Pooling=False");
     static SqlDataAdapter da = new SqlDataAdapter("select *from student",con);
     SqlCommandBuilder cmdb = new SqlCommandBuilder(da);
     DataTable dt = new DataTable();
       
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            da.Fill(dt);
            ViewState["dt"] = dt;
            bind();
        }
        else
        {
            dt = (DataTable)ViewState["dt"];
        }
    }

    protected void bind()
    {        
        dl.DataSource = dt;
        dl.DataBind();     
    }
on 4 comments

Create cookie in c#

Here simple code for createing cookie and value show on button click..!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    HttpCookie mycookie = new HttpCookie("malvik");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["malvik"] != null)
        {
         
            Response.Write(Request.Cookies["malvik"].Value);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        mycookie.Value = TextBox1.Text;
        mycookie.Expires = DateTime.Now.AddMinutes(1);
        Response.Cookies.Add(mycookie);
   
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        Page.Theme = "Theme1";
    }
}
on Leave a Comment

Bind Data Into DropDownLIst

SqlConnection con = new SqlConnection("ConnectionString");
SqlDataAdapter da = new SqlDataAdapter("Select * from TableName",con);
DataTable dt = new DataTable();
da.Fill(dt);

ddlcategory.DataSource = dt;
ddlcategory.DataTextField = "Name";
ddlcategory.DataValueField = "Id";
ddlcategory.DataBind();
ddlcategory.Items.Insert(0, "---select---");

on 1 comment

3 tier architecture example in asp.net with C#

Here I will how to create or implement 3-tier architecture for our project in asp.net 
Basically 3-Tier architecture contains 3 layers

1.    Property Layer 
2.    Business Logic Layer(BLL
3.    Data Access Layer(DAL)

Here I will explain each layer with simple example that is MangeCategory page

on Leave a Comment

Bind DataTable Data In GridView

Here I will Explain how to bind Datatable data into gridview

First we take one gridview in the aspx page. in this gridview we bind student table data.it has three column sid,sname,marks.
Aspx page
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"
            BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
            CellPadding="3" GridLines="Vertical" >      
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <Columns>        
                <asp:BoundField DataField="sid" HeaderText="Id" ReadOnly="True" />
                <asp:BoundField DataField="sname" HeaderText="Name" />
                <asp:BoundField DataField="marks" HeaderText="Marks" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>        
        </asp:GridView>

Now add following namespaces in codebehind
Aspx.cs Page
using System.Data;
using System.Data.SqlClient;
After that add following code in code behind

 static SqlConnection con = new SqlConnection("Data Source=MALVIK-PC\\SQLEXPRESS;Initial Catalog=malvik;Integrated Security=True;Pooling=False");
    static SqlDataAdapter da = new SqlDataAdapter("select *from student", con);
    DataTable dt = new DataTable();
 protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            da.Fill(dt);
            ViewState["dt"] = dt;
            bind();
        }
        else
        {
            dt = (DataTable)ViewState["dt"];
        }
    }
    protected void bind()
    {
        gv.DataSource = dt;
        gv.DataBind();
    }
on Leave a Comment

Using StreamReader To Read HTML File/TextFile

Here I will explain how to read text or html file using StreamReader
In previous articles I explained write a html file using StreamWriter,now I will explain how to read text or html file using StreamReader.
we need to write the following code in aspx page
<asp:Button ID="btnread" runat="server" Text="Read" onclick="btnread_Click" />      
Now in code behind add the following namespaces          
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnread_Click(object sender, EventArgs e)
    {
        StreamReader sr = new StreamReader(Request.PhysicalApplicationPath + "nam_email.htm");
        while (sr.Peek() >= 0)
        {
            Response.Write(sr.ReadLine() + "<br />");
        }
        sr.Close();
    }
}                  
           

on 1 comment

Using StreamWriter To Write HTML File

Here, I will explain how to add textbox text into HTML file using StreamWriter.
First,we need to write the following code in aspx page.
Default.aspx
<div>
        <table border="1">
            <tr>
                <td>
                    <asp:Label ID="lbnname" runat="server" Text="Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lbnemail" runat="server" Text="Email"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnwrite" runat="server" Text="Write" 
                        onclick="btnwrite_Click" />
                </td>
                <td>                    
                    <asp:Button ID="btnreset" runat="server" Text="Reset" 
                        onclick="btnreset_Click" />
                </td>
            </tr>         
        </table>
    </div>
Now, first we add namespace System.IO  in Default.aspx.cs  page  and also add html file  from addNewItem  and give "name nam_email.htm".
 Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnwrite_Click(object sender, EventArgs e)
    {       
        int id = 0,;   
        StreamWriter sw = new StreamWriter(Request.PhysicalApplicationPath + "nam_email.htm", true);      
       sw.WriteLine("<tr><td>" + id +"</td><td>" + txtname.Text + "</td><td>" + txtemail.Text + "</td></tr>");        
        sw.Close();
    }  
    protected void btnreset_Click(object sender, EventArgs e)
    {
        txtname.Text = string.Empty;
        txtemail.Text = string.Empty;
    } 
}
on Leave a Comment

What is .NET Framework?

.NET Framework is a complete environment that allows developers to develop, run, and deploy the following applications:
  • Console applications
  • Windows Forms applications
  • Windows Presentation Foundation (WPF) applications
  • Web applications (ASP.NET applications)
  • Web services
  • Windows services
  • Service-oriented applications using Windows Communication Foundation (WCF)
  • Workflow-enabled applications using Windows Workflow Foundation (WF)
on Leave a Comment

What is ASP.NET?

ASP.NET is a specification developed by Microsoft to create dynamic Web applications, Web sites, and Web services. It is a part of .NET Framework. You can create ASP.NET applications in most of the .NET compatible languages, such as Visual Basic, C#, and J#. The ASP.NET compiles the Web pages and provides much better performance than scripting languages, such as VBScript. The Web Forms support to create powerful forms-based Web pages. You can use ASP.NET Web server controls to create interactive Web applications. With the help of Web server controls, you can easily create a Web application.
aspdotnet-learn. Powered by Blogger.