Saturday, April 9, 2011


What is Web.Config File?


Web.config file, as it sounds like is a configuration file for the Asp .net web application. An Asp .net application has one web.config file which keeps the configurations required for the corresponding application. Web.config file is written in XML with specific tags having specific meanings.


What is Machine.config File?


As web.config file is used to configure one asp .net web application, same way Machine.config file is used to configure the application according to a particular machine. That is, configuration done in machine.config file is affected on any application that runs on a particular machine. Usually, this file is not altered and only web.config is used which configuring applications.






What can be stored in Web.config file?


There are number of important settings that can be stored in the configuration file. Here are some of the most frequently used configurations, stored conveniently inside Web.config file..




  1. Database connections

  2. Session States

  3. Error Handling

  4. Security

Database Connections:


The most important configuration data that can be stored inside the web.config file is the database connection string. Storing the connection string in the web.config file makes sense, since any modifications to the database configurations can be maintained at a single location. As otherwise we'll have to keep it either as a class level variable in all the associated source files or probably keep it in another class as a public static variable.


But it this is stored in the Web.config file, it can be read and used anywhere in the program. This will certainly save us a lot of alteration in different files where we used the old connection.


Lets see a small example of the connection string which is stored in the web.config file.







<configuration>


<appSettings>


<add key="ConnectionString"


value="server=localhost;uid=sa;pwd=;database=DBPerson" />


</appSettings>


</configuration>


As you can see it is really simple to store the connection string in the web.config file. The connection string is referenced by a key which in this case is "ConnectionString". The value attribute of the configuration file denotes the information about the database. Here we can see that if has database name, userid and password. You can define more options if you want.


There is a very good website that deals with all sorts of connection strings. Its called http://www.connectionstrings.com/ , in the website you will find the connection strings for most of the databases.


Lets see how we access the connection string from our Asp .net web application.







using System.Configuration;


string connectionString = (string )ConfigurationSettings.AppSettings["ConnectionString"];


The small code snippet above is all that is needed to access the value stored inside the Web.config file.


Session States:


Session in Asp .net web application is very important. As we know that HTTP is a stateless protocol and we needs session to keep the state alive. Asp .net stores the sessions in different ways. By default the session is stored in the asp .net process. You can always configure the application so that the session will be stored in one of the following ways.


1) Session State Service


There are two main advantages of using the State Service. First the state service is not running in the same process as the asp .net application. So even if the asp .net application crashes the sessions will not be destroyed. Any advantage is sharing the state information across a Web garden (Multiple processors for the same computer).


Lets see a small example of the Session State Service.






<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:55455" sqlConnectionString="data source=127.0.0.1;user id=sa;password='' cookieless="false" timeout="20"/>


The attributes are self explanatory but I will go over them.


mode: This can be StateServer or SqlServer. Since we are using StateServer we set the mode to StateServer.


stateConnectionString: connectionString that is used to locate the State Service.


sqlConnectionString: The connection String of the sql server database.


cookieless: Cookieless equal to false means that we will be using cookies to store the session on the client side.



2) SQL Server


The final choice to save the session information is using the Sql Server 2000 database. To use Sql Server for storing session state you need to do the following:


1) Run the InstallSqlState.sql script on the Microsoft SQL Server where you intend to store the session.


You web.config settings will look something like this:





<sessionState mode = "SqlServer" stateConnectionString="tcpip=127.0.0.1:45565" sqlConnectionString="data source="SERVERNAME;user id=sa;password='' cookiesless="false" timeout="20"/>

SQL Server lets you share session state among the processors in a Web garden or the servers in a Web farm. Apart from that you also get additional space to store the session. And after that you can take various actions on the session stored.


The downside is SQL Server is slow as compared to storing session in the state in process. And also SQL Server cost too much for a small company.


3) InProc:


This is another Session State. This one is mostly used for development purposes. The biggest advantage of using this approach is the applications will run faster when compared to other Session state types. But the disadvantage is Sessions are not stored when there is any problem that occurs with the application, when there is a small change in the files etc., Also there could be frequent loss of session data experienced..


Error Handling:


