ASP.NET MySQL Kayıt Arama

Asp.net örneklerimize mysql veritabanı üzerinde kayıt arama işlemi ile devam ediyoruz, önceki örneklerimizde MySQL bağlantısının nasıl kurulacağı ve Kayıtları Listeleme işlemlerini gerçekleştirmiştik. Şimdi sıra geldi girilen kayıtlar üzerinde aram işlemini yapmaya, ilk olarak form üzerinde bir GridView,textbox,label ve buton ekleyerek aşağıdaki form görüntüsünü oluşturuyoruz.
mysqlara1

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>www.aspnertornekleri.com</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>      
    <asp:Label id="Label1" runat="server" text="Aranan Öğrenci No"></asp:Label>
        <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
        <asp:Button id="Button1"  runat="server" Text="Ara" onclick="Button1_Click" 
            Width="71px"></asp:Button>
        <br />
       
        <br />
        <asp:GridView id="GridView1" runat="server"
		AutoGenerateColumns="False" onRowDataBound="GridView1_DataBound" >
            <HeaderStyle backcolor="#cccccc"></HeaderStyle>
            <AlternatingRowStyle backcolor="#e8e8e8"></AlternatingRowStyle>
            <Columns>
                <asp:TemplateField HeaderText="Öğrenci No">
                    <ItemTemplate>
                        <asp:Label id="Label2" runat="server"></asp:Label> 
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Adı">
                    <ItemTemplate>
                     <asp:Label id="Label3" runat="server"></asp:Label> 
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Soyadı">
                    <ItemTemplate>
                        <asp:Label id="Label4" runat="server"></asp:Label> 
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Doğum Yeri">
                    <ItemTemplate>
                       <asp:Label id="Label5" runat="server"></asp:Label> 
                    </ItemTemplate>
                </asp:TemplateField>
              
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

using System.Data;
using MySql.Data.MySqlClient;

aranma işlemi ve GridView içerisinde kayıtları gösterme işlemi kodlarını yazamadan önce bu iki namespace’i eklemeyi unutmuyoruz.

mysqlara2

String aranacak;
    void Page_Load(object sender, EventArgs e)
    {
        aranacak = this.TextBox1.Text;
    }

    void BilgileriGetir()
    {
        MySqlConnection baglanti = new MySqlConnection();
        MySqlCommand komut = new MySqlCommand();
        MySqlDataAdapter dtAdapter = new MySqlDataAdapter();
        DataSet ds = new DataSet();
        String baglanticumlesi, sorgu;

        baglanticumlesi = "Server=localhost;User Id=root; Password=123456; Database=okul; Pooling=false";
        sorgu = "SELECT * FROM ogrenci WHERE ono="+aranacak;

        baglanti.ConnectionString = baglanticumlesi;
        komut.Connection = baglanti;
        komut.CommandText = sorgu;
        komut.CommandType = CommandType.Text;
        dtAdapter.SelectCommand = komut;
        dtAdapter.Fill(ds);

        //*** http://www.aspnetornekleri.com ***//
        GridView1.DataSource = ds;
        GridView1.DataBind();

        dtAdapter = null;
        baglanti.Close();
        baglanti = null;

    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        BilgileriGetir();
    }
    protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
    {
        //*** Öğrenci No ***//
        Label Label2 = (Label)(e.Row.FindControl("Label2"));
        if (Label2 != null)
        {
            Label2.Text = (string) Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ono"));
        } //*** http://www.aspnetornekleri.com ***//
        //*** Adı ***//
        Label Label3 = (Label)(e.Row.FindControl("Label3"));
        if (Label3 != null)
        {
            Label3.Text = (string)DataBinder.Eval(e.Row.DataItem, "adi");
        }

        //*** Soyadı ***//
        Label Label4 = (Label)(e.Row.FindControl("Label4"));
        if (Label4 != null)
        {
            Label4.Text = (string)DataBinder.Eval(e.Row.DataItem, "soyadi");
        }
        //*** http://www.aspnetornekleri.com ***//
        //*** Doğum Yeri ***//
        Label Label5 = (Label)(e.Row.FindControl("Label5"));
        if (Label5 != null)
        {
            Label5.Text = (string)DataBinder.Eval(e.Row.DataItem, "dyeri");
        }
       
    }

You may also like...

Subscribe
Bildir
guest
1 Yorum
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
denemw

hocam bende “aranacak does not exits in the current context” hatası verdi ?

1
0
Yazı hakkındaki yorum, görüş ve önerilerinizi yazınx
()
x