Sno Sname Class101 John 1std102 Mike 1std103 Sekar 2nd104 Mathew 3rd
Select Class<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>1std</asp:ListItem> <asp:ListItem>2nd</asp:ListItem> <asp:ListItem>3rd</asp:ListItem></asp:DropDownList><br />Select Srudent Name<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList><br /><asp:Button runat="server" Text="Search" OnClick="Unnamed1_Click" /><br /><asp:GridView runat="server" ID="gridview"></asp:GridView>
using System.Data.OleDb;using System.Data;using System.Data.SqlClient;public partial class ExcelSearch : System.Web.UI.Page{ DataTable dt = new DataTable(); DataSet ds = new DataSet(); string path = @"D:\student.xlsx"; protected void Page_Load(object sender, EventArgs e) { //Initially load student name in the second dropdown list from excel sheet if (!Page.IsPostBack) { OleDbConnection MyConnection = null; DataSet DtSet = null; OleDbDataAdapter MyCommand = null; MyConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';Extended Properties=Excel 12.0;"); MyConnection.Open(); MyCommand = new OleDbDataAdapter("select Sname from [Sheet1$]", MyConnection); DtSet = new System.Data.DataSet(); MyCommand.Fill(DtSet, "[Sheet1$]"); DropDownList2.DataSource = DtSet; DropDownList2.DataTextField = "Sname"; DropDownList2.DataValueField = "Sname"; DropDownList2.DataBind(); MyConnection.Close(); } } protected void Unnamed1_Click(object sender, EventArgs e) { try { string excelconstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';Extended Properties=Excel 12.0;"; OleDbConnection excelcon = new OleDbConnection(excelconstr); OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$] where class='" + DropDownList1.Text + "' and Sname='" + DropDownList2.Text + "'", excelcon); excelcon.Open(); OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(dt); gridview.DataSource = dt; gridview.DataBind(); excelcon.Close(); } catch (Exception ex) { } }}