• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2077
  • Last Modified:

Cannot implicitly convert type 'System.Data.DataTable' to 'bool'

Hi experts, How do i go about solving this line of code?
private void btnCheckOut_Click(object sender, System.EventArgs e)
            {
                  if (dsCustomers.Tables["Customers"]) ========here
                  
                  {
                        BAL.Customer Customer = new BAL.Customer();
                        this.SetCustomerValues(Customer);
                        DAL.HalloweenDB.AddCustomer(Customer);

                  }
            
            }

thanks
0
SirReadAlot
Asked:
SirReadAlot
  • 8
  • 4
1 Solution
 
NightmanCTOCommented:
What are you trying to do? A table cannot be true or false. Are you looking to check the value of one of the columns perhaps?
0
 
SirReadAlotAuthor Commented:
Are you looking to check the value of one of the columns perhaps? I was.

but now I just want to insert some items into the database
0
 
NightmanCTOCommented:
if (dsCustomers.Tables["Customers"]!=null) //Checks if the object exists

Is that what you are looking for?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
SirReadAlotAuthor Commented:
original code
private void btnCheckOut_Click(object sender, System.EventArgs e)
          {
               if (!(dsCustomers.Tables["Customers"].Rows.Contains(txtEmail.Text))) ===>highlighted
               
               {
                    BAL.Customer Customer = new BAL.Customer();
                    this.SetCustomerValues(Customer);
                    DAL.HalloweenDB.AddCustomer(Customer);
//                    sEmail = Session["Email"];
//                    if (sEmail != txtEmail.Text & sEmail != "")
//                    {
//                         HalloweenDB.DeleteCustomer(sEmail);
//                    }
//                    Session.Remove("Customers");
//               }
//               else
//               {
//                    if (CustomerModified())
//                    {
//                         Customer Customer = new Customer();
//                         this.SetCustomerValues(Customer);
//                         HalloweenDB.UpdateCustomer(Customer);
//                    }
               }
               //lblCheckOut.Text = "The remaining check out functions have not been implemented.<br>" + "Click Cancel Order to start a new order, or Continue Shopping<br>" + "to modify your order.";

          }
0
 
SirReadAlotAuthor Commented:
will try you code
0
 
SirReadAlotAuthor Commented:
now it says
Object reference not set to an instance of an object.


if (dsCustomers.Tables["Customers"]!=null) //Checks if the object exists ======highlighted
0
 
SirReadAlotAuthor Commented:
this is the insert statment

public static object AddCustomer(BAL.Customer Customer)
            {
                  SqlConnection conHalloween = Connection();
                  SqlCommand cmdCustomer = new SqlCommand();
                  cmdCustomer.Connection = conHalloween;
                  cmdCustomer.CommandText = "Insert Into Customers Values ('" + Customer.Email + "', '" + Customer.LastName + "', '" + Customer.FirstName + "', '" + Customer.Address + "', '" + Customer.City + "', '" + Customer.State + "', '" + Customer.ZipCode + "', '" + Customer.PhoneNumber + "')";
                  conHalloween.Open();
                  cmdCustomer.ExecuteNonQuery();
                  conHalloween.Close();
                  return Customer;
            }
0
 
NightmanCTOCommented:
Try this:
if (dsCustomers!=null && dsCustomers.Tables["Customers"]!=null)

Is dsCustomers populated?
Does the "Customers" data table exist?
0
 
SirReadAlotAuthor Commented:
Is dsCustomers populated?
Does the "Customers" data table exist?

yes and there is data in it

i will try you code
0
 
SirReadAlotAuthor Commented:
it seems to have solved it,

but
private void btnCheckOut_Click(object sender, System.EventArgs e)
            {
                  if (dsCustomers!=null && dsCustomers.Tables["Customers"]!=null)
                  {
                        BAL.Customer Customer = new BAL.Customer(); ====this line is not called
                        this.SetCustomerValues(Customer);
                        DAL.HalloweenDB.AddCustomer(Customer);

                  }
            }
0
 
NightmanCTOCommented:
Well, clearly the problem is that it is evaluating that statement as false. So either dsCustomers is null or dsCustomers.Tables["Customers"] is null.
0
 
SirReadAlotAuthor Commented:
thanks

i did this

      if(!(dsCustomers!=null && dsCustomers.Tables["Customers"]!=null))
            
your is

if (dsCustomers!=null && dsCustomers.Tables["Customers"]!=null)

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

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now