Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:53:06+00:00 2026-06-13T09:53:06+00:00

So I am practicing using a databases with ASP.Net… I have two datatable columns

  • 0

So I am practicing using a databases with ASP.Net…

I have two datatable columns from Northwind database. One is Product.ProductID and the other is Product.UnitsInStock. I need to compare that with a collection (SortedList) of objects. Essentially comparing variables Product ID and quantity(client side). The objects are a ICollection Value saved in a Session["cart"].

How can I compare the variables I have of product id and units in stock against the DataTable columns 1 is Products.ProductID and second column is Products.UnitsInStock?

Here is the query and roughly how I thought I could grab these variables from the database.

DataTable dt = new DataTable();

if (dt.Columns.Count != 0 &&
       dt.Rows.Count != 0)
    {
        int quantityOfUnit = 0;
        int productID = 0;
        for (int index = 0; index < dt.Columns.Count; index++)
        {
            if (index == indexOfUnitsInStock)//indexOfUnitsInStock = 1
            {
                quantityOfUnit = int.Parse(dt.Rows[0][index].ToString());
            }
            else//index = 0
            {
                productID = int.Parse(dt.Rows[0][index].ToString());
            }
        }

Building a new query:

foreach (object items in ((ShoppingCart)Session["cart"]).Values)
    {
        OleDbConnection conn = new OleDbConnection((string)Application["DBConnectionString"]);
    string selectionString =
        "SELECT Products.ProductID, Products.UnitsInStock " +
            "FROM Products" +
            "WHERE Products.ProductID = " +
            ((ShoppingCart)Session["cart"]).Values;
    DataTable dt = new DataTable();
    try
    {
        OleDbCommand cm = new OleDbCommand(selectionString, conn);
        OleDbDataAdapter da = new OleDbDataAdapter();
        da.SelectCommand = cm;   
        da.Fill(dt);
        da.Dispose();
    }
    catch(Exception ex)
    {
        txtUnderstockedItems.Text = "There was a problem connecting to the database: " + ex.Message ;
    }
    finally
    {
        conn.Close();
    }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:53:07+00:00Added an answer on June 13, 2026 at 9:53 am

    Firstly, you generally do not want to concatenate SQL in the way you are doing. Use parameterized SQL. If these are Integer values, you can cast them to integers and then put them in the query and achieve a similar result.

    If ShoppingCart.Values is a collection, then you have a few choices.

    1. Issue the query one at a time, for each item in Values
    2. Build a query that uses a sql list “WHERE Products.ProductID IN ( 1, 2, 3, 4)” – the list you would build by iterating values
    3. Create a stored procedure that performs the query and use an XML parameter to supply the list values.

    Out of the 3, the third would be my preference as its safer and probably easier to maintain over the long run.

    An example of #1 would be something like this…

    foreach (object obj in (ShoppingCart)Session["cart"].Values)
    {
      DataTable dt = new DataTable();
      int productID = (int)obj;
      string query = "SELECT * FROM Products WHERE ProductID = ?";   //yuck, oledb params
      using (OleDbConnection conn = new OleDbConnection (connStr))
      {
         //dont hold me to this syntax, its from my head
         OleDbCommand cmd = new OleDbCommand(query, conn);
         cmd.Parameters.Add(productID);
         OleDbDataAdapter da = new OleDbDataAdapter();
         da.SelectCommand = cm;   
         da.Fill(dt);
      }
      //do something with the data table
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am practicing Entity Framework. I am practicing Asp.net MVC framework using Razor Engine
I've been practicing with asp.net web forms and controls and have now become a
I'm using g++ on fedora linux 13. I'm just practicing some exercises from my
For practicing purposes, I’m about to create a new ASP.NET MVC 3.0 application. My
I am practicing to make a new module using Module::Starter. I have written a
I have been practicing writing a number of Ruby scrapers using Mechanize and Nokogiri.
So I'm trying to get productive practicing TDD in a ASP.NET webpages project, and
I am practicing using pointers. I have a pointer to an array of 3
I'm practicing using WCF with two projects in the same solution. The service is
Im practicing with one way hash encryption. I have a MVC model with Entity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.