coding @ the speed of thought RSS 2.0
 Saturday, January 05, 2008
Asp.net projelerinde genelde verinin db den okunması için sqldatasource 
nesneleri kullanırız.Fakat kullanıcının arama yapması gibi durumlarda 
dinamik sql yaratmamız gerekebilir.Bu tür durumlarda Gridview nesnesinin
datasource özelliğine codebehind da yarattığımız bir DataSet nesnesini
atarız.





Örnek bir search

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection conSQL = new SqlConnection(
            ConfigurationManager.
               ConnectionStrings["XConnectionString"].
                  ConnectionString);
        SqlCommand cmdSQL = new SqlCommand();
       
        string strSQL = "select x,y,z from a_tbl
 where ";

        if (this.txtX.Text.Trim().Length > 0)
        {
            strSQL += "x like @X and ";
            cmdSQL.Parameters.Add(new
               SqlParameter("@X", this.txtX.Text.Trim()));
        }
        if (this.txtY.Text.Trim().Length > 0)
        {
            strSQL += "y like @Y and ";
            cmdSQL.Parameters.Add(
               new SqlParameter("@Y", this.txtY.Text.Trim()));
        }

        if (strSQL.Substring(strSQL.Length - 5, 5) == " and ")
            strSQL = strSQL.Substring(0, strSQL.Length - 5);
        if (strSQL.Substring(strSQL.Length - 7, 7) == " where ")
            strSQL = strSQL.Substring(0, strSQL.Length - 7);

        try
        {
            conSQL.Open();
        }
        catch (Exception excp)
        {
            this.lblSearchInfo.Text = "Hata oluştu -> " +
               excp.Message;
            return;
        }
        cmdSQL.Connection = conSQL;
        cmdSQL.CommandText = strSQL;
        SqlDataReader drdSQL = null;
        DataSet dsSQL = new DataSet();
        try
        {
            drdSQL = cmdSQL.ExecuteReader();
        }
        catch (Exception excp)
        {
            conSQL.Close();
            this.lblSearchInfo.Text = excp.Message;
            return;
        }
        dsSQL.Load(drdSQL, LoadOption.OverwriteChanges,
            new string[] { "a_tbl" });
        drdSQL.Close();
        conSQL.Close();
        this.gvwCus.DataSource = dsSQL;
        this.gvwCus.DataBind();
    }

Son iki satırda gördüğünüz gibi gvwCus Gridview'umuza dsSQL dataset'ini
dinamik olarak bağlıyoruz.Fakat eğer gridview un paging özelliğini true
olarak set edersek bu birsonraki sayfaya gitmeye kalktığımızda hata
almamıza neden olacaktır.

Bu sorun can sıkıcı olsa da çözümü basittir.İlk olarak DataSet nesnesini
ister class'ın en üstüne private static olarak tanımlayın, isterseniz
ViewState veya Session bazında saklayın, dataset'ın sınıfın her
üyesinden ulaşılabilecek hale getirin.

Bu durumda ben viewstate'i kullanıyorum.
Search'ün son iki satırını aşağıdaki gibi değiştiriyorum.

         ViewState["a_tbl"] = dsSQL;         
        this.gvwCus.DataSource = dsSQL;
       
this
.gvwCus.DataBind();
    }

Ardından Gridview'un PageIndexChanging eventine aşağıdaki kodu yazın.

    protected void gvwCus_PageIndexChanging(object sender,
      GridViewPageEventArgs e)
    {
        gvwCus.PageIndex = e.NewPageIndex;
        this.gvwCus.DataSource = (DataSet)ViewState["a_tbl"];
        gvwCus.DataBind();
    }

Sorununuz bu şekilde çözülmüş olur.

Saturday, January 05, 2008 9:44:16 PM (GTB Standard Time, UTC+02:00)  #    Comments [0] -
Software
Archive
<January 2008>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Levent YILDIZ
Sign In
Statistics
Total Posts: 88
This Year: 20
This Month: 4
This Week: 0
Comments: 28
Themes
Pick a theme:
All Content © 2008, Levent YILDIZ
DasBlog theme 'Business' created by Christoph De Baene (delarou)