Write a code to retrieve data from table into a datagridview in CSharp

create table phone ( 
id int primary key identity(1,1),
firstname varchar(50) not null,
lastname varchar(50) not null,
mobileno varchar(25) not null,
emailid varchar(200) not null );
SqlConnection con = new SqlConnection(@"Data Source=.\;Initial Catalog=blogger;Integrated Security=True");
private void button2_Click(object sender, EventArgs e) 
{
con.Open();
SqlCommand cm=new SqlCommand("INSERT INTO phone (firstname,lastname,mobileno,emailid) VALUES('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"', '"+textBox4.Text+"')",con);
cm.ExecuteNonQuery();
con.Close();
MessageBox.Show("data insert successfully...!");
}
private void button3_Click(object sender, EventArgs e) 
{
con.Open();
SqlCommand cm=new SqlCommand("DELETE FROM phone WHERE (mobileno='"+textBox3.Text+"')",con);
cm.ExecuteNonQuery();
con.Close();
MessageBox.Show("delete successfully...!");
display();
}
private void button4_Click(object sender, EventArgs e) 
{
con.Open();
SqlCommand cm = new SqlCommand("UPDATE phone SET firstname='"+textBox1.Text+"', lastname='"+textBox2.Text+"',mobileno='"+textBox3.Text+"',emailid='"+textBox4.Text+"' WHERE (emailid='"+textBox4.Text+"')", con); cm.ExecuteNonQuery();
con.Close();
MessageBox.Show("update successfully...!");
display();
}
void display() 
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM phone",con); System.Data.DataTable dt = new System.Data.DataTable(); sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach(DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = item[1].ToString(); dataGridView1.Rows[n].Cells[1].Value = item[2].ToString(); dataGridView1.Rows[n].Cells[2].Value = item[3].ToString(); dataGridView1.Rows[n].Cells[3].Value = item[4].ToString();
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e) 
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); textBox4.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace tele
{
public partial class Form1 : Form
{
public Form1()
{ InitializeComponent();
}
SqlConnection con = new SqlConnection(@"Data Source=.\;Initial Catalog=blogger;Integrated Security=True"); private void Form1_Load(object sender, EventArgs e)
{
display();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Clear();
textBox3.Text = "";
textBox4.Clear();
textBox1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cm=new SqlCommand("INSERT INTO phone (firstname,lastname,mobileno,emailid) VALUES('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"', '"+textBox4.Text+"')",con);
cm.ExecuteNonQuery();
con.Close();
MessageBox.Show("data insert successfully...!");
}
void display()
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM phone",con); System.Data.DataTable dt = new System.Data.DataTable(); sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach(DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add(); dataGridView1.Rows[n].Cells[0].Value = item[1].ToString(); dataGridView1.Rows[n].Cells[1].Value = item[2].ToString(); dataGridView1.Rows[n].Cells[2].Value = item[3].ToString(); dataGridView1.Rows[n].Cells[3].Value = item[4].ToString(); }
}
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); textBox4.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cm=new SqlCommand("DELETE FROM phone WHERE (mobileno='"+textBox3.Text+"')",con);
cm.ExecuteNonQuery();
con.Close();
MessageBox.Show("delete successfully...!");
display();
}
private void button4_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cm = new SqlCommand("UPDATE phone SET firstname='"+textBox1.Text+"', lastname='"+textBox2.Text+"',mobileno='"+textBox3.Text+"',emailid='"+textBox4.Text+"' WHERE (emailid='"+textBox4.Text+"')", con); cm.ExecuteNonQuery();
con.Close();
MessageBox.Show("update successfully...!");
display();
}
}
}

--

--

--

DailyAspirants is one website for all begginers to learn website design and coding

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
DailyAspirants

DailyAspirants

DailyAspirants is one website for all begginers to learn website design and coding

More from Medium

Working with XML Using C#

C# Programming — Variables (Lesson 01)

Improve Code Quality with BannedSymbolAnalyzers

Debugging 101 in Visual Studio 2022