What are we goining to Learn Today? We are goining to Create a DropDownList. This List will contains names of employees. Once you select an employee and press the Edit button , the details of the employee will be displayed in a form.
Confusing? Dont worry. Have a look at this Screenshoot of output.
What is the advantage of this program? or Where can I use this program? This can be used for Editing a particular record or for Deleting a Record.
Which language we are going to use? C# or VB ? We will use C#
Are we going to create a single aspx file(with code inside) or there will be code-behind file also? We first create a single file with code inside. Then We will create by the other method also. What Database are we going to use? We will use Oracle; and Table Name is Emp. This table has the fields:EmpID,EmpName Just go through these code:
|
code for test.aspx (with c# code inside) |
<%@ Page Language="C#"
Debug="true" %> OracleDataReader
myReader = myCmd.ExecuteReader(CommandBehavior.SingleRow); myReader.Close(); <asp:Button
id="editButton" onclick="editButton_click" runat="server"
Text="Edit"></asp:Button> |
The Other method is code-behind method. Here there will be two files: 1.test.aspx 2.test.aspx.cs Please compare the two methods and learn the difference. |
Code for test.aspx |
<%@
Page Language="C#" Debug="true"
CodeFile="test.aspx.cs" Inherits="test"
%> <html> <asp:Button
id="editButton" onclick="editButton_click" runat="server"
Text="Edit"></asp:Button> |
Code for test.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; using System.Data.OracleClient; public
partial class test : System.Web.UI.Page if (!Page.IsPostBack) protected void editButton_click(Object
sender, EventArgs e) myReader.Close(); } |
So you have seen the code.Do you have any doubts?Yes. What is the meaning of CommandBehavior.CloseConnection?In this case you need not type the line "myConn.Close();" .The connection will be closed automatically by the reader. |
What is the meaning of CommandBehavior.SingleRow?Only one Row will be read. |
To fillup the TextBox value, the following line is used here:TextBox1.Text=myReader["EmpID"].ToString();But, instead of TextBox, if there is a DropDownList, what code I should use?Drop1.SelectedItem.Value=myReader["EmpID"].ToString(); |
What is the meaning of "if (myReader.Read())" ? It is equal to the classic asp code: " If Not Rs.Eof " Which means, "If there are records to read then proceed."
|
What is the meaning of the line:" if (!Page.IsPostBack)" ?This means :" If the page is opened for the very first time". Actually when you type the url, the page is executed for the very first time. But, in the page, when you click the Edit button, the Request is sent to the Server and the Server is Returning the same page again. During this second time, the code inside the IF CONDITION will not be executed. But you may have a doubt that if the IF CONDITION is not executed, then how the DropDownList will be populated in the next time? Actually It uses the concept called View State for this purpose.
|
What is View State?Run the above aspx file from your browser. Then, in the browser, you select View > Source. Then Search for the word "VIEWSTATE".You will see the following line there: <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMT Q1NzU4MDQwMA9kFgI CAQ9k FgICAQ8QDxYEHg1EYXRhVGV4dEZpZWxkBQhlbX Bfbm FtZR4LXyFEYXRhQm91bmRn ZBAV AgZyYW1 2gHUmFtZXNoZBUCBn JhbWVzaAdSYW1lc2hkFCsDAmdnZGRk PryumC8M/js87r1KlKqEC25BmVg=" /> Actually, the Complete Data in the DropDownList is encoded and kept there as a hidden text. When you press the Edit button, these data also goes to the Server. The very same data is returned back to you again. So there is no need of opening the database and retrieving the data again using that IF CONDITION. Am I confusing. Then just leave this concept temporarily. You will learn later when you create a number of pages like this.
|
When I tried this code, I got an error against the line having OracleConnection
like this :"Compiler Error Message: CS0246: The type or namespace
name 'OracleConnection' could not be found (are you missing a using directive
or an assembly reference?)" Actually for the first time when you are using OracleConnection,etc, you should make a Reference. For this you can follow this Procedure: Website Menu >Add Reference > .Net Tab > Select System.Data.OracleClient > Click OK. |