Skip to content
Advertisement

C# DatagridView fill with class SingleResponsibility

Hi StackOverflow members, I decided to build a library project to improve myself. I created a class to code Book Operations but i couldn’t find out how to fill datagrid via class method. When i text this method into a button action it didn’t work. I found out a solution to make it happen. And here’s some code from my BookOperation class;

public void GetBookByName(Yönetici f1)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * from Kitaplar where Kitap_Adi like '%" + f1.textBox1.Text.Trim() + "%'", conn); 
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        f1.dataGridView1.DataSource = dt;
        conn.Close();
    }
    public void GetBookByAuthor(Yönetici f1)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * from kitaplar where Yazar like '%" + f1.textBox1.Text.Trim() + "%'", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        f1.dataGridView1.DataSource = dt;
        conn.Close();

    }
    public void GetBookByGenre(Yönetici f1)
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand("Select * from kitaplar where Tür like '%" + f1.textBox1.Text.Trim() + "%'", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        f1.dataGridView1.DataSource = dt;
        conn.Close();
    }

Yönetici is the name of my application screen of book operations. In the button action, it includes;

Kitap_islemleri ki = new Kitap_islemleri();
ki.GetBookByName(this);

is it okay for single responsibility or is there any different way? Can you please help me? That’s kind of weird using somemethod(this), is it wrong or good for a start? what do you think. How can i get data without using (this)? If there’s a way, can you hit it?

Advertisement

Answer

I suggest you to change your method parameters and return type with what you need. It seems your queries need just 1 criteria.

public DataTable GetBookByName(string bookName)
{
    conn.Open();
    SqlCommand cmd = new SqlCommand("Select * from Kitaplar where Kitap_Adi like '%" + bookName.Trim() + "%'", conn); 
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    conn.Close();
    return dt;
}

So you won’t need all the Yonetici page on your data access layer and with returned DataTable, you can bind it to your gridview on presentation layer.

var bookName = f1.textBox1.Text;
f1.dataGridView1.DataSource = ki.GetBookByName(bookname);

Part 2

public DataTable GetBooks(string filter, string criteria)
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Kitaplar where " + filter + " like '%" + criteria.Trim() + "%'", conn); 
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}

Usage

GetBooks("Kitap_Adi", "name");
GetBooks("Yazar", "author");
GetBooks("Tür", "kind")
2 People found this is helpful
Advertisement