Saturday, September 2, 2017

C# curd read portion

In our previous lesson, You learn that how to insert data into sql server by c# windows application. In this lesson, we learn how to view that data.

First add button view and DataGridView from Toolbox, that you learn from the previous lesson.













Then click button view and paste this code.Where we call a function name display. 

   private void button2_Click(object sender, EventArgs e)
        {
          display();
        }

Then write function and paste the below code.

        private void display()
         {
          con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from yy";

            cmd.ExecuteNonQuery();
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
         }   


Then we save the code.Now click on start for execute the code.click view to show the data it very easy and simple but you have to follow the first lesson.















If you like this article please share.

No comments:

Post a Comment