Asp.Net web programming

Add to Favourites
Post to:

ASP.NET How can u set ConnectonString in web.config file ? & How can u access it in WebForm ? Example: ----------------------------------Web.config File:------------------------------------------- -----------------------------------WebForm1.aspx.cs------------------------ using System; using System.Data; using System.Web; using System.Data.SqlClient; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace WebconfigSettings { public class WebForm1 : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { SqlConnection conn=new SqlConnection(); SqlDataAdapter da; DataSet ds=new DataSet(); conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["connstr"]; Response.Write("ok"); string sq="select * from authors"; da=new SqlDataAdapter(sq,conn); da.Fill(ds,"authors"); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } } } tag in web.config file it could be within tag or out of tag ? ( tag in web.config file should be within tag. (See question 1. Response Object This object allows you to send HTTP response data to a client and contains information about that response. Response.Write(); ( Writes information to an HTTP output content stream. Response.Flush(); ( Sends all currently buffered output to the client. ( Forces all currently buffered output to be sent to the client. Flush can be called multiple times during request processing. Example [Visual Basic, C#] The following example flushes the buffer to the output stream.[Visual Basic] Response.Flush() [C#] Response.Flush(); Response.Clear() ( Clears all content output from the buffer stream. (Example [Visual Basic, C#] The following example clears the current buffer stream. [Visual Basic] Response.Clear() [C#] Response.Clear(); 3.What is Response.Clear() & Response.Flush() ? Response.Clear() ( Clears all content output from the buffer stream. Response.Flush() ( Sends all currently buffered output to the client. ( Forces all currently buffered output to be sent to the client. Flush can be called multiple times during request processing. What is Response.Redirect() & Server.Transfer() ? (Response.Redirect takes the round trip more. How means the request page will go to the server from there it will come to client browser requested page, & then it redirects to new page using the specied URL. (Server.Transfer reduces the round trip. How means when request goes to the server it will be processed by the server & server redirects the user to a new page using specified URL path to the page without coming back to client browser requested page. Response.Redirect("WebForm2.aspx"); Redirects a client to a new URL and specifies the new URL. Server.Transfer("WebForm2.aspx"); Terminates execution of the current page and begins execution of a new page using the specified URL path to the page. (Response.Redirect() redirects a client to a new URL and specifies the new URL (Server.Transfer() clears the contents(all the controls placed on WebForm1) of existing Webpage(WebForm1.aspx) & displays the o/p of WebForm2 in WebForm1 only.[Visual Basic] Overloads Public Sub Transfer(String) [Visual Basic] Overloads Public Sub Redirect(String) [Visual Basic] Public Sub Clear() 5. How u can retrieve the values of one form fields in other WebForm in asp.Net ? (Whenever u want to access the values of one webform in other webform then use HTML Form but not server side Form. (A form on an ASP.NET page is generally posted to the same page. This enables the page to take an advantage of view state. (You cannot post a server-side form to a different page.If u do need to post a form to a different page, use a standard HTML form instead of an ASP.NET form & use the following line of code to post the form an ASP.NET page. //This is HTML Form (Type this code in WebForm1.aspx <%@ page language=vb debug=true %> //This is HTML Form Enter user name ******************************* (Type this code in WebForm2.aspx <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebApplication224.WebForm2" %>



[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button in the upper-left corner of the page. 6. Can we use more than one Form in one webpage ? (The ans is no. In Asp.net we can use only one Form in one web page. Means in Asp.net we can use only one server side form tag, but not more than one. ( In Asp.net one wegpage(WebForm1.aspx) allows only one server side form & any no of HTML forms. Ex: <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebApplication226.WebForm2" %>

7. What is method=”post” & action attributes of Form tag in Asp.net ? (In ASP, the default value of the Method attribute of Form tag is Get. But, in ASP.NET, the default value of this attribute is Post. Hence, u do not need to specify this value explicitly. (The server - side form control automatically posts contents to the same page. Hence, u do not need to add the Method & Action attributes. (The default value of Action attribute in ASP.NET is the name of the page. (This implies that the following 2 lines of code are equivalent.
8. What is method=”post” & method=”Get” ? METHOD Attribute | method Property (The value of the METHOD attribute determines how to send the data to the server. (METHOD attribute sets or retrieves how to send the form data to the server. get Append the arguments to the action URL and open it as if it were an anchor. post Send the data through an HTTP post transaction. Remarks (When using the post method, there is no theoretical limit to the amount of data that can be sent to the HTTP server. The amount of data may be constrained by the physical limits of the client computer. (When using the get method to send data to an HTTP server, the amount of data that can be sent is limited by the maximum length of a URL. In this case the URL cannot be longer than 2048 bytes. From ASP: (When u request an HTML or an ASP file from a web server, the protocal that is invoked & used to manage the intraction b/w the requesting client browser & the requested web server is the Hyper Text Transfer Protocol(HTTP). The Http service, which manages the HTTP protocol, runs on the web server. HTTP is a client/server protocol that species how messages, especially b/w a browser & a web server, can be transported over the internet. It accepts requests & delivers appropriate responses via HTML. (HTTP is also known as request & response protocol. Note that while the Web server hosts the HTTP service, it remains a passive interactor in the information interchange. Communication must always be initiaed by the browser. 9. Which are the Page events ? And tell the sequense of exicution of Page events ? The page events are: Init, Load, PreRender and Unload. Init (inherited from Control) Occurs when the server control is initialized, which is the first step in the its lifecycle. Load (inherited from Control) Occurs when the server control is loaded into the Page object. PreRender (inherited from Control) Occurs when the server control is about to render to its containing Page object. Unload (inherited from Control) Occurs when the server control is unloaded from memory. 10. What for Page_Unload( ) event ? This event occurs when the server control is unloaded from memory. Use: ( Unload event is used to perform any final clean-up, such as closing files, database connections and discarding objects etc. (Unload event is used to perform any final cleanup before the control is torn down/ unloaded from memory. Control authors generally perform cleanup in Dispose and do not handle this event 11. What is Dispose ? Page.Dispose(); Dispose is a method of Page object. Dispose method is used to perform any final cleanup before the control is torn down/ unloaded from memory. References to expensive resources such as database connections must be released in this phase. Dispose method enables a server control to perform final clean up before it is released from memory. 12. What is PlaceHolder control ?[Visual Basic] Overridable Public Sub Dispose() Implements IDisposable.Dispose (PlaceHolder control acts as a container control. (This control is mainly used to add /remove the controls dynamically at run time. (The PlaceHolder Web server control enables you to place an empty container control within the page and then dynamically add, remove, or loop through child elements at run time. The control only renders its child elements. 13. What is AdRotator Control ? (The AdRotator control is an ASP.NET rich control that is used to create & manipulate banner advertisements in web pages. 14. Can we add Webuser control dynamically at run time ? Yes we can add. 15. File name extensions : (WebForm/webpage - WebForm1.aspx ( Global.asax (Usercontrol - WebUserControl1.ascx (WebServise - Service1.asmx 15. How u can display all error messages in a list ? (This can be done by using Validation Summary Control Validation Summary Control : (Displays a list of all validation errors on the Web page. (The ValidationSummary control allows you to summarize the error messages from all validation controls on a Web page in a single location. The summary can be displayed as a list, a bulleted list, or a single paragraph, based on the value of the DisplayMode property. Ex: <%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebApplication231.WebForm2" %>
Top of Form Bottom of Form Top of Form Bottom of Form Top of Form Bottom of Form Top of Form Bottom of Form 16. How u can bind data into a DropDownList Control ? Dim con As New SqlConnection Dim com As New SqlCommand Dim dr As SqlDataReader Dim sql As String con.ConnectionString = "server=chandru; uid=sa; pwd=; database=pubs" con.Open() com.Connection = con sql = "select * from authors" com.CommandText = sql dr = com.ExecuteReader() DropDownList1.DataSource = dr DropDownList1.DataTextField = "au_lname" DropDownList1.DataBind() //DataBind() method of DropDownList control is called. con.Close() 17. What is the difference b/w DataGrid & DataList control ? DataGrid (The DataGrid control renders a tabular, data-bound grid. (DataGrid has advanced features such as support for Paging & Sorting through records. (You can display data in this control without using any templates. Just bind a data source to the control. It automatically displays the data in a grid format. (The DataGrid control supports selection, editing, deleting, paging, and sorting. Means we can edit the data, sort the data (Paging can be done (It has physical outlook/appearance (Does not require templates. Without applying templates DataGrid will work. If necessary then we can apply the templates. (The DataGrid control contains built-in functionality to display its contents in page segments. The number of items on the page is determined by the PageSize property. If no value is specified for the PageSize property, the DataGrid control displays ten items on the page at a time. (By default, paging is disabled. To enable paging, set the AllowPaging property to true (DataGrid & DataList controls support event bubbling, templates, and the Datakeys collection. DataList ( It does not have physical outlook/appearance (We can edit the data & sort the data. This requires a lot more coding. (Since it does not have physical outlook/appearance, templates are necessary (Uses templates (Paging cannot be done (By default, a DataList control shows data within an HTML table. (Displays the items from a data source by using templates. You can customize the appearance and contents of the control by manipulating the templates that make up the different components of the DataList control, such as the ItemTemplate and HeaderTemplate. (You can manipulate the control's layout and content by defining templates. 17. What is the difference b/w DataList & Repeater control ? Repeater control (It is readonly control. (We cannot modify the data, We cannot sort the data (Paging cannot be done (It does not have physical outlook (Since it does not have physical outlook/appearance, templates are necessary (The Repeater control has no built-in layout or styles; you must explicitly declare all HTML layout, formatting, and style tags within the control's templates. (The Repeater control has no built-in selection or editing support. 18. Differences between the Repeater, DataList, and DataGrid Web server controls The following table lists the Web server controls that display lists and differences between them. Type of Control Purpose Features Table General-purpose, programmable table. Can display any combination of HTML text and controls. Supports a TableCell control that can be set to display information. Does not use templates for display. Is not inherently data-bound. Exposes a model for dynamically creating rows (TableRow controls) and cells (TableCell controls). Repeater Simple, read-only output Has no built-in support for selecting or editing items. Has no default appearance; you must lay out the list by creating templates. The list can be vertical, horizontal, all on one line, or in any format you specify. Has no default paging; all data is displayed in a single list. Can define separators between elements using a template. Supports custom functionality that can be applied to items (for example, specifying an "Add to shopping cart" button for each item). DataList List output with editing Non-tabular lists (for example, comma-delimited lists) Easily customizable output Has a table appearance by default, but can be configured to show any list output. Can customize look of list extensively. Has an auto-format option. WYSIWYG template editing. Supports styles for custom look of items. Can define separators between elements using a template. Has editable contents (contents displayed in text boxes or other controls, based on data type of bound data). Supports single selection. Multiple selection requires custom code. Supports either horizontal (columns) or vertical (rows) layout of the data items. Has no default paging; all data is displayed in a single list. Supports custom functionality that can be applied to items (for example, specifying an "Add to shopping cart" button for each item). DataGrid Full-featured list output with editing Paged reporting Has a grid appearance by default. Can customize look of grid extensively. Has an auto-format option. Can specify output using bound columns, columns of buttons or hyperlinks, and custom columns created using templates. Has no separator template. However, the grid renders in a table, and you can specify table border size and color. Supports WYSIWYG template editing. Items support styles for custom look. Can edit, update, and delete contents. Supports single selection. Multiple selection requires custom code. Has optional paged output. Supports sorting. Supports custom functionality that can be applied to items (for example, specifying an "Add to shopping cart" button for each item). 18. What is Event Bubbling ? (The DataList, DataGrid and the Repeater controls support event bubbling. This feature enables them to handle the events raised in their child controls. The event raised in a child control bubbles up to the containing control, which then executes a subroutine to handle the event. This enables u to avoid writing event handlers for each child control separately. (Suppose the Repeater control has a number of link buttons bound to a data source. A single subroutine can handle the click event raised by any of the link buttons. (Controls that have no DataSource property must be placed in a Repeater control & the data must be bound to the parent control. 19. How can u retrieve data from XML file ? (Using ReadXml( ) method of DataSet object we can retrieve data from XML file. DataSet ds=new DataSet(); ds.ReadXml(MapPath("XMLFile1.xml")); DataGrid1.DataSource=ds; DataGrid1.DataBind(); 20.How u can read & write the data into XML file ? ds.ReadXml(MapPath("XMLFile1.xml")); ds.WriteXml("c://test.xml"); (Writing data into xml file: using System.Data; using System.Data.SqlClient; SqlConnection conn=new SqlConnection(); SqlDataAdapter da; DataSet ds=new DataSet(); conn.ConnectionString="data source=SERVER;initial catalog=pubs;integrated security=SSPI;persist security info=False;workstation id=MACHINE102;packet size=4096"; conn.Open(); string sel="select * from authors"; da=new SqlDataAdapter(sel,conn); da.Fill(ds,"sample"); dataGrid1.DataSource=ds; dataGrid1.DataMember="sample"; ds.WriteXml("c://test.xml"); 20. What for Web.Config File ? Application Configuration Files (Application configuration files contain settings specific to an application. This file contains configuration settings that the common language runtime reads (such as assembly binding policy, remoting objects, and so on), and settings that the application can read. (ASP.NET configuration files are called Web.config. 21. What is AspCompat attribute of Page directive ? AspCompat ( AspCompat is the attribute of Page directive. (When set to true, this allows the page to be executed on a single-threaded apartment (STA) thread. This allows the page to call STA components, such as a component developed with Microsoft Visual Basic 6.0. Setting this attribute to true also allows the page to call COM+ 1.0 components that require access to the unmanaged Active Server Pages (ASP) built-in objects. These are accessible through the ObjectContext object or the OnStartPage method. The default is false. Note   Setting this attribute to true can cause your page's performance to degrade. 22. What are the other attributes of Page directive ? Language AutoEventWireup Src Debug Trace TraceMode Inherits AspCompat ClassName EnableSessionState EnableViewState 23. Can we run Asp application in Asp.net ? ( Yes. (We can run .asp file in Asp.net. But in the page directive we have to set AspCompat attribute to true. 24. Application_Start event of Global.asax file will be fired every time when the users request the website/webapplication ? (No ( Application_Start event of Global.asax file will be fired only once for the first time when the user request the website, but not every time. 25. what are the events of Global.asax file ? Application_Start(Object sender, EventArgs e) Application_End(Object sender, EventArgs e) Application_BeginRequest(Object sender, EventArgs e) Application_EndRequest(Object sender, EventArgs e) Application_Error(Object sender, EventArgs e) Application_AuthenticateRequest(Object sender, EventArgs e) Session_Start(Object sender, EventArgs e) Session_End(Object sender, EventArgs e) 24. "Select au_lname, au_fname from authors"; Here will it display all the columns of authors table or only au_lname, au_fname columns in a Grid ? ( only au_lname, au_fname columns of authors table are displayed in a Grid.But not all the columns. EX: using System.Data.SqlClient; SqlConnection conn=new SqlConnection(); SqlDataAdapter da; DataSet ds=new DataSet(); conn.ConnectionString="initial catalog=pubs;integrated security=SSPI;persist security info=False;workstation id=SYS5;packet size=4096"; conn.Open(); string str="Select au_lname, au_fname from authors"; da=new SqlDataAdapter(str,conn); da.Fill(ds,"authors"); DataGrid1.DataSource=ds; DataGrid1.DataBind(); 24(a) In the same above example for a Grid if you set AutoGenerateColumns="False" then will it display the columns of query in a Grid ? (No. (By default for a Grid AutoGenerateColumns property is true. If we set AutoGenerateColumns="False" then it will not display the columns of query in the Grid. Then to pupulate the data into a Grid we have to apply BoundColumn to a Grid. 25. Have u worked on Security ? How u can do that ? (Refer the project Login_Project & run it & see how security works To support security, Asp.net has comes up with 2 facilities Authentication Authorization Authentication (Authentication can be defined as the process, which is used to verify the (validity of the) credentials of the user. (Authentication is the process of discovering and verifying the identity of a principal/user by examining the user's credentials and validating those credentials against some authority. Authorization (Authorization is the process of granting access rights to system resources on the basis of user’s identity. (Access to resources is granted to users through the process of authorization. This process follows the authentication process. (After authenticating a principal/user, the next step is to determine whether that principal has permission to access the resources it is requesting. This process is known as authorization. (Authorization is the process of determining whether a principal is allowed to perform a requested action. Authorization occurs after authentication and uses information about the principal's identity and roles to determine what resources the principal can access. You can use .NET Framework role-based security to implement authorization. (Settings in Web.config file. (http://localhost/Login_Project This will automatically redirect to Login.aspx ( http://localhost/Login_Project/WebForm2.aspx This will automatically redirect to Login.aspx. Because access to this page(WebForm2.aspx) is denied/resticted. Because the user is not authenticated by Webserver. () Ex: using System.Data.SqlClient; using System.Web.Security; private void btnLogin_Click(object sender, System.EventArgs e) { string xpwd; xpwd=Getpwd(this.TextBox1.Text); Label7.Text=xpwd; if(this.TextBox2.Text==xpwd) { FormsAuthentication.RedirectFromLoginPage(this.TextBox1.Text,false); } else { errMsg.Text="The supplied user Id or Password was not correct. If u do not have an account, use the link on this page to create one."; } } (If u want to allow everyone to access a particular page then do these settings in web.config file. (No authentication) ( To allow John and deny everyone else, one might construct the following configuration section: 26. What is Caching ? Write the syntax for caching ? (Caching is a performance improving technique. It involves storage of frequently accessed data in the memory of the system so that users can access the data quickly. Otherwise the data will have to be read from hard disk every time user requests it. This consumes system resources & slows the response. Hence caching is used to optimize performance & response time. (On the Web too, caching is used to store recently visited web pages in the browser memory. So if a user wishes to go back to a previous page, the browser simply loads the page from its memory & does not have to request the server to send the page again. The user gets a quick response, the server is not loaded, & the network traffic is reduced. (One of the most important factors in building high-performance, scalable Web applications is the ability to store items, whether data objects, pages, or parts of a page, in memory the initial time they are requested. You can store these items on the Web server or on other software in the request stream, such as the proxy server or browser. This allows you to avoid recreating information that satisfied a previous request, particularly information that demands significant processor time, or other resources, on the server when it is created. Known as caching, it allows you to use a number of techniques to store page output or application data across HTTP requests and reuse it. Thus, the server does not have to recreate information, saving time and resources. (Syntax : <%@ OutputCache Duration=”30” VaryByParam=”None” %> (Duration in seconds ( VaryByParam=”None” means we are not passing any arguments. [Visual Basic] Public ReadOnly Property Response As HttpResponse

Join the .NET Community

Description
Asp.Net web programming

Comments
Satya Santosh
By: Satya Santosh
607 days 22 hours 58 minutes ago

nice article

Arslan Saleem Butt
By: Arslan Saleem Butt
341 days 22 hours 18 minutes ago

I WANNA DONLOAD THIS HOW???]

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy
4 Followers

Your Facebook Friends on WizIQ

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect