WFC# P2: Load All Data From Products Table Provide Addition Column With Name "Requireitems", The Column Is Readonly
WFC# P2: Load All Data From Products Table Provide Addition Column With Name "Requireitems", The Column Is Readonly
// 1. Create connection
lstLog.Items.Insert(0, "1. Create connection");
SqlConnection conn = new SqlConnection("Server=.;database=Northwind;uid=sa;pwd=sa;");
if (conn.State == ConnectionState.Closed)
conn.Open();
// 2. Create command, adapter
lstLog.Items.Insert(0, "2. Create command, adapter");
SqlCommand sCommand = new SqlCommand("Select * from Products",conn);
SqlDataAdapter adap = new SqlDataAdapter();
adap.SelectCommand = sCommand;
// 3. Fill data to DataSet
lstLog.Items.Insert(0, "3. Fill data to DataSet");
DataSet dsPro = new DataSet();
adap.Fill(dsPro, "Products");
DataColumn col = new DataColumn("RequireItems");
col.ReadOnly = true;
col.Expression = "UnitsOnOrder - UnitsInStock";
dsPro.Tables[0].Columns.Add(col);
// 4. Filer data on your application, display Product can't sell
lstLog.Items.Insert(0, "4. Filer data on your application, display Product can't
sell");
DataView dView = new DataView(dsPro.Tables[0]);
dView.RowFilter = "UnitsInStock < UnitsOnOrder";
// 5. Display data by gridview component
lstLog.Items.Insert(0, "5. Display data by gridview component");
dGr.DataSource = dView;
CUSC
2012
Page 1
WFC# P2
Load all data from Employees table
1.
2.
3.
4.
using
using
using
using
using
using
using
using
using
namespace ManipulatingData
{
public partial class Frm_DataDetected : Form
{
DataSet dsPro;
SqlDataAdapter adap;
public Frm_DataDetected()
{
InitializeComponent();
}
private void Frm_DataDetected_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Server=.;database=Northwind;uid=sa;pwd=sa;");
if (conn.State == ConnectionState.Closed)
CUSC
2012
Page 2
WFC# P2
conn.Open();
SqlCommand sCommand = new SqlCommand("Select * from Employees", conn);
adap = new SqlDataAdapter();
adap.SelectCommand = sCommand;
SqlCommand iCommand = new SqlCommand("Insert into Employees(FirstName, LastName,
Birthdate) values(@FirstName, @LastName, @Birthdate)", conn);
iCommand.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.VarChar, 50,
"FirstName"));
iCommand.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar, 50,
"LastName"));
iCommand.Parameters.Add(new SqlParameter("@Birthdate", SqlDbType.DateTime, 8,
"Birthdate"));
adap.InsertCommand = iCommand;
dsPro = new DataSet();
adap.Fill(dsPro, "Employees");
dsPro.Tables[0].Columns["EmployeeID"].AutoIncrement = true;
dsPro.Tables[0].Columns["EmployeeID"].AutoIncrementSeed =
Convert.ToInt32(dsPro.Tables[0].Rows[dsPro.Tables[0].Rows.Count - 1]["EmployeeID"]) + 1;
dsPro.Tables[0].Columns["EmployeeID"].AutoIncrementStep = 1;
DataView dView = new DataView(dsPro.Tables[0]);
dataGridView1.DataSource = dView;
dsPro.Tables[0].WriteXml("Employees.xml");
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "")
{
DataRow row = dsPro.Tables[0].NewRow();
row["FirstName"] = textBox1.Text;
row["LastName"] = textBox2.Text;
row["BirthDate"] = dateTimePicker1.Value;
dsPro.Tables[0].Rows.Add(row);
}
else
{
MessageBox.Show("Please input employee name !", "Errors", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void button1_Click(object sender, EventArgs e)
{
dsPro.Tables[0].RejectChanges();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
SqlCommandBuilder com = new SqlCommandBuilder(adap);
adap.Update(dsPro.Tables[0]);
}
private void button2_Click(object sender, EventArgs e)
{
int add = 0;
int edit = 0;
int delete = 0;
foreach (DataRow row in dsPro.Tables[0].Rows)
{
if (row.RowState == DataRowState.Added)
CUSC
2012
Page 3
WFC# P2
add++;
if (row.RowState == DataRowState.Modified)
edit++;
if (row.RowState == DataRowState.Deleted)
delete++;
}
MessageBox.Show("We have " + add + " added rows, " + edit + " rows edited and " +
delete + " rows was deleted!", "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
CUSC
2012
Page 4