using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CrudContact
{
public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
{
IF (!IsPostBack)
{
btnDelete.Enabled = false;
GrillView(); //panggil GrillView, kodenya ada dibawah, klo belum dibuat dihapus dulu aja biar ga error
}
}
//Kita Buat untuk Button Clear dulu yaaah
protected void btnClear(object sender, EventArgs e)
{
Clear();
}
private void Clear()
{
hvContactID.Value = "";
txtAddress.Text = txtMobile.Text = txtName.Text = "";
lblerror.Text = lblsuccess.Text ="";
btnDelete.Enabled = false;
btnSave.Text = "Save";
}
//Nah sudah kita buat button Clear, bisa kita coba jalankan pilih Browser anda kalau saya pakai google chrome
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ViewAll", sqlCon); //jika memanggil gunakan SqlDataAdapter untuk mengambil data pada query "ViewAll"
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure; //untuk panggil letak query pada folder StoredProcedure
DataTable dtbl = new DataTable(); //membuat variabel data tabel
sqlDa.Fill(dtbl);
gvContact.DataSource = dtbl; //untuk mengisi / melihat data pada grid view
gvContact.DataBind();
}
// Kan ada kolom Action View, Nah ini kita panggil Idnya "lnkView_Click" supaya nanti tampil di desaign bisa kita hapus atau update
protected void lnkView_Click(object sender, EventArgs e) //untuk memanggil action view berdasarkan ID
{
int ContactID = Convert.ToInt32((sender as LinkButton).CommandArgument );
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("ViewByID", sqlCon);
sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
sqlDa.SelectCommand.Parameters.AddWithValue("@ContactID", ContactID);
DataTable dtbl = new DataTable();
sqlDa.Fill(dtbl);
sqlCon.Close();
hvContactID.Value = ContactID.ToString(); //untuk menampilkan pada designn
txtName.Text = dtbl.Rows[0]["Name"].ToString();
txtMobile.Text = dtbl.Rows[0]["Mobile"].ToString();
txtAddress.Text = dtbl.Rows[0]["Address"].ToString();
GrillView();
btnDelete.Enabled = true;
btnSave.Text = "Update";
}
//Terakhir niiih buat button Delete
protected void btnDelete(object sender, EventArgs e)
{
if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("DeleteById", sqlCon);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("ContactID",Convert.ToInt32(hvContactID.Value));
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
GrillView();
lblsuccess.Text = "Delete Sukses";
Clear();
}
}
//....................Silahkan coba di run, semoga berhasil yaaa...
Semoga bermanfaat..
Sampai jumpa di tutorial selanjutnyaa...
Wassalamualaikum..