another obj ref error/ need help here

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

larzeb

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
 
R

rbutch

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

larzeb

rik,

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

da.SelectCommand = cmd;
 
R

rbutch

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top