none
Manipulate SqlDataReader results RRS feed

  • Question

  • Hello Everyone,

    I have sql datareader which runs a stored procedure to return some sample data from the northwind database. It executes fine but all of the examples I have found just do a console.write with the data. Can anyone please help me move some into a variable I can do something with like bind to a listview, gridview, or something I can show all of the data with?

    try
                {
                    conn.ConnectionString = (connString);
                        
                    using (var command = new SqlCommand("Ten Most Expensive Products", conn)
                    {
                        CommandType = CommandType.StoredProcedure
                    })
                    {
                        //Open connection
                        conn.Open();
                        
                        //Execute Procedure
                        rdr = command.ExecuteReader();
    
                        while (rdr.Read())
                        {
                            System.Diagnostics.Debug.WriteLine("Product: {0,-25} Price: ${1,6:####.00}",
                                rdr["TenMostExpensiveProducts"].ToString(),
                                rdr["UnitPrice"].ToString();
                                                 
                                              }
                       
                    }
                }


    t

    Wednesday, March 27, 2013 6:45 PM

Answers

  • This link would answer your QUery

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=143

    Else if you want to assing varaibles you can use

    string str;

    While(dr.read)

    {

    str = dr["TenMostExpensiveProducts"].ToString();

    }


    Abhinav
    http://bishtabhinav.wordpress.com/


    • Edited by AB82 Wednesday, March 27, 2013 6:53 PM
    • Marked as answer by helpdevelop Wednesday, March 27, 2013 7:57 PM
    Wednesday, March 27, 2013 6:52 PM

All replies

  • This link would answer your QUery

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=143

    Else if you want to assing varaibles you can use

    string str;

    While(dr.read)

    {

    str = dr["TenMostExpensiveProducts"].ToString();

    }


    Abhinav
    http://bishtabhinav.wordpress.com/


    • Edited by AB82 Wednesday, March 27, 2013 6:53 PM
    • Marked as answer by helpdevelop Wednesday, March 27, 2013 7:57 PM
    Wednesday, March 27, 2013 6:52 PM
  • Thank you this you answered my question perfectly and I plan to mark your response as the answer shortly but I was wondering if you know how to make the header text of the gridview something different from the database column name?

    t

    Wednesday, March 27, 2013 7:31 PM
  • Go to the design view of the page and change the Header text field i changed the CITY name Customisedcity


    Abhinav
    http://bishtabhinav.wordpress.com/

    Wednesday, March 27, 2013 8:12 PM