Response.write only shows the first record from the database (this.session.sessionid)

168 views Asked by At

I've got a problem for the past few days. I will explain short what i've did. I have a table created in the database called 'Cart'. This Cart cointains: ClientID, Artical number, and quantity. In the ClientID, a session.sessionID stored. In the Artical just a number like 1012. And in quantity a number like 1 or 3.

What I would like to, is retrieve all the records, with the session.session id of the user. It does work in the page, but only the first record of the like 4-5 records that are in the cart table is shown. I think i comes due the problem that it looks for this.session.sessionidand when it found one, it doesn't look any further then that.

I've tried to loop through the query where sessions is. But it won't let me loop because it doesn't know for? Even if I loop the whole query outside of it like this:for (int i = 0; i < sessies.Length; i++) It will show more records.. but they are all the first records.. I know that was a stupid try but I can always try..

Looked for all over the internet but couldn't find the solution to this.

Hope to get a response soon from somebody. It would be gratefull.

Used the following code:

using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["GarageConnectionString"].ToString()))
    {
        string sessions = this.Session.SessionID;



            SqlCommand cmd = new SqlCommand("SELECT * FROM cart where ClientID='" + sessions + "'", cn);

            cn.Open();
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            rdr.Read();


            TextBox1.Text = rdr[2].ToString();


            Response.Write("<br>" + ("Sessie id "+rdr[1].ToString())); 
            Response.Write("<br>" + ("Artikel nummer "+rdr[2].ToString())); 
            Response.Write("<br>" + ("Aantal "+rdr[3].ToString())); 

            cn.Close();

    }
4

There are 4 answers

0
Michael Di Felice On BEST ANSWER

SqlDataReader advances to the next record in the set: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read%28v=vs.110%29.aspx

Call Read() in a loop, like so:

using (SqlConnection cn = newSqlConnection(ConfigurationManager.ConnectionStrings["GarageConnectionString"].ToString()))
{
    string sessions = this.Session.SessionID;



        SqlCommand cmd = new SqlCommand("SELECT * FROM cart where ClientID='" + sessions + "'", cn);

        cn.Open();
        SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        while (rdr.Read())
        {
           TextBox1.Text = rdr[2].ToString();


           Response.Write("<br>" + ("Sessie id "+rdr[1].ToString())); 
           Response.Write("<br>" + ("Artikel nummer "+rdr[2].ToString())); 
           Response.Write("<br>" + ("Aantal "+rdr[3].ToString())); 
        }

        cn.Close();

}
0
prog1011 On

Use While Loop- Example

        if (rdr .HasRows)
        {
            while (rdr .Read())
            {
                Console.WriteLine("{0}\t{1}", rdr .GetInt32(0),
                    rdr .GetString(1));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        rdr .Close();
0
Irfan TahirKheli On

You should loop through the datareader: ie:

while (rdr .Read())
{
    Console.WriteLine("{0}\t{1}", rdr .GetInt32(0),
                    rdr .GetString(1));
}
0
Richard On

Each call to SqlDataReader.Read() gets a single row, unless there are no more rows when it returns false.

So you need to loop to get all rows:

while (rdr.Read()) {
  // Use rdr methods to access the values from the current row.
}