another obj ref error/ need help here

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

im getting the dreaded:
Object reference not set to an instance of an object.
on the code below. pretty new to C# but i know im missing something basic here.

any help is appreciated
thanks
rik

public void GetEEs()
{

SqlConnection myConnection = new SqlConnection("Data Source=(Local);Initial Catalog=A39;Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("GetAuditTimeCard",myConnection);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@PERIOD",SqlDbType.DateTime,8);
da.SelectCommand.Parameters["@PERIOD"].Value = txtPpe.Text;
DataSet ds = new DataSet();
myConnection.Open();
da.Fill(ds);
Dg1.DataSource = ds.Tables["TimeCard"].DefaultView;
Dg1.DataBind();
da.Dispose();
myConnection.Close();
}

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
rik,

Did you create a new SqlCommand for your data adapter's SelectCommand?

// Create the SelectCommand.

cmd = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", conn);

cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15);

da.SelectCommand = cmd;

Lars
 
yes i did.
and the command type was a stored procedure.


da.SelectCommand.CommandType = CommandType.StoredProcedure;

thanks
rik


**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
rik,

Where are you assigning the newly created SqlCommand to the new SqlDataAdapter? Something like:

da.SelectCommand = cmd;
 
i figured this one out. there are so many ways of doing things in .net both right and wrong. but, this one works just fine.
many many thanks for your help
rik

SqlConnection myConnection = new SqlConnection("Data Source=(Local);Initial Catalog=A39;Integrated Security=SSPI");

SqlCommand command = new SqlCommand("GetAuditTimeCard",myConnection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@PERIOD", SqlDbType.DateTime).Value = txtPpe.Text;
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
myConnection.Open();
da.Fill(ds,"TimeCard");
Dg1.DataSource = ds.Tables["TimeCard"].DefaultView;
Dg1.DataBind();
myConnection.Close();
da.Dispose();

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
Back
Top