Tuesday, May 10, 2016

Find the data of the column in SQL database on the basis of one column

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;
namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
        private void Form1_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from emp", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bool b = false;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i][0].ToString().Trim() == textBox1.Text.Trim())
                {
                    textBox2.Text = ds.Tables[0].Rows[i][1].ToString();
                    textBox3.Text = ds.Tables[0].Rows[i][2].ToString();
                    b = true;
                    break;
                }
            }
            if (!b)
            {
                MessageBox.Show("Id does not exist");
            }
        }
    }
}

No comments:

Post a Comment