Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

getting data from database unto datagridview

$
0
0

Hello there

I saved data into sql server 2008 from my windows form application. but know i want to retrieve some columns from the table in my database to be displayed in a datagridview on a button click this is what i have

        private void findBtn_Click(object sender, EventArgs e)
        {
            // loading data from the database
            string connectionString = @"Data Source=JEREMIAH-HP\sqlexpress;Initial Catalog=CourseInfo;    
                                                       Integrated Security=True";
            SqlConnection sqlCon = new SqlConnection(connectionString);
            sqlCon.Open();
            string commandString = "select * from STUDINFO_TBL where MATRICNO='" + entmatno.Text + "'";
            SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
            SqlDataReader read = sqlCmd.ExecuteReader();
            while (read.Read())
            {
                fName.Text = read["FIRST_NAME"].ToString();  
                lName.Text = read["LAST_NAME"].ToString();
                mNo.Text = read["MATRICNO"].ToString();
                Dept.Text = read["DEPT"].ToString();
                level.Text = read["LEVEL"].ToString();
                cgpa.Text = read["CGPA"].ToString();
            }

            {
                string command = "select * from COURSEINFO_TBL where MATRICNO='" + entmatno.Text + "'";
                SqlCommand Cmd = new SqlCommand(command, sqlCon);
                SqlDataReader reread = Cmd.ExecuteReader();
                while (reread.Read())
                {
                    //Create a data table to hold the retrieved data.
                    DataTable dataTable = new DataTable();

                    //Load the data from SqlDataReader into the data table.
                    dataTable.Load(reread);

                    //Display the data from the datatable in the datagridview.
                    this.dgvresult.DataSource = dataTable;
                
                }
            }
        }     


please help the data is not displaying on my datagridview

Viewing all articles
Browse latest Browse all 2535

Trending Articles