Unable to Update rows of DataGridView from DB Query

Daryl Gill

i'm having problems with updating a DataGridView to rows from data from a database. The code I have is as followed:

    public Array ClientSearch(string Argument, string SearchType)
    {
        Connection.Open();
        string QueryStr = "SELECT ClientName,PostCode,ContactNo FROM ClientSearch WHERE "+SearchType+" LIKE %"+Argument+"%";



        SqlCommand Query = new SqlCommand(QueryStr, Connection);

        SqlDataReader ExecuteQuery = Query.ExecuteReader();

        DataTable ResultSet = new DataTable();

        ResultSet.Load(ExecuteQuery);
        Connection.Close();
        if (ResultSet.Rows.Count.Equals(0))
        {
            return null;
        }
        else
        {

            // Single-dimensional array (strings).
            System.Collections.ArrayList Results = new System.Collections.ArrayList();
            while (ExecuteQuery.Read())
            {
                Results.Add(ExecuteQuery.GetString(0));
            }

            return Results.ToArray();

        }
    }

And the method is invoked by:

    private void SearchBy_TextChanged(object sender, EventArgs e)
    {
        SQLCmdSet Database = new SQLCmdSet();
        string Param = "";
        if (this.ByName.Checked.Equals(true))
        {
            Param = "ClientName";
        }
        if (this.ByPostCode.Checked.Equals(true))
        {
            Param = "PostCode";
        }
        Array Array = Database.ClientSearch(this.SearchBy.Text, Param);

        foreach (string Element in Array)
        {
            this.ClientInfor.Rows.Add(Element);
        }
    }

The Grid its self has 3 Columns named:

ClientName

PostCode

ContactNo

I've tried the following link to assist http://www.rhyous.com/2010/05/28/how-to-query-a-database-in-csharp/ and tried manually modifying to return to the method which would handle the addition of rows. This provided no avail, then I searched some stackoverflow URLs to find the method which uses a while loop on ExecuteQuery.Read() but this also provided no use

Daryl Gill

The resolution was quite simple after further research:

I changed the Querying Method to return a DataTable by:

 public DataTable ClientSearch(string Argument, string SearchType)
        {
            Connection.Open();
            string QueryStr = "SELECT ClientName,PostCode,ContactNo FROM Clients WHERE " + SearchType + " LIKE '%" + Argument + "%'";
            DataTable DataT = new DataTable();
            SqlDataAdapter SQLDA = new SqlDataAdapter(QueryStr, Connection);
            SQLDA.Fill(DataT);
            return DataT;

        }

and also opened the connection to the Database, and the modified Searchby text box method has been changed to the following:

    private void SearchBy_TextChanged(object sender, EventArgs e)
    {
        SQLCmdSet Database = new SQLCmdSet();
        string Param = "";
        if (this.ByName.Checked)
        {
            Param = "ClientName";
        }
        if (this.ByPostCode.Checked)
        {
            Param = "PostCode";
        }

        this.ClientInfor.DataSource = Database.ClientSearch(this.SearchBy.Text,Param);

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding rows from db to a datagridview

From Dev

Unable to count the number of rows returned from query

From Dev

Correct the update query for marking duplicate rows in DB2

From Dev

Update each selected datagridview rows

From Dev

Update datagridview from a WindowForm

From Dev

MySQL unable to update multiple rows from while loop

From Dev

Unable to get DB::query() to work with variables from simpleXML

From Dev

Removing the selected rows from a DataGridView

From Dev

Removing the selected rows from a DataGridView

From Dev

How to update datagridview at same time of datainsetion in db

From Dev

How to update multiple rows from database in one query using codeigniter

From Dev

How to update multiple rows from database in one query using codeigniter

From Dev

Query a Datagridview.Rows or Dataset using Linq

From Dev

Query a Datagridview.Rows or Dataset using Linq

From Dev

MEAN: unable to update data to DB

From Dev

Unable to update DB by form data

From Dev

MEAN: unable to update data to DB

From Dev

Multiple rows from db

From Dev

Codeigniter - update query executed successfully but $this->db->affected_rows() returns false

From Dev

Count values from selected rows from dataGridView

From Dev

Count values from selected rows from dataGridView

From Dev

How to update and insert into db with @transactional on db query

From Dev

linq query to update multiple rows

From Dev

Multiple rows update into a single query

From Dev

How to select row from DataGridView with DB to a new DataGridView in C#

From Dev

Fill DataGridView from SQLite DB (C#)

From Dev

how to pull data from DB/datagridview/datatable

From Dev

pass checked datagridview rows from child form to main form datagridview

From Dev

Laravel DB query builder update with query data

Related Related

  1. 1

    Adding rows from db to a datagridview

  2. 2

    Unable to count the number of rows returned from query

  3. 3

    Correct the update query for marking duplicate rows in DB2

  4. 4

    Update each selected datagridview rows

  5. 5

    Update datagridview from a WindowForm

  6. 6

    MySQL unable to update multiple rows from while loop

  7. 7

    Unable to get DB::query() to work with variables from simpleXML

  8. 8

    Removing the selected rows from a DataGridView

  9. 9

    Removing the selected rows from a DataGridView

  10. 10

    How to update datagridview at same time of datainsetion in db

  11. 11

    How to update multiple rows from database in one query using codeigniter

  12. 12

    How to update multiple rows from database in one query using codeigniter

  13. 13

    Query a Datagridview.Rows or Dataset using Linq

  14. 14

    Query a Datagridview.Rows or Dataset using Linq

  15. 15

    MEAN: unable to update data to DB

  16. 16

    Unable to update DB by form data

  17. 17

    MEAN: unable to update data to DB

  18. 18

    Multiple rows from db

  19. 19

    Codeigniter - update query executed successfully but $this->db->affected_rows() returns false

  20. 20

    Count values from selected rows from dataGridView

  21. 21

    Count values from selected rows from dataGridView

  22. 22

    How to update and insert into db with @transactional on db query

  23. 23

    linq query to update multiple rows

  24. 24

    Multiple rows update into a single query

  25. 25

    How to select row from DataGridView with DB to a new DataGridView in C#

  26. 26

    Fill DataGridView from SQLite DB (C#)

  27. 27

    how to pull data from DB/datagridview/datatable

  28. 28

    pass checked datagridview rows from child form to main form datagridview

  29. 29

    Laravel DB query builder update with query data

HotTag

Archive