Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Read database but dont get all answers only first one

Posted on 2009-05-03
2
Medium Priority
?
176 Views
Last Modified: 2012-05-06
Why do I only get the first answer in my sql statment from the database?

I know there are at least four CustomerID
public void ReadOrder(String customerid)
    {
        String myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= C:\\Documents and Settings\\MFL\\Dokumenter\\Visual Studio 2005\\WebSites\\WebSite\\WebSite\\Tenta1\\Nwind.mdb";
        OleDbConnection myConnection = new OleDbConnection(myConnectionString);
        string myInsertQuery = "SELECT * FROM Orders WHERE (CustomerID) ='" +customerid +"'";
        OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
        myCommand.Connection = myConnection;
        myConnection.Open();
        OleDbDataReader rdr = myCommand.ExecuteReader();
        rdr.Read();
 
        if (rdr.HasRows)
        {
            Order temp = new Order();
            temp.OrderId = rdr[0].ToString();
            temp.Customer = rdr[1].ToString();
            temp.Employee = rdr[2].ToString();
            temp.OrderDate = rdr[3].ToString();
            temp.RequriedDate = rdr[4].ToString();
            temp.ShippingDate = rdr[5].ToString();
            temp.ShipVia = rdr[6].ToString();
            temp.Freight = rdr[7].ToString();
            temp.ShipName = rdr[8].ToString();
            temp.ShipAdress = rdr[9].ToString();
            temp.ShipCity = rdr[10].ToString();
            temp.ShipRegion = rdr[11].ToString();
            temp.ShipPostalCode = rdr[12].ToString();
            temp.ShipCountry = rdr[13].ToString();
            order.Add(temp);
 
 
            Response.Redirect("Uppgift3cc.aspx");
        }
 
        myConnection.Close();
    }
}

Open in new window

0
Comment
Question by:Mickeys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
cauos earned 1200 total points
ID: 24290043
because you should add rdr.Read() to loop through all the result that come from the query

public void ReadOrder(String customerid)
    {
        String myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= C:\\Documents and Settings\\MFL\\Dokumenter\\Visual Studio 2005\\WebSites\\WebSite\\WebSite\\Tenta1\\Nwind.mdb";
        OleDbConnection myConnection = new OleDbConnection(myConnectionString);
        string myInsertQuery = "SELECT * FROM Orders WHERE (CustomerID) ='" +customerid +"'";
        OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
        myCommand.Connection = myConnection;
        myConnection.Open();
        OleDbDataReader rdr = myCommand.ExecuteReader();
        while(rdr.Read())
        {
        if (rdr.HasRows)
        {
            Order temp = new Order();
            temp.OrderId = rdr[0].ToString();
            temp.Customer = rdr[1].ToString();
            temp.Employee = rdr[2].ToString();
            temp.OrderDate = rdr[3].ToString();
            temp.RequriedDate = rdr[4].ToString();
            temp.ShippingDate = rdr[5].ToString();
            temp.ShipVia = rdr[6].ToString();
            temp.Freight = rdr[7].ToString();
            temp.ShipName = rdr[8].ToString();
            temp.ShipAdress = rdr[9].ToString();
            temp.ShipCity = rdr[10].ToString();
            temp.ShipRegion = rdr[11].ToString();
            temp.ShipPostalCode = rdr[12].ToString();
            temp.ShipCountry = rdr[13].ToString();
            order.Add(temp);
 
 
            Response.Redirect("Uppgift3cc.aspx");
        }
        }
        myConnection.Close();
    }
}

Open in new window

0
 
LVL 9

Assisted Solution

by:tillgeffken
tillgeffken earned 800 total points
ID: 24290051
Cause you're not looping through the DataReader.

public void ReadOrder(String customerid)
    {
        String myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= C:\\Documents and Settings\\MFL\\Dokumenter\\Visual Studio 2005\\WebSites\\WebSite\\WebSite\\Tenta1\\Nwind.mdb";
        OleDbConnection myConnection = new OleDbConnection(myConnectionString);
        string myInsertQuery = "SELECT * FROM Orders WHERE (CustomerID) ='" +customerid +"'";
        OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
        myCommand.Connection = myConnection;
        myConnection.Open();
        OleDbDataReader rdr = myCommand.ExecuteReader();
 
        if (rdr.HasRows)
        {
           while (dr.Read())
           {
            Order temp = new Order();
            temp.OrderId = rdr[0].ToString();
            temp.Customer = rdr[1].ToString();
            temp.Employee = rdr[2].ToString();
            temp.OrderDate = rdr[3].ToString();
            temp.RequriedDate = rdr[4].ToString();
            temp.ShippingDate = rdr[5].ToString();
            temp.ShipVia = rdr[6].ToString();
            temp.Freight = rdr[7].ToString();
            temp.ShipName = rdr[8].ToString();
            temp.ShipAdress = rdr[9].ToString();
            temp.ShipCity = rdr[10].ToString();
            temp.ShipRegion = rdr[11].ToString();
            temp.ShipPostalCode = rdr[12].ToString();
            temp.ShipCountry = rdr[13].ToString();
            order.Add(temp);
           }
 
            Response.Redirect("Uppgift3cc.aspx");
        }
 
        myConnection.Close();
    }
}

Open in new window

0

Featured Post

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!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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