Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

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;
                 conn.Open();

                try
                {
                    myAdapter.SelectCommand = cmd;
                    myAdapter.Fill(ds);
                   

****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);
                
                   cn.Open();
            
                   try
                   {
 
                       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

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

do you have an catch (Exception ex) for your "try" ? you should have, and get the error described there.
Avatar of Camillia

ASKER

let me look at it again. It doesnt go thru the exception but let me see again..
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!
Do I need to create the DBAdapter??
// Create the DbDataAdapter.
            DbDataAdapter adapter = factory.CreateDataAdapter();
            adapter.SelectCommand = command;

I dont have CreateDataAdapter...http://msdn.microsoft.com/en-us/library/fks3666w.aspx
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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();
Once again, thanks for helping me out. That "this.Database.GetDataAdapter()" worked.

You're a life saver :) Kamila.