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.Data.SqlClient;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
SqlConnection cs = new SqlConnection("Data Source=newdell2017; Initial Catalog=Pocono20; Integrated Security=TRUE");
/*cs.Open();
MessageBox.Show(cs.State.ToString());
cs.Close();*/
SqlDataAdapter da = new SqlDataAdapter();
BindingSource tblNamesBS = new BindingSource();
public Form1()
{
InitializeComponent();
}
private void BtnAdd_Click(object sender, EventArgs e)
{
da.InsertCommand = new SqlCommand("INSERT into tblContacts VALUES(@FIRSTNAME, @LASTNAME, @Truck)", cs);
da.InsertCommand.Parameters.Add("FIRSTNAME", SqlDbType.VarChar).Value = txtFirstName.Text;
da.InsertCommand.Parameters.Add("LASTNAME", SqlDbType.VarChar).Value = txtLastName.Text;
da.InsertCommand.Parameters.Add("Truck", SqlDbType.VarChar).Value = txtTruck.Text;
//da.InsertCommand.Parameters.Add("StartDate", SqlDbType.Date).Value = Start_Date.Text;
cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();
}
private void BtnDisplay_Click(object sender, EventArgs e)
{
da.SelectCommand = new SqlCommand("Select * From TblContacts", cs);
ds.Clear();
da.Fill(ds);
dg.DataSource = ds.Tables[0];
tblNamesBS.DataSource = ds.Tables[0];
txtFirstName.DataBindings.Add(new Binding("Text", tblNamesBS, "FirstName"));
txtLastName.DataBindings.Add(new Binding("Text", tblNamesBS, "LastName"));
txtTruck.DataBindings.Add(new Binding("Text", tblNamesBS, "Truck"));
records();
}
private void BtnNext_Click(object sender, EventArgs e)
{
tblNamesBS.MoveNext();
dgUpdate();
records();
}
private void BtnPrevious_Click(object sender, EventArgs e)
{
tblNamesBS.MovePrevious();
dgUpdate();
records();
}
/*private void button2_Click(object sender, EventArgs e)
{
tblNamesBS.MoveLast();
dgUpdate();
records();
}*/
private void BtnFirst_Click(object sender, EventArgs e)
{
tblNamesBS.MoveFirst();
dgUpdate();
records();
}
private void dgUpdate()
{
dg.ClearSelection();
dg.Rows[tblNamesBS.Position].Selected = true;
records();
}
private void records()
{
label3.Text = "Records " + tblNamesBS.Position + " of " + (tblNamesBS.Count - 1);
}
private void BtnUpdate_Click(object sender, EventArgs e)
{
da.UpdateCommand = new SqlCommand("UPDATE tblContacts SET FIRSTNAME =@FIRSTNAME, LASTNAME =@LASTNAME WHERE ID =@ID, Truck =@Truck", cs);
da.UpdateCommand.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = txtFirstName.Text;
da.UpdateCommand.Parameters.Add("@LASTNAME", SqlDbType.VarChar).Value = txtLastName.Text;
da.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables[0].Rows[tblNamesBS.Position][0];
//da.UpdateCommand.Parameters.Add("@Truck", SqlDbType.VarChar).Value = txtTruck.Text;
cs.Open();
int x = da.UpdateCommand.ExecuteNonQuery();
cs.Close();
if (x >= 1)
MessageBox.Show("Record has been updated");
}
private void BtnDelete_Click(object sender, EventArgs e)
{
DialogResult dr;
dr = MessageBox.Show("Are You Sure?\nThere is NO Undo once data has been DELETED", "CONFIRM Deletion", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes)
{
da.DeleteCommand = new SqlCommand("DELETE FROM tblContacts WHERE ID = @ID", cs);
da.DeleteCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ds.Tables[0].Rows[tblNamesBS.Position][0];
cs.Open();
da.DeleteCommand.ExecuteNonQuery();
cs.Close();
ds.Clear();
da.Fill(ds);
}
else
{
MessageBox.Show("Deletion Canceled");
}
}
private void txtLastName_TextChanged(object sender, EventArgs e)
{
}
//private void btnChangeForm_Click(object sender, EventArgs e)
//{
// Form2 f2 = new Form2();
//this.Hide();
//f2.ShowDialog();
//}
}
}