Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to search data from my db and display data onto the fixed column in the gridview.
i tryed this but not working. please helpmeout



private void txtDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(Configuratiing = 
             "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
         
            string sqlQuery = null;
            sqlQuery = "select * from DistrictnT where District = '" + txtDistrict.Text + "'";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = sqlQuery;
            cmd.CommandType = System.Data.CommandType.Text;
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            try
            {
                while (dr.Read())
                {
                    dgvDistrict.Rows[dgvDistrict.Rows.Count - 1].Cells["clmTowns"].Value = 
                      dr[2].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message, "Error", 
                  MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                conn.Close();
            }
	}
Posted
Updated 21-Apr-13 21:08pm
v2
Comments
[no name] 21-Apr-13 20:37pm    
"but not working" is not a helpful description of any kind of a problem.
Chinmaya C 22-Apr-13 1:11am    
Could you please post some more description about the problem so that it will help us to answer.
Vani Kulkarni 22-Apr-13 2:40am    
What is the error or issue you are facing?

1 solution

There are a few things wrong with this:
0) You are concatenating strings to form an SQL statement, which leave you wide open to SQL Injection which can damage or destroy your database. Use parametrized queries instead.
1) You don't list your fields in the SELECT statement (which is a fairly poor idea) but you explicitly use the third (and only the third) column of the returned data, whether it exists or not. A better approach would be to only return the one column you are interested in - this reduces bandwidth used as well as ensuring that the data is available.
2) If your DataGridView has no rows, then your dgvDistrict.Rows[dgvDistrict.Rows.Count - 1] will fail with an indexing exception.
3) You write all the data in the same cell of the DataGridView, which kinda makes the loop redundant. (This is probably the problem you are concerned about) Instead, you need to use a variable to change the row number from 0 to the number of rows in the reader.

But don't do it like that - it is so liable to problems that it isn't worth doing.
If you want to search and filter the results, use a BindingSource to supply the whole data to the DataGridView, and use it's Filter property to show only the matching rows. It's quicker, simpler and a lot easier!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900