We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,644 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

The SelectCommand property has not been initianlized before calling 'Fill'

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);

                }

            }


            }

    }
3
Contributors
2
Replies
1 Hour
Discussion Span
8 Months Ago
Last Updated
9
Views
Trebron
Newbie Poster
11 posts since May 2010
Reputation Points: 22
Solved Threads: 0
Skill Endorsements: 0

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

SQLDataAdapter adapter = new SQLDataAdapter(SELECT query, connection string);
//Gonz
Light Poster
35 posts since Nov 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 2

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).

nmaillet
Posting Pro
542 posts since Aug 2008
Reputation Points: 111
Solved Threads: 104
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0576 seconds using 2.68MB