Error handling is one of the most important part of any web application. Each error has to be caught and suitable action has to be taken to resolve that problem. Asp.net web.config file lets us configure, what to do when an error occurs in our application.


Check the following xml tag in the web.config file that deals with errors:







<customErrors mode = "On">


<error statusCode = "404" redirect = "errorPage.aspx" />


</customErrors>


This tells the Asp.net to display custom errors from a remote client or a local client and to display a page named errorPage.aspx. Error "404" is "Page not found" error.


If custom error mode is turned "off" than you will see Asp.net default error message. This error messages are good for debugging purposes but should never be exposed to the users. The users should always be presented with friendly errors if any.


Security:


The most critical aspect of any application is the security. Asp.net offers many different types of security method which can be used depending upon the condition and type of security you need.


1) No Authentication:


No Authentication means "No Authentication" :) , meaning that Asp.net will not implement any type of security.


2) Windows Authentication:


The Windows authentication allows us to use the windows user accounts. This provider uses IIS to perform the actual authentication, and then passes the authenticated identity to your code. If you like to see that what windows user is using the Asp.net application you can use:



User.Identity.Name;


This returns the DOMAIN\UserName of the current user of the local machine.


3) Passport Authentication:


Passport Authentication provider uses Microsoft's Passport service to authenticate users. You need to purchase this service in order to use it.


4) Forms Authentication:


Forms Authentication uses HTML forms to collect the user information and than it takes required actions on those HTML collected values.


In order to use Forms Authentication you must set the Anonymous Access checkbox checked. Now we need that whenever user tries to run the application he/she will be redirected to the login page.







<authentication mode="Forms">


<forms loginUrl = "frmLogin.aspx" name="3345C" timeout="1"/>


</authentication>


<authorization>


<deny users="?" />


</authorization>


As you can see we set the Authentication mode to "Forms". The forms loginUrl is the first page being displayed when the application is run by any user.


The authorization tags has the deny users element which contains "?", this means that full access will be given to the authenticated users and none access will be given to the unauthenticated users. You can replace "?" with "*" meaning that all access is given to all the users no matter what.


Final Words:


As you have seen that Web.config file plays a very important role in the over all Asp.net application. There are a lot more features that I have not discussed which includes caching. Try using web.config file when you need to configure the overall application.





Mohammad Azam, also known as Azamsharp have been programming in .NET for 4 years. He is the author of several articles. Apart from the articles Azamsharp is also the Top 50 poster on Microsoft official forums (www.asp.net). At present Azamsharp is completing his undergraduate degree in Computer Science from University of Houston and also working as a .NET consultant for cSoft Technologies. You can reach Azamsharp at xMohammadAzamx (at) yahoo.com

Monday, March 21, 2011

Classes

1) First you need to make stored procedures. (To study just read Stored Procedures lesson).
2) Add folder in solution explorer naming app_code. And in this add ".cs" file as "Add new item" (by default class.cs).
The code in .cs file is this:

************************** Code in Class.cs ********************************

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

/// Summary description for Class1
//public class Class1 {
// public Class1()
// { }}

namespace ns
{
public abstract class clscon
{
protected SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString); // it must be //protected otherwise child class can not call it
public clscon() //To make a class from which provide connection to other classes.
{
con.ConnectionString=ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
}
}

public class clsdeptprp //This defines all the cells in your database.
{
public Int32 deptid {get;set;} //"get" directly takes value from database.
public string deptnam { get;set;} //"set" returns that value after changes to database.
}

