?
Solved

Simple DataAdapter doesnt work

Posted on 2008-11-16
7
Medium Priority
?
594 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:Camillia
  • 5
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22971732
do you have an catch (Exception ex) for your "try" ? you should have, and get the error described there.
0
 
LVL 7

Author Comment

by:Camillia
ID: 22971756
let me look at it again. It doesnt go thru the exception but let me see again..
0
 
LVL 7

Author Comment

by:Camillia
ID: 22971790
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!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:Camillia
ID: 22971816
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
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 22971829
>Do I need to create the DBAdapter??

yes, you indeed need to do that.
what is "this.Database" as object?
0
 
LVL 7

Author Comment

by:Camillia
ID: 22971847
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();
0
 
LVL 7

Author Comment

by:Camillia
ID: 22971920
Once again, thanks for helping me out. That "this.Database.GetDataAdapter()" worked.

You're a life saver :) Kamila.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question