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 Retrieve_data_from_Sql_server_database
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection("Data Source=.;Initial Catalog=db1;Integrated Security=true");
conn1.Open();
SqlCommand cmd1 = new SqlCommand("Select firstname,lastname,phone,gender,date_birth,image1 From Table1 Where id=@idpar", conn1);
cmd1.Parameters.AddWithValue("idpar", idtxt.Text.Trim());
SqlDataReader reader1;
reader1 = cmd1.ExecuteReader();
if (reader1.Read())
{
ftxt.Text = reader1["firstname"].ToString();
ltxt.Text = reader1["lastname"].ToString();
phtxt.Text = reader1["phone"].ToString();
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "MM-dd-yyyy";
dateTimePicker1.Text = reader1["date_birth"].ToString();
pictureBox1.Image = Image.FromFile(reader1["image1"].ToString());
Boolean gender_v = (bool)reader1["gender"];
if (gender_v==true)
{
m1.Checked = true;
}
else
{
f1.Checked = true;
}
}
else
{
ftxt.Text = "";
ltxt.Text = "";
phtxt.Text = "";
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
pictureBox1.Image = null;
m1.Checked = false;
f1.Checked = false;
MessageBox.Show("No data found");
}
conn1.Close();
}
private void browse_btn_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Image|*.jpg;png";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
else
pictureBox1.Image = null;
}
private void update_btn_Click(object sender, EventArgs e)
{
SqlConnection conn2 = new SqlConnection("Data Source=.;Initial Catalog=db1;Integrated Security=true");
SqlCommand cmd2 = new SqlCommand("Update Table1 Set firstname=@fname,lastname=@lname,phone=@phone,gender=@gender,image1=@image,date_birth=@dateb Where id=@id", conn2);
cmd2.Parameters.AddWithValue("fname", ftxt.Text.Trim());
cmd2.Parameters.AddWithValue("lname", ltxt.Text.Trim());
cmd2.Parameters.AddWithValue("phone", phtxt.Text.Trim());
int gg;
if (m1.Checked==true)
{
gg = 1;
}
else
{
gg = 0;
}
cmd2.Parameters.AddWithValue("gender", gg);
cmd2.Parameters.AddWithValue("image", openFileDialog1.FileName);
cmd2.Parameters.AddWithValue("dateb", dateTimePicker1.Value);
cmd2.Parameters.AddWithValue("id", idtxt.Text.Trim());
conn2.Open();
cmd2.ExecuteNonQuery();
conn2.Close();
MessageBox.Show("Data updated successfully");
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you want to delete this row?", "Delete row", MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)
{
SqlConnection conn3 = new SqlConnection("Data Source=.;Initial Catalog=db1;Integrated Security=True");
SqlCommand cmd3 = new SqlCommand("Delete From Table1 Where id=@id", conn3);
cmd3.Parameters.AddWithValue("id", idtxt.Text.Trim());
conn3.Open();
cmd3.ExecuteNonQuery();
conn3.Close();
idtxt.Text = "";
ftxt.Text = "";
ltxt.Text = "";
phtxt.Text = "";
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";
pictureBox1.Image = null;
m1.Checked = false;
f1.Checked = false;
MessageBox.Show("ID deleted Successfully");
}
}
}
}