I'm working on a simple contact database style program I am using button click event to start an event that pulls an adjacent text box text and queries it however i can't seem to figure out how to update the DGV. here is what i have.

private void searchButton_Click_1(object sender, EventArgs e)
        {

            string connString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\clients.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
           SqlConnection conn = new SqlConnection(connString);
           string searchQuery;
            searchQuery = "select * from clients where [lastname] like'" + searchTB.Text + "%'";
            try
            {

                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(searchQuery, conn);

Want to update it here!

        }
        catch
        {
            MessageBox.Show("error" + e);
        }
    }

i have tried various snippets of code and i Am sure its a simple solution however i can't seem to find it

Edited 3 Years Ago by mike_2000_17: Fixed formatting

From all the examples i have seen this is the best i could come up with

[INDENT]try[/INDENT]
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(searchQuery, conn);
                DataSet ds = new DataSet();
                conn.Open();
                //da.Fill(ds);
                //dataGridView1.DataSource = ds;
            }
            catch
            {
                MessageBox.Show("error" + e);
            }

the two commented lines throw the exception but I'm not sure why.

From all the examples i have seen this is the best i could come up with

[INDENT]try[/INDENT]
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(searchQuery, conn);
                DataSet ds = new DataSet();
                conn.Open();
                //da.Fill(ds);
                //dataGridView1.DataSource = ds;
            }
            catch
            {
                MessageBox.Show("error" + e);
            }

the two commented lines throw the exception but I'm not sure why.

Well I don't know why they throw errors (look fine to me) but I always added one more line of code.

[INDENT]try[/INDENT]
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand(searchQuery, conn);
                DataSet ds = new DataSet();
                conn.Open();
                //da.Fill(ds);
                //dataGridView1.DataSource = ds;
[INDENT]dataGridView1.DataBind();[/INDENT]

            }
            catch
            {
                MessageBox.Show("error" + e);
            }

i guess it could be that I'm missing a reference

i am currently using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;

Well you references look good. Have you personally tried the code alostdruid posted? If so did you get errors on the two lines that he commented?

This article has been dead for over six months. Start a new discussion instead.