Solved

Read database but dont get all answers only first one

Posted on 2009-05-03
2
168 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
2 Comments
 
LVL 4

Accepted Solution

by:
cauos earned 300 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 200 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 45
mySql Syntax 7 33
SQL Query to display duplicates ? 6 27
Birthdays 3 21
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now