Monday, March 21, 2011

Form view with coding

First you need to make the database as we discuss in earlier lessons

Your database look like this :










1) Design of Form view to see HTML code click here.....










2) Properties for Form view
















3) Events for Form view












4) Code for .aspx


******************** Code default.aspx *********************


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

public partial class firmvw : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
con.Open();
if (!Page.IsPostBack)
sunny();
}
public void sunny() //make a function to bind the formview
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tblbook", con);
DataSet ds = new DataSet();
adp.Fill(ds);
FormView1.DataSource = ds;
FormView1.DataBind();
}


protected void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e) //event of formview
{
Int32 bid;
bid = Convert.ToInt32(((Label)(FormView1.FindControl("lblbid"))).Text);
SqlCommand cmd = new SqlCommand("delete from tblbook where bookid=@bid", con);
cmd.Parameters.Add("@bid", SqlDbType.Int).Value = bid;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
FormView1.PageIndex = -1;
sunny();
}
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e) //event of formview
{
Int32 prc, bid;
string tit, aut, img, pub;
bid = Convert.ToInt32(((TextBox)(FormView1.FindControl("txtbid"))).Text);
tit = ((TextBox)(FormView1.FindControl("txttit"))).Text;
aut = ((TextBox)(FormView1.FindControl("txtaut"))).Text;
img = ((TextBox)(FormView1.FindControl("txtimg"))).Text;
pub = ((TextBox)(FormView1.FindControl("txtpub"))).Text;
prc = Convert.ToInt32(((TextBox)(FormView1.FindControl("txtprc"))).Text);
SqlCommand cmd = new SqlCommand("insert into tblbook(bookid,booktit,bookaut,bookimg,bookpub,bookprc) values(@bid,@tit,@aut,@img,@pub,@prc)", con);
cmd.Parameters.Add("@tit", SqlDbType.VarChar).Value = tit;
cmd.Parameters.Add("@aut", SqlDbType.VarChar).Value = aut;
cmd.Parameters.Add("@img", SqlDbType.VarChar).Value = img;
cmd.Parameters.Add("@pub", SqlDbType.VarChar).Value = pub;
cmd.Parameters.Add("@prc", SqlDbType.Int).Value = prc;
cmd.Parameters.Add("@bid", SqlDbType.Int).Value = bid;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
FormView1.ChangeMode(FormViewMode.ReadOnly);
sunny();
}
protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e) //event of formview
{
if (e.NewMode == FormViewMode.Edit)
FormView1.ChangeMode(FormViewMode.Edit);
else if (e.NewMode == FormViewMode.Insert)
FormView1.ChangeMode(FormViewMode.Insert);
else
FormView1.ChangeMode(FormViewMode.ReadOnly);
sunny();
}

protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e) //event of formview
{
Int32 prc;
string tit, aut, img, pub;
tit = ((TextBox)(FormView1.FindControl("txttit"))).Text;
aut = ((TextBox)(FormView1.FindControl("txtaut"))).Text;
img = ((TextBox)(FormView1.FindControl("txtimg"))).Text;
pub = ((TextBox)(FormView1.FindControl("txtpub"))).Text;
prc = Convert.ToInt32(((TextBox)(FormView1.FindControl("txtprc"))).Text);
SqlCommand cmd = new SqlCommand("update tblbook set booktit=@tit, bookaut=@aut,bookimg=@img,bookpub=@pub,bookprc=@prc where bookid=@bid", con);
cmd.Parameters.Add("@bid", SqlDbType.Int).Value = e.Keys[0].ToString();
cmd.Parameters.Add("@tit", SqlDbType.VarChar).Value = tit;
cmd.Parameters.Add("@aut", SqlDbType.VarChar).Value = aut;
cmd.Parameters.Add("@img", SqlDbType.VarChar).Value = img;
cmd.Parameters.Add("@pub", SqlDbType.VarChar).Value = pub;
cmd.Parameters.Add("@prc", SqlDbType.Int).Value = prc;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
FormView1.ChangeMode(FormViewMode.ReadOnly);
sunny();
}
protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e) //event of formview
{
FormView1.PageIndex = e.NewPageIndex;
sunny();
}
}




********************** Web config file **************************************





5) Running interface




No comments:

Post a Comment