Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,

I developed a desktop application and I want to generate a SqlDataReader to fill the gridview.

the is class
C#
class operation{
SqlConnection con = new SqlConnection(Connection.connectionString);
        SqlCommand com = new SqlCommand();
public SqlDataReader rd;

        public SqlDataReader getdata(string query)
        {
            com.CommandText = query;
            com.Connection = con;
            con.Open();
            rd = com.ExecuteReader();

            con.Close();
            return rd;      
        }
}


and the button to generate
 private void button2_Click(object sender, EventArgs e)
{
           oper.getdata("Select * From Customers");
           rd = oper.rd;
           if (rd.HasRows)
           {
               while (rd.Read())
               {

               }
           }
           con.Close();
       }

I have a lot of errors. I hope to find the answer.

Thanks a lot!
Posted
Updated 6-Nov-13 9:43am
v2
Comments
Richard MacCutchan 6-Nov-13 16:50pm    
I have a lot of errors
Well I'm afraid my mind reading skills are a bit rusty; care to tell us what they are?
beljk 6-Nov-13 19:55pm    
First , i want when i click on the button , fill all data from the customer table in the gridview by using sqlreader but i don't know how , so this is my simple code i think that's need something like in the while loop , and is the method right or wrong ??? i hope to understand me
Richard MacCutchan 7-Nov-13 5:00am    
Take a look at this article on the use of datareaders. You may also like to study the dataadapter class on MSDN.

Hi Please try this

C#
protected void button2_Click(object sender, EventArgs e)
  {
  SqlDataReader sqlDataReader = GetData(query, conString);//conString is Connection String
  }


And 'GetData' method is as below

public SqlDataReader GetData(string queryString, string connectionString)
    {

        SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand command = new SqlCommand(queryString, connection);
            SqlDataReader reader = command.ExecuteReader();
            SqlDataReader sqlDataReader = reader;
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Customer Id");
            dataTable.Columns.Add("Customer Name");
            while (sqlDataReader.Read())
            {
                DataRow row = dataTable.NewRow();
                row["Customer Id"] = sqlDataReader["CustomerId"];
                row["Customer Name"] = sqlDataReader["CustomerName"];
                dataTable.Rows.Add(row);
            }
            GridView1.DataSource = dataTable;
            GridView1.DataBind();
            connection.Close();
        return reader;  
    }



Hope it will helpful. Mark question as solved after trying.
--
Thanks
 
Share this answer
 
SqlDataReader objects allow you to read data in a fast forward-only manner. Call the Close method of the SqlDataReader to ensure there are not any resource leaks.

C#
private void BindGrid()
{
    string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select * from Customers";
            cmd.Connection = con;
            con.Open();
            DataGridView1.DataSource = cmd.ExecuteReader(); 
            con.Close();
        }
    }
}
 
Share this answer
 
v4
Comments
sinpulay 7-Nov-13 2:13am    
Nice..........
:)
Member 14111311 10-Jan-19 1:28am    
Thanks brother

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