Code to Populate a drop down list

Q.4) Design a form and write code to:
a) Populate and display students name in drop down list.
b) Select a student from DDL and display its details in underlying text boxes.
c) Add a record.
d) Delete selected record.
e) Edit selected record.


Name of table StudentMaster(studID, studName, DOB, Per, courceName)

Ans.

Create a Table and name it as StudentMaster. In given example I have taken Sql server as a database.



After creating the Database and Table write code in ASP.NET using C#.

using System;
using System.Web.UI;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=StudentDB;Data Source=SARFAC184\\SQLEXPRESS");
SqlCommand cmd;
SqlDataReader dr;
string query, StudentName, StudentDOB, StudentPercentage, StudentCource;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillDDL();
}
}
public void FillDDL()
{

query = "select * from StudentMaster";
cmd = new SqlCommand(query, con);
con.Open();
dr = cmd.ExecuteReader();

DDLName.DataSource = dr;
DDLName.DataValueField = "studID";
DDLName.DataTextField = "studName";
DDLName.DataBind();
con.Close();
}

protected void btnAdd_Click(object sender, EventArgs e)
{

StudentName = txtName.Text;
StudentDOB = txtDOB.Text;
StudentPercentage = txtPer.Text;
StudentCource = txtCource.Text;


query = "insert into StudentMaster values('" + StudentName + "','" + StudentDOB + "'," + StudentPercentage + ",'" + StudentCource + "')";

con.Open();

cmd = new SqlCommand(query, con);

cmd.ExecuteNonQuery();

Response.Write("< script>alert('Record Added Successfully')");

con.Close();
ClearData();
FillDDL();

}
protected void btnDelete_Click(object sender, EventArgs e)
{
query = "delete StudentMaster where studID=" + DDLName.Text;

con.Open();

cmd = new SqlCommand(query, con);

cmd.ExecuteNonQuery();

Response.Write("< script>alert('Record Deleted Successfully')");

con.Close();
ClearData();
FillDDL();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
StudentName = txtName.Text;
StudentDOB = txtDOB.Text;
StudentPercentage = txtPer.Text;
StudentCource = txtCource.Text;
query="update StudentMaster set studName='"+StudentName+"', DOB='"+StudentDOB+"',Percentage='"+StudentPercentage+"',courceName='"+StudentCource+"' where studID=" + DDLName.Text;
con.Open();

cmd = new SqlCommand(query, con);

cmd.ExecuteNonQuery();

Response.Write("< script>alert('Record Updated Successfully')");

con.Close();
ClearData();
FillDDL();
}
protected void DDLName_SelectedIndexChanged(object sender, EventArgs e)
{
query = "select * from StudentMaster where studID=" + DDLName.Text;

con.Open();
cmd = new SqlCommand(query, con);

dr = cmd.ExecuteReader();

if (dr.Read())
{
txtName.Text = dr["studName"].ToString();
txtDOB.Text = dr["DOB"].ToString();
txtPer.Text = dr["Percentage"].ToString();
txtCource.Text = dr["courceName"].ToString();

}
con.Close();
}
protected void btnReset_Click(object sender, EventArgs e)
{
ClearData();
}

public void ClearData()
{
txtName.Text = " ";
txtDOB.Text = " ";
txtPer.Text = " ";
txtCource.Text = " ";
}
}

Run the application:

Post your comment

    Discussion

  • RE: Code to Populate a drop down list -harshada (11/26/15)
  • Thanks alot for this solved question paper...also upload the other subjects solved question paper or Atleast add all the question papers of all subjects till now...