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);
}
}
}
}
Related Article: Property of a class rises wildly
is a C# discussion thread by niggz that has 3 replies, was last updated 1 year ago and has been tagged with the keywords: c#, property, values.
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
© 2013 DaniWeb® LLC
Page generated in 0.0576 seconds
using 2.68MB