Click here to Skip to main content
15,906,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So i'm doing the example from http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx[^] on stored procedures. Which is all well and good in console but I'm trying to apply it to a windows form. Below is what I have on a button click:

private void btnQ4_Click(object sender, EventArgs e)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
                        
            try
            {
                ArrayList list = new ArrayList();
                // create and open a connection object
                conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
                conn.Open();

                // 1. create a command object identifying
                // the stored procedure
                SqlCommand cmd = new SqlCommand("Ten Most Expensive Products", conn);

                // 2. set the command object so it knows
                // to execute a stored procedure
                cmd.CommandType = CommandType.StoredProcedure;

                // execute the command
                rdr = cmd.ExecuteReader();

                // iterate through results, printing each to console
                while (rdr.Read())
                {
                    dataGridView1.ColumnCount = 1;
                    dataGridView1.Columns[0].Name = "Top10";
                    dataGridView1.Columns[1].Name = "Units";
                    dataGridView1.Rows.Add(rdr["TenMostExpensiveProducts"].ToString() + "" + rdr["UnitPrice"].ToString());
                }
            }            
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            
        }



The while statement in bold is the part I'm having a problem with. I have no idea what object I should have the data read into. In the example it goes straight to console however should I be putting it into a datagridveiw? And if so how do I go about doing that? I've tried getting it into a richtextbox however a string will only hold one lines worth of data not a list. Any help will be much appreciated thanks.
Posted
Updated 12-Dec-11 1:29am
v2

In the above example, you have to use sqldatadapter in the place of sqldatareader, So the sqldataadapter can hold any number of records.

once you get data in to sqldataadapter, you have to assign to datatable

so data table can be assigned as a data source to any grid.

example:

C#
System.data.SqlAdapter da;
datatable dt;
da=new system.data.SqlAdapter ("select * from emp",connection)
dt.fill(da)


now bind dt to your grid
 
Share this answer
 
v3
Hi,


Can you please change your procedure name.
I think in your procedure name there ia a keyword name "most".
may your problem solve by change your procedure name.

thanks
 
Share this answer
 
Comments
WurmInfinity 12-Dec-11 8:11am    
Got it working now. I think since the procedure is considered in string format the key word wont effect it. Thanks anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900