public class clsdept :clscon //for a particular table handle all the functions as save,delete,update
{ //etc.
public void SaveRec(clsdeptprp p)
{
if(con.State==ConnectionState.Closed) //here in intelligence shell con appear only when we define it protected in parent class
con.Open();
SqlCommand cmd= new SqlCommand("insdept",con);
cmd.CommandType= CommandType.StoredProcedure;
cmd.Parameters.Add("@deptid",SqlDbType.Int).Value= p.deptid;
cmd.Parameters.Add("@deptnam",SqlDbType.VarChar).Value=p.deptnam;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}

public void DelRec(clsdeptprp p) //to delete the data from the database
{
if(con.State==ConnectionState.Closed)
con.Open();
SqlCommand cmd=new SqlCommand("deldept",con);
cmd.CommandType =CommandType.StoredProcedure;
cmd.Parameters.Add("@deptid",SqlDbType.Int).Value=p.deptid;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}

public void UpdateRec(clsdeptprp p) //to update the data from the database
{
if(con.State==ConnectionState.Closed)
con.Open();
SqlCommand cmd=new SqlCommand("upddept",con);
cmd.CommandType= CommandType.StoredProcedure;
cmd.Parameters.Add("@deptnam",SqlDbType.VarChar).Value=p.deptnam;
cmd.Parameters.Add("@deptid",SqlDbType.Int ).Value=p.deptid;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}

public List DispRec (Int32 id) //to display the data from the database
{
if(con.State==ConnectionState.Closed)
con.Open();
SqlCommand cmd=new SqlCommand("dispdept",con);
cmd.CommandType= CommandType.StoredProcedure;
cmd.Parameters.Add("@deptid", SqlDbType.Int).Value = id;
SqlDataReader dr= cmd.ExecuteReader();
List obj=new List();
while(dr.Read())
{
clsdeptprp p =new clsdeptprp();
p.deptid=Convert.ToInt32(dr["deptid"]);
p.deptnam=dr["deptnam"].ToString();
obj.Add(p);
}
dr.Close();
cmd.Dispose();
con.Close();
return obj;
}
}
}

***********************************************************************************

3) Design of classes. To see HTML click here....
















4)
************************** Code in default.aspx.cs *****************************

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 dept_class : System.Web.UI.Page
{
ns.clsdept obj = new ns.clsdept();
ns.clsdeptprp objprp = new ns.clsdeptprp();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ListBox1.DataBind();
}

public void listbind() //make a function to bind the ListBox
{
ListBox1.DataBind();
}

public void clrclass() //function to clear all the fields.
{
Label1.Text = "";
Label2.Text = "";
}

protected void insert_Click(object sender, EventArgs e)
{
if (update2.Visible == true)
{
update2.Visible = false;
update.Visible = true;
}
TextBox1.Visible = true;
TextBox2.Visible = true;
Label1.Visible = false;
Label2.Visible = false;
insert.Visible = false;
insert2.Visible = true;
}

protected void delete_Click(object sender, EventArgs e)
{
objprp.deptid = Convert.ToInt32(Label1.Text);
obj.DelRec(objprp); //the obj pass value to DeleRec define in "Class.cs"
clrclass();
ListBox1.DataBind();
}

protected void update_Click(object sender, EventArgs e)
{
if (insert2.Visible == true)
{
insert2.Visible = false;
insert.Visible = true;
TextBox1.Visible = false;
Label1.Visible = true;
}
TextBox2.Visible = true;
Label2.Visible = false;
update.Visible = false;
update2.Visible = true;
}

protected void clear_Click(object sender, EventArgs e)
{
clrclass();
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Visible = true;
Label2.Visible = true;
TextBox1.Visible = false;
TextBox2.Visible = false;
update2.Visible = false;
update.Visible = true;
insert2.Visible = false;
insert.Visible = true;
clrclass();
List p =obj.DispRec(Convert.ToInt32(ListBox1.SelectedValue));
Label1.Text = p[0].deptid.ToString();
Label2.Text = p[0].deptnam.ToString();
}

protected void insert2_Click(object sender, EventArgs e) //Event to insert value.
{
objprp.deptid = Convert.ToInt32(TextBox1.Text);
objprp.deptnam = TextBox2.Text;
obj.SaveRec(objprp);
clrclass();
ListBox1.DataBind();
TextBox1.Visible = false;
TextBox2.Visible = false;
insert2.Visible = false;
Label1.Visible = true;
Label2.Visible = true;
insert.Visible = true;
}

protected void update2_Click(object sender, EventArgs e)
{
objprp.deptid = Convert.ToInt32(TextBox1.Text);
objprp.deptnam = TextBox2.Text;
obj.UpdateRec(objprp); //the obj pass value to UpdRec define in "Class.cs"
clrclass();
ListBox1.DataBind();
TextBox2.Visible = false;
update2.Visible = false;
Label2.Visible = true;
update.Visible = true;
}
}

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

5) Running interface of Classes....





