C# Code For Beginners (Part 1)
1. Add item to Textbox from Textbox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
int code1 = int.Parse(tbcode1.Text);
tbcode2.Text=code1.ToString();
string name1= tbname1.Text;
tbname2.Text = name1;
}
}
}
2. Add item to Datagridview from Datagridview
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
int n = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (dataGridView1.Rows.Count != n + 1)
{
dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = row.Cells[0].Value.ToString();
dataGridView2.Rows[n].Cells[1].Value = row.Cells[1].Value.ToString();
}
n += 1;
}
}
}
}
3. Add item to Datagridview from Textbox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n = 0;
private void btnadd_Click(object sender, EventArgs e)
{
{
dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = tbcode.Text;
dataGridView1.Rows[n].Cells[1].Value = tbname.Text;
}
n += 1;
}
}
}
4. Add item to Textbox from Datagridview
i.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n = 0;
private void btnadd_Click(object sender, EventArgs e)
{
if (n >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[n];
tbcode.Text = row.Cells[0].Value.ToString();
tbname.Text = row.Cells[1].Value.ToString();
}
n += 1;
}
}
}
ii.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
tbcode.Text = row.Cells[0].Value.ToString();
tbname.Text = row.Cells[1].Value.ToString();
}
}
}
}
5.Add data to Database from Textbox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
string Conn1 = "server=localhost;user id=root;password=;database=blog";
string Query = "insert into example(Item_Code,Item_Name) values('" + this.tbcode.Text + "','" + this.tbname
.Text + "');";
MySqlConnection Conn2 = new MySqlConnection(Conn1);
MySqlCommand Command = new MySqlCommand(Query, Conn2);
MySqlDataReader Reader;
Conn2.Open();
Reader = Command.ExecuteReader();
MessageBox.Show("Item added to the Database successfully!");
while (Reader.Read())
{
}
Conn2.Close();
}
}
}
6.Add data to Database from Datagridview
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
MySqlConnection MyConn2 = new MySqlConnection("server=localhost;user id=root;password=;database=blog");
MyConn2.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
MySqlCommand MyCommand2 = new MySqlCommand("INSERT INTO example(Item_Code,Item_Name) VALUES('" + dataGridView1.Rows[i].Cells["I_Code"].Value + "','" + dataGridView1.Rows[i].Cells["I_Name"].Value + "')", MyConn2);
MyCommand2.ExecuteNonQuery();
}
MessageBox.Show("Item Add Successfully..!");
MyConn2.Close();
}
}
}
7.Add data to Textbox from Database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnsrch_Click(object sender, EventArgs e)
{
string Conn1 = "server=localhost;user id=root;password=;database=blog";
string Query = "(Select Item_Name From example Where Item_Code Like '" + tbcode.Text + "');";
MySqlConnection Conn2 = new MySqlConnection(Conn1);
MySqlCommand Command = new MySqlCommand(Query, Conn2);
MySqlDataReader Reader;
Conn2.Open();
Reader = Command.ExecuteReader();
while (Reader.Read())
{
tbname.Text = Reader.GetString("Item_Name");
}
Conn2.Close();
}
}
}
8.Add data to Datagridview from Database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnsrch_Click(object sender, EventArgs e)
{
string Conn1 = "server=localhost;user id=root;password=;database=blog";
string Query = "(Select Item_Code as 'Code',Item_Name as 'Name' From example Where Item_Code Like '" + tbcode.Text + "');";
MySqlConnection Conn2 = new MySqlConnection(Conn1);
MySqlCommand Command = new MySqlCommand(Query, Conn2);
MySqlDataReader Reader;
Conn2.Open();
Reader = Command.ExecuteReader();
while (Reader.Read())
{
}
Conn2.Close();
MySqlDataAdapter Adapter = new MySqlDataAdapter();
Adapter.SelectCommand = Command;
DataTable dTable = new DataTable();
Adapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
int code1 = int.Parse(tbcode1.Text);
tbcode2.Text=code1.ToString();
string name1= tbname1.Text;
tbname2.Text = name1;
}
}
}
output
2. Add item to Datagridview from Datagridview
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
int n = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (dataGridView1.Rows.Count != n + 1)
{
dataGridView2.Rows.Add();
dataGridView2.Rows[n].Cells[0].Value = row.Cells[0].Value.ToString();
dataGridView2.Rows[n].Cells[1].Value = row.Cells[1].Value.ToString();
}
n += 1;
}
}
}
}
output
3. Add item to Datagridview from Textbox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n = 0;
private void btnadd_Click(object sender, EventArgs e)
{
{
dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = tbcode.Text;
dataGridView1.Rows[n].Cells[1].Value = tbname.Text;
}
n += 1;
}
}
}
output
4. Add item to Textbox from Datagridview
i.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int n = 0;
private void btnadd_Click(object sender, EventArgs e)
{
if (n >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[n];
tbcode.Text = row.Cells[0].Value.ToString();
tbname.Text = row.Cells[1].Value.ToString();
}
n += 1;
}
}
}
output
ii.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
tbcode.Text = row.Cells[0].Value.ToString();
tbname.Text = row.Cells[1].Value.ToString();
}
}
}
}
output
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
string Conn1 = "server=localhost;user id=root;password=;database=blog";
string Query = "insert into example(Item_Code,Item_Name) values('" + this.tbcode.Text + "','" + this.tbname
.Text + "');";
MySqlConnection Conn2 = new MySqlConnection(Conn1);
MySqlCommand Command = new MySqlCommand(Query, Conn2);
MySqlDataReader Reader;
Conn2.Open();
Reader = Command.ExecuteReader();
MessageBox.Show("Item added to the Database successfully!");
while (Reader.Read())
{
}
Conn2.Close();
}
}
}
output
6.Add data to Database from Datagridview
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
MySqlConnection MyConn2 = new MySqlConnection("server=localhost;user id=root;password=;database=blog");
MyConn2.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
MySqlCommand MyCommand2 = new MySqlCommand("INSERT INTO example(Item_Code,Item_Name) VALUES('" + dataGridView1.Rows[i].Cells["I_Code"].Value + "','" + dataGridView1.Rows[i].Cells["I_Name"].Value + "')", MyConn2);
MyCommand2.ExecuteNonQuery();
}
MessageBox.Show("Item Add Successfully..!");
MyConn2.Close();
}
}
}
output
7.Add data to Textbox from Database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnsrch_Click(object sender, EventArgs e)
{
string Conn1 = "server=localhost;user id=root;password=;database=blog";
string Query = "(Select Item_Name From example Where Item_Code Like '" + tbcode.Text + "');";
MySqlConnection Conn2 = new MySqlConnection(Conn1);
MySqlCommand Command = new MySqlCommand(Query, Conn2);
MySqlDataReader Reader;
Conn2.Open();
Reader = Command.ExecuteReader();
while (Reader.Read())
{
tbname.Text = Reader.GetString("Item_Name");
}
Conn2.Close();
}
}
}
output
8.Add data to Datagridview from Database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Blog1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnsrch_Click(object sender, EventArgs e)
{
string Conn1 = "server=localhost;user id=root;password=;database=blog";
string Query = "(Select Item_Code as 'Code',Item_Name as 'Name' From example Where Item_Code Like '" + tbcode.Text + "');";
MySqlConnection Conn2 = new MySqlConnection(Conn1);
MySqlCommand Command = new MySqlCommand(Query, Conn2);
MySqlDataReader Reader;
Conn2.Open();
Reader = Command.ExecuteReader();
while (Reader.Read())
{
}
Conn2.Close();
MySqlDataAdapter Adapter = new MySqlDataAdapter();
Adapter.SelectCommand = Command;
DataTable dTable = new DataTable();
Adapter.Fill(dTable);
dataGridView1.DataSource = dTable;
}
}
}
output
Comments
Post a Comment