Fill: SelectCommand.Connection property has not been initialized.

I got this error on the program I am creating. After reading some forums and same problems on the net, I’ve finalyy figured out what the problem is..

The errors appears when I try to get records from the database. The code looks like:

SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.Text;
cmd.CommandText = “SELECT * FROM ProfileTable ORDER By MemberNo;”;

da = new SqlDataAdapter();
da.SelectCommand = cmd;

ds = new DataSet();
da.Fill(ds, “ProfileTable”);

what I missed out was this line:

cmd.Connection = cn;

Connection was not initialized. OK.

The code now looks like:

SqlCommand cmd = new SqlCommand();

cmd.Connection = cn;

cmd.CommandType = CommandType.Text;
cmd.CommandText = “SELECT * FROM ProfileTable ORDER By MemberNo;”;

da = new SqlDataAdapter();
da.SelectCommand = cmd;

ds = new DataSet();
da.Fill(ds, “ProfileTable”);

3 thoughts on “Fill: SelectCommand.Connection property has not been initialized.

  1. cmd.CommandType = CommandType.Text;
    cmd.CommandText = “SELECT * FROM ProfileTable ORDER By MemberNo;”;

    siyet. nosebleed. anong linggawahe to?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s