The things I learnt while I migrated from classic asp to asp.net is given here for my reference as well as for new beginners.

ASP.NET-CSHARP-DROPDOWNLIST-EDIT FORM-SOURCE CODE

Code for creating a dropdownlist and displaying the selected record in a form:

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" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OracleClient" %>
<script runat="server">
void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
string sConn= "Data Source=SALES;user id=scott;password=tiger;";
string sql = "select * from emp";
OracleConnection myConn = new OracleConnection(sConn);
OracleCommand myCmd=new OracleCommand(sql,myConn);
DropDownList1.DataTextField="EmpName";
myConn.Open();
DropDownList1.DataSource = myCmd.ExecuteReader(CommandBehavior.CloseConnection);
DropDownList1.DataBind();
}
}
void editButton_click(Object sender, EventArgs e) {
string sConn = "Data Source=SALES;User Id=ctmslocal;password=ctms123;";
string selected_EmpName= DropDownList1.SelectedItem.Text;
string sql="select * from emp where EmpName='" + selected_EmpName+ "'";
OracleConnection myConn= new OracleConnection(sConn);
OracleCommand myCmd=new OracleCommand(sql,myConn);
myConn.Open();

OracleDataReader myReader = myCmd.ExecuteReader(CommandBehavior.SingleRow);
if (myReader.Read())
{
TextBoxEmpID.Text=myReader["EmpID"].ToString();
TextBoxEmpName.Text=myReader["EmpName"].ToString();
}

myReader.Close();
myConn.Close();
}
</script>
<html>
<head><title>Test</title></head>
<body>
<form id="Form1" runat="server">
Select the Employee to be edited:
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>

<asp:Button id="editButton" onclick="editButton_click" runat="server" Text="Edit"></asp:Button>
<table border="1">
<tr>
<td>Employee Name</td>
<td>
<asp:TextBox id="TextBoxEmpID" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> Emp Name </td>
<td>
<asp:TextBox id="TextBoxEmpName" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
</html>

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>
<head><title>Test</title></head>
<body>
<form id="Form1" runat="server">
Select the Employee to be edited:
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>

<asp:Button id="editButton" onclick="editButton_click" runat="server" Text="Edit"></asp:Button>
<table border="1">
<tr>
<td>Employee Name</td>
<td>
<asp:TextBox id="TextBoxEmpID" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> Emp Name </td>
<td>
<asp:TextBox id="TextBoxEmpName" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
</html>

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
{
protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
string sConn = "Data Source=SALES;user id=ctmslocal;password=ctms123;";
string sql = "select * from emp";
OracleConnection myConn = new OracleConnection(sConn);
OracleCommand myCmd = new OracleCommand(sql, myConn);
myConn.Open();
OracleDataReader myReader = myCmd.ExecuteReader(CommandBehavior.CloseConnection);
DropDownList1.DataTextField = "EmpName";


DropDownList1.DataSource = myCmd.ExecuteReader(CommandBehavior.CloseConnection);
DropDownList1.DataBind();
}
}

protected void editButton_click(Object sender, EventArgs e)
{
string sConn = "Data Source=SALES;User Id=ctmslocal;password=ctms123;";
string selected_EmpName = DropDownList1.SelectedItem.Text;
string sql="select * from emp where EmpName='" + selected_EmpName+ "'";
OracleConnection myConn= new OracleConnection(sConn);
OracleCommand myCmd=new OracleCommand(sql,myConn);
myConn.Open();
OracleDataReader myReader=null;
myReader=myCmd.ExecuteReader(CommandBehavior.SingleRow);
if (myReader.Read())
{
TextBoxEmpID.Text=myReader["EmpID"].ToString();
TextBoxEmpName.Text=myReader["EmpName"].ToString();
}

myReader.Close();
myConn.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?)"
". What is the problem?

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.

Thanks for Your Visit

Google Search
Disclaimer and Copy Right