vini
vini

Reputation: 4742

Error showing up Fill: SelectCommand.Connection property has not been initialized

  DataSet ds = new DataSet();

    SqlDataAdapter cmd = new SqlDataAdapter("Select * FROM event", con);
    cmd.Fill(ds, "Table");

Getting the same error again and again tried everything

please help

Upvotes: 3

Views: 4509

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176936

EDIT

Try like this , missing element in this might be commandtype which is added in below code and also open connection for databse

  SqlConnection sqlcon;
   using(sqlcon = new SqlConnection("Data Source=Servername;Initial 
       Catalog=Marketing;Integrated Security=SSPI"))
   {

        SqlCommand cmd = new SqlCommand("Select * FROM event", sqlcon);
        cmd.CommandType = CommandType.Text;
        sqlcon.Open();
        da = new SqlDataAdapter(sqlcmd);
        da.Fill(dt);
   }

Note : used and then disposed all within using statements. which is also added above


do this before calling fill method

SqlConnection conn= new 
         SqlConnection("Data Source=Servername;Initial Catalog=Marketing;Integrated Security=SSPI");

SqlDataAdapter cmd = new SqlDataAdapter("Select * FROM event", con);
   cmd.Fill(ds, "Table"); 

Upvotes: 4

AnandPhadke
AnandPhadke

Reputation: 13506

SqlConnection myconn = new SqlConnection(YourConnString);
SqlCommand mycmd = new SqlCommand();

try{ myconn.Open(); }
catch (Exception ex) { MessageBox("Error");}
mycmd.Connection = myconn;

DataSet ds = new DataSet();
DataTable dt = new DataTable();

String sqlStr = "Select * FROM event";
SqlDataAdapter da = new SqlDataAdapter(sqlStr, myconn);
da.Fill(ds);
dt = ds.Tables[0];

Upvotes: 0

Related Questions