Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a form with data gridview which has 6 labled columns. i want to display data from
sql db onto only 3 of the labled columns. i tried but only the first row in the db gets displayed
on the datagridview. please help me out
below are the codes i used

public void loadproduct3()
        {
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
            conn.ConnectionString = "Data Source=USER-PC;Initial Catalog=BARDB;User ID=sa;Password=mike";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            string sqlQueryy = null;
            sqlQueryy = "select * from tblproduct";
            cmd.Connection = conn;
            cmd.CommandText = sqlQueryy;
            cmd.CommandType = System.Data.CommandType.Text;
            SqlDataReader dr = null;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                for (int i = 0; i < dgvSales.Rows.Count; i++)
                {
                    dgvSales.Rows[i].Cells["clmProductid"].Value = dr["productid"].ToString();
                    dgvSales.Rows[i].Cells["clmProductname"].Value = dr["productname"].ToString();
                    dgvSales.Rows[i].Cells["clmUnitprice"].Value = dr["unitprice"].ToString();
                }
            }
            else
            {
                //return false;
            }
            conn.Close();
        }
Posted
Updated 19-Apr-17 0:52am
v2
Comments
[no name] 3-Aug-13 15:22pm    
"only the row in the db gets displayed", you are only reading the data from your datareader once so all you are going to get is one row.
mikeoabban 3-Aug-13 15:35pm    
please how do i read the dtatbase multiple times
[no name] 3-Aug-13 15:42pm    
You don't. You read the data out of your reader, once for each row. Try replacing your "if" with a "while".
mikeoabban 3-Aug-13 15:47pm    
i tried and only displayed the last row in the db
[no name] 4-Aug-13 6:51am    
Then you did something wrong is all I can tell you.

Use the below:

C#
public void loadproduct3()
    {
        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        conn.ConnectionString = "Data Source=USER-PC;Initial Catalog=BARDB;User ID=sa;Password=mike";


        SqlDataAdapter oSDA = new SqlDataAdapter("select * from tblproduct", conn);
        DataSet oDs = new DataSet();
        oSDA.SelectCommand.CommandType = CommandType.Text;
        oSDA.Fill(oDs);

        if (oDs.Tables[0].Rows.Count > 0)
        {
            dgvSales.DataSource = oDs;
            dgvSales.DataBind();
        }
    }
 
Share this answer
 
mike I think you should use first
while dr.read() before ... for .... to loop
which will end with end while
 
Share this answer
 

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