Camillia
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.StoredProcedur e;
cmd.CommandText = "pSearchLastName";
cmd.Parameters.Add("@name" , SqlDbType.NVarChar).Value = cName;
conn.Open();
try
{
myAdapter.SelectCommand = cmd;
myAdapter.Fill(ds);
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.StoredProcedur
cmd.CommandText = "pSearchLastName";
cmd.Parameters.Add("@name"
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)
...
do you have an catch (Exception ex) for your "try" ? you should have, and get the error described there.
ASKER
let me look at it again. It doesnt go thru the exception but let me see again..
ASKER
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!
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!
ASKER
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
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.GetDataAdapt er();
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.GetDataAdapt
ASKER
Once again, thanks for helping me out. That "this.Database.GetDataAdap ter()" worked.
You're a life saver :) Kamila.
You're a life saver :) Kamila.