Simple DataAdapter doesnt work

My original code was working. Then my coworker made a change to the DAL and I have to use DBDataAdapter. I don't get an error with this piece of code BUT the dataset is not filled..skips the
dbAdapter.Fill(ds); line and goes to "finally" section. Not sure what's wrong.

My ****Orginal**** code was like this:
SqlDataAdapter myAdapter = new SqlDataAdapter();
           cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "pSearchLastName";

                cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = cName;

                    myAdapter.SelectCommand = cmd;

****new code that doesnt work
using (DbConnection cn = this.Database.CreateConnection())
               using (DbCommand cmd = this.Database.GetStoredProcCommand("pSearchLastName"))
                   DbDataAdapter dbAdapter = null;
                     this.Database.AddInParameter(cmd, "@name", DbType.String, cName);
                       cmd.Connection = cn;
                       dbAdapter.SelectCommand = cmd;
                       dbAdapter.Fill(ds); //** doesnt even get to here
                       foreach (DataRow row in ds.Tables[0].Rows)

Open in new window

Who is Participating?
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
>Do I need to create the DBAdapter??

yes, you indeed need to do that.
what is "this.Database" as object?
Guy Hengel [angelIII / a3]Billing EngineerCommented:
do you have an catch (Exception ex) for your "try" ? you should have, and get the error described there.
CamilliaAuthor Commented:
let me look at it again. It doesnt go thru the exception but let me see again..
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

CamilliaAuthor Commented:
yes, i didnt have the right exception trap.I get:
Object reference not set to an instance of an object

but what's wrong with dbAdapter.SelectCommand = cmd; ??
howcome my orig code works and the new one doesnt? is it because i have:
DbDataAdapter dbAdapter = null; ??

Been at this for 4 hours now!
CamilliaAuthor Commented:
Do I need to create the DBAdapter??
// Create the DbDataAdapter.
            DbDataAdapter adapter = factory.CreateDataAdapter();
            adapter.SelectCommand = command;

I dont have CreateDataAdapter...
CamilliaAuthor Commented:
In my original code i have:
SqlDataAdapter myAdapter = new SqlDataAdapter();

but cant do that with DbDataAdapter: DbDataAdapter dbAdapter = new DbDataAdapter();
gives me an error...

>> what is "this.Database" as object?
I see this with this.Database ...trying it now...
DbDataAdapter dbAdapter = this.Database.GetDataAdapter();
CamilliaAuthor Commented:
Once again, thanks for helping me out. That "this.Database.GetDataAdapter()" worked.

You're a life saver :) Kamila.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.