Stored Procedures

1) Open SQL Management Studio. Enter username and password.
2) In database which you create click on Programmability----> Stored Procedure option as in image.
















3) Right click on stored procedure and add new---> in open tab select all and delete.
4)Then in empty tag. Write code as shown in image 2. in place of Alter Procedure write create procedure and stored procedure name. And other line behind that are same.
5) Syntax for Display.










3) Syntax for Update.











6) Syntax for Delete.









7) Syntax for Insert.













Detail view without coding


First you need to make the database with stored procedures (name should be similar which you use in properties of sqldatasource )as we discuss in earlier lessons.

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

















2) Properties for Detail view













3) Properties of SqlDataSource
















4) Add Stored Procedures in SqlDataSource.















5)
Code for .aspx

*********************************************************************************
Initially need to make stored procedures of insert,delete,update and display. And use those in detail view without coding with SqlDataSource properties as shown in pictures.


********************************Web config ****************************



***************************.aspx **********************************

There is not any coding in .aspx or default.aspx because this detail view interface is without coding


6) Running interface.


Grid view with coding

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

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















2) Properties for Grid view















3) Events for Grid view













4) Code for .aspx


*********************** .aspx 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.SqlClient;
using System.Data;
using System.Configuration;

public partial class grdvw_wc : 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()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tblbook", con);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) //Event of grid view
{
GridView1.PageIndex = e.NewPageIndex;
sunny();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) //Event of grid view
{
GridView1.EditIndex = -1;
sunny();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) //Event of grid view
{
GridView1.EditIndex = e.NewEditIndex;
sunny();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) //Event of grid view
{
Int32 prc,bid;
string tit, aut, pub,img;
img = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtimg"))).Text;
tit = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txttit"))).Text;
aut = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtaut"))).Text;
pub = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtpub"))).Text;
prc = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtprc"))).Text);
bid = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("lblbid"))).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("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();
GridView1.EditIndex = -1;
sunny();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) //Event of grid view
{
GridView1.EditIndex = -1;
sunny();
}
}


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

5) Running interface



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




Detail view with coding

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















2) Properties set for Detail view
















3) Events applied in Detail view










4) Code for .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 detailvw_WC : 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()
{
SqlDataAdapter adp = new SqlDataAdapter("select * from tblbook", con);
DataSet ds = new DataSet();
adp.Fill(ds);
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e) // event of detail view
{
Int32 bid;
bid = Convert.ToInt32(((Label)(DetailsView1.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();
DetailsView1.PageIndex = -1;
sunny();
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e) // event of detail view
{
DetailsView1.PageIndex = e.NewPageIndex;
sunny();
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e) // event of detail view
{
if (e.NewMode == DetailsViewMode.Edit)
DetailsView1.ChangeMode(DetailsViewMode.Edit);
else if (e.NewMode == DetailsViewMode.Insert)
DetailsView1.ChangeMode(DetailsViewMode.Insert);
else
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
sunny();
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) // event of detail view
{
Int32 prc;
string tit, aut, img, pub;
tit = ((TextBox)(DetailsView1.FindControl("txttit"))).Text;
aut = ((TextBox)(DetailsView1.FindControl("txtaut"))).Text;
img = ((TextBox)(DetailsView1.FindControl("txtimg"))).Text;
pub = ((TextBox)(DetailsView1.FindControl("txtpub"))).Text;
prc = Convert.ToInt32(((TextBox)(DetailsView1.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("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 = e.Keys[0].ToString();
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
sunny();
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) // event of detail view
{
Int32 prc,bid;
string tit, aut, img, pub;
bid = Convert.ToInt32(((TextBox)(DetailsView1.FindControl("txtbid"))).Text);
tit = ((TextBox)(DetailsView1.FindControl("txttit"))).Text;
aut = ((TextBox)(DetailsView1.FindControl("txtaut"))).Text;
img = ((TextBox)(DetailsView1.FindControl("txtimg"))).Text;
pub = ((TextBox)(DetailsView1.FindControl("txtpub"))).Text;
prc = Convert.ToInt32(((TextBox)(DetailsView1.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();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
sunny();
}
}




***************** Web config Code**************************




5) Running interface