Merhaba arkadaşlar bu makalemizde Excel sayfamızdaki Ad sütununa göre arama işlemi yapacağız. Excel sayfanızı Şekil 1 de olduğu gibi oluşturun. Sayfanıza TextBox, Button ve Label ekleyin.
Screenshot
Şekil 1
Şekil 2
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = " ";
//excel veritabanina baglanti yapiyoruz.
string str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("App_Data/personel.xlsx") + ";Extended Properties=Excel 12.0;";
OleDbConnection baglan = new OleDbConnection(str);
baglan.Open();
String sql = "Select * From [Personel$]";
OleDbCommand komut = new OleDbCommand(sql, baglan);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = komut;
da.Fill(dt);
baglan.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
//ad sutununa gore arama yapacagiz.
DataColumn[] dsutun = { dt.Columns["Ad"] };
dt.PrimaryKey = dsutun;
try
{
string ad = txtFind.Text;
DataRow drow = dt.Rows.Find(ad);
string goster = "Id= " + drow["Id"].ToString() + " " + "Personel Adı Soyadı= " + drow["Ad"].ToString() + " " + drow["Soyad"].ToString() + " " + "Departmanı= " + drow["departman"].ToString() + " " + "Mail= " + drow["Mail"].ToString();
Label1.Text += " " + goster;
}
catch
{
Label1.Text = "Aradığınız Kayıt Bulunamadı...";
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtFind" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
2 comments:
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.
diyor
Yorum Gönder