Hi can anyone see what the problem is below

I am new to c# so not sure why this code does not excute?

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


    namespace Eight_Stage
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();    


            }

            private void btnClick_Click(object sender, EventArgs e)
            {
                string connectionString = null;
                SqlConnection sqlCnn;
                SqlCommand sqlCmd;
                SqlDataAdapter adapter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                int i = 0;
                string sql = null;

                connectionString = "Data Source=GGG-RRR-REP1;Initial Catalog=Reporting;User Id=user; Password=password;connect Timeout=0";

                sql = "Select Distinct PCode,Emails,Comments,Send_Option from [GGG-RRR-rep1].Reporting.dbo.myTable";
                sqlCnn = new SqlConnection(connectionString);
                try
                {


                    sqlCmd = new SqlCommand(sql, sqlCnn);

                    dgEmail.DataSource = ds;
                    sqlCnn.Open();
                    adapter.Fill(ds);

                    adapter.Dispose();
                    sqlCmd.Dispose();
                    sqlCnn.Close();



                }
                catch (Exception ex )
                {
                  MessageBox.Show("Error ! "+ ex.Message);

                }

            }


            }

    }

you need to add the select command to the data adapter before calling adapter.Fill
Example:

SQLDataAdapter adapter = new SQLDataAdapter(SELECT query, connection string);

It's exactly what the title of your post says it is. You need to give the SelectCommand property a command.

Hint: SqlDataAdapter (adapter) has a SelectCommand property and you initialized an SqlCommand class (sqlCmd).

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