SQL Connection Full

 

 SQL Connection Full




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 System.Data.SqlClient;

using System.IO;

 

namespace SQLConnection_Full

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        string imgLog = "";

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

 

                byte[] img = null;

                FileStream fs = new FileStream(imgLog, FileMode.Open, FileAccess.Read);

                BinaryReader br = new BinaryReader(fs);

                img = br.ReadBytes((int)fs.Length);

 

                SqlConnection con = new SqlConnection("Data Source=DESKTOP-31HBTKT\\SQLEXPRESS;Initial Catalog=ICTPT;Integrated Security=True");

                con.Open();

                SqlCommand cmd = new SqlCommand("insert into sturegfull values (@id,@name,@city,@dob,@gender,@l1,@l2,@l3,@pic,@regtime)", con);

                cmd.Parameters.AddWithValue("@id", textBox1.Text);

                cmd.Parameters.AddWithValue("@name", textBox2.Text);

                cmd.Parameters.AddWithValue("@city", comboBox1.Text);

                cmd.Parameters.AddWithValue("dob", dateTimePicker1.Text);

                if (radioButton1.Checked)

                {

                    cmd.Parameters.AddWithValue("@gender""Male");

                }

                else

                {

                    cmd.Parameters.AddWithValue("@gender""Female");

                }

 

                if (checkBox1.Checked)

                {

                    cmd.Parameters.AddWithValue("@l1""Tamil");

                }

                else

                {

                    cmd.Parameters.AddWithValue("@l1""tno");

                }

                if (checkBox2.Checked)

                {

                    cmd.Parameters.AddWithValue("@l2""Sinhala");

                }

                else

                {

                    cmd.Parameters.AddWithValue("@l2""sno");

                }

                if (checkBox3.Checked)

                {

                    cmd.Parameters.AddWithValue("@l3""English");

                }

                else

                {

                    cmd.Parameters.AddWithValue("@l3""eno");

                }

                cmd.Parameters.Add(new SqlParameter("@pic", img));

                cmd.Parameters.AddWithValue("@regtime"DateTime.Now.ToString());

                cmd.ExecuteNonQuery();

                MessageBox.Show("DOne");

                this.sturegfullTableAdapter.Fill(this.iCTPTDataSet.sturegfull);

            }

            catch (Exception ex)

            {

 

                MessageBox.Show(ex.Message);

            }

 

        }

 

        private void pictureBox1_Click(object sender, EventArgs e)

        {

            try

            {

                OpenFileDialog dlg = new OpenFileDialog();

 

                dlg.Filter = "Image Files(Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.tif; *.tiff; *.pdf)|*.jpg; *.jpeg; *.gif; *.bmp; *.tif; *.tiff; .*pdf";

                if (dlg.ShowDialog() == DialogResult.OK)

                {

                    imgLog = dlg.FileName.ToString();

                    pictureBox1.ImageLocation = imgLog;

                }

            }

 

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

 

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            // TODO: This line of code loads data into the 'iCTPTDataSet.sturegfull' table. You can move, or remove it, as needed.

            this.sturegfullTableAdapter.Fill(this.iCTPTDataSet.sturegfull);

 

        }

 

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            try

            {

                if (e.RowIndex >= 0)

                {

                   DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];

                   textBox1.Text = row.Cells[0].Value.ToString();

                   textBox2.Text = row.Cells[1].Value.ToString();

                   comboBox1.Text = row.Cells[2].Value.ToString();

                   dateTimePicker1.Text = row.Cells[3].Value.ToString();

                  // string a = row.Cells[4].Value.ToString();

                   if (row.Cells[4].Value.ToString() == "Male")

                    {

                        radioButton1.Checked = true;

                    }

                    else

                    {

                        radioButton2.Checked = true;

                    }

                   if (row.Cells[5].Value.ToString() == "Tamil")

                   {

                       checkBox1.Checked = true;

                   }

                   else

                   {

                       checkBox1.Checked = false;

                   }

                   if (row.Cells[6].Value.ToString() == "Sinhala")

                   {

                       checkBox2.Checked = true;

                   }

                   else

                   {

                       checkBox2.Checked = false;

                   }

                   if (row.Cells[7].Value.ToString() == "English")

                   {

                       checkBox3.Checked = true;

                   }

                   else

                   {

                       checkBox3.Checked = false;

                   }

                   byte[] img = (byte[])(row.Cells[8].Value);

                   if (img == null)

                    {

                        pictureBox1.Image = null;

                       

                    }

 

                    else

                    {

                        MemoryStream ms = new MemoryStream(img);

                        pictureBox1.Image = Image.FromStream(ms);

                      

                    }

 

                }

            }

            catch

            {

                MessageBox.Show("Please click correct Cell");

                textBox1.Text = "";

                textBox2.Text = "";

                comboBox1.Text = "";

                radioButton1.Checked= false;

                checkBox1.Checked = false;

                checkBox2.Checked = false;

                checkBox3.Checked = false;

                pictureBox1.Image = null;

              

            }

 

 

        }

 

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

 

        }

    }

}


Fetch

try

            {

                SqlConnection con = new SqlConnection("Data Source=DESKTOP-31HBTKT\\SQLEXPRESS; Initial Catalog=ICTPT;Integrated Security=True");

                con.Open();

                SqlCommand cmd = new SqlCommand("SELECT * FROM sturegfull WHERE stuid= '" + textBox1.Text + "' ", con);

                SqlDataReader read = cmd.ExecuteReader();

                while (read.Read())

                {

 

                    textBox2.Text = (string)read[1];

                    comboBox1.Text = (string)read[2];

 

                    dateTimePicker1.Text = (string)read[3];

                    // string a = row.Cells[4].Value.ToString();

                    if (read[4].ToString() == "Male")

                    {

                        radioButton1.Checked = true;

                    }

                    else

                    {

                        radioButton2.Checked = true;

                    }

                    if (read[5].ToString() == "Tamil")

                    {

                        checkBox1.Checked = true;

                    }

                    else

                    {

                        checkBox1.Checked = false;

                    }

                    if (read[6].ToString() == "Sinhala")

                    {

                        checkBox2.Checked = true;

                    }

                    else

                    {

                        checkBox2.Checked = false;

                    }

                    if (read[7].ToString() == "English")

                    {

                        checkBox3.Checked = true;

                    }

                    else

                    {

                        checkBox3.Checked = false;

                    }

                    byte[] img = (byte[])(read[8]);

                    if (img == null)

                    {

                        pictureBox1.Image = null;

 

                    }

 

                    else

                    {

                        MemoryStream ms = new MemoryStream(img);

                        pictureBox1.Image = Image.FromStream(ms);

 

                    }

                                       

                }

 

                read.Close();

            }

            catch (Exception ex)

            {

 

                MessageBox.Show(ex.Message);

            }

 

Post a Comment

0 Comments