Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我怎样才能在我只想查找记录并想在列表框中显示它的地方添加搜索按钮代码_C#_Mysql_Database - Fatal编程技术网

C# 我怎样才能在我只想查找记录并想在列表框中显示它的地方添加搜索按钮代码

C# 我怎样才能在我只想查找记录并想在列表框中显示它的地方添加搜索按钮代码,c#,mysql,database,C#,Mysql,Database,我是C#新手,我正在做一个简单的数据库项目,我想在其中添加一个功能,如果用户输入身份号码并单击按钮,所有记录都将显示在列表框中。。。请帮我完成它。。。我要添加的代码位于按钮2\u单击中代码的最后一部分 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System

我是C#新手,我正在做一个简单的数据库项目,我想在其中添加一个功能,如果用户输入身份号码并单击按钮,所有记录都将显示在列表框中。。。请帮我完成它。。。我要添加的代码位于
按钮2\u单击
中代码的最后一部分

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 main_form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=windowDb;Integrated Security=True;Pooling=False");
        SqlCommand command = new SqlCommand();
        //SqlDataReader dataSearch;

        private void Form1_Load(object sender, EventArgs e) {}

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e) {}

        private void btn_save_Click(object sender, EventArgs e)
        {
            if(id_Txt.Text!=""& f_txt.Text!=""& Lname_txt.Text!=""& m_txt.Text!=""& dateTimePicker1.Text!=""){
            command.Connection = con;
            con.Open();

            command.CommandText = "insert into people(id,fname,lname,mobile,TDATE) values('"+id_Txt.Text+"','"+f_txt.Text+"','"+Lname_txt.Text+"','"+m_txt.Text+"','"+dateTimePicker1.Text/*Value.ToString()*/+"')";
            command.ExecuteNonQuery();
            con.Close();

            MessageBox.Show("Data Saved Successfully");
            id_Txt.Clear();
            f_txt.Clear();
            Lname_txt.Clear();
            m_txt.Clear();
            dateTimePicker1.Value = DateTime.Now;
            search();
            }
        }
        private void search() {

            listBox1.Items.Clear();
            listBox2.Items.Clear();
            listBox3.Items.Clear();
            listBox4.Items.Clear();
            listBox5.Items.Clear();
            con.Open();
            command.CommandText = "select * from people";
            command.Connection = con;

            SqlDataReader dataSearch = command.ExecuteReader();


            if(dataSearch.HasRows){
                while (dataSearch.Read())
                {
                    listBox1.Items.Add(dataSearch["id"].ToString());
                    listBox2.Items.Add(dataSearch["fname"].ToString());
                    listBox3.Items.Add(dataSearch["lname"].ToString());
                    listBox4.Items.Add(dataSearch["mobile"].ToString());
                    listBox5.Items.Add(dataSearch["TDATE"].ToString());
                }
                }

          con.Close();
        }

        private void btn_view_Click(object sender, EventArgs e)
        {
            search();
        }

        private void bn_update_Click(object sender, EventArgs e)
        {
            if (id_Txt.Text != "" & f_txt.Text != "" & Lname_txt.Text != "" & m_txt.Text != "" & dateTimePicker1.Text != "")
            {
                command.Connection = con;
                con.Open();
                command.CommandText = "update people  set fname='" + f_txt.Text + "',lname='" + Lname_txt.Text + "',mobile='" + m_txt.Text + "',TDATE='" + dateTimePicker1.Text + "' where id='" +id_Txt.Text + "'";
                command.ExecuteNonQuery();
                con.Close();

                MessageBox.Show("Data updated Successfully");
                id_Txt.Clear();
                f_txt.Clear();
                Lname_txt.Clear();
                m_txt.Clear();
                dateTimePicker1.Value = DateTime.Now;
                search();
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox list = sender as ListBox;
            if(list.SelectedIndex!=-1)
            {
                listBox1.SelectedIndex = list.SelectedIndex;
                listBox2.SelectedIndex = list.SelectedIndex;
                listBox3.SelectedIndex = list.SelectedIndex;
                listBox4.SelectedIndex = list.SelectedIndex;
                listBox5.SelectedIndex = list.SelectedIndex;

             //   retrive to TextBox of AllowDrop events;
               /* id_Txt.Text = listBox1.SelectedIndex.ToString();
                f_txt.Text = listBox2.SelectedIndex.ToString();
                Lname_txt.Text = listBox3.SelectedIndex.ToString();
                m_txt.Text = listBox4.SelectedIndex.ToString();
                dateTimePicker1.Value = DateTime.Now;*/

                id_Txt.Text = listBox1.SelectedItem.ToString();
                f_txt.Text = listBox2.SelectedItem.ToString();
                Lname_txt.Text = listBox3.SelectedItem.ToString();
                m_txt.Text = listBox4.SelectedItem.ToString();
                dateTimePicker1.Value = DateTime.Now;
            }
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (id_Txt.Text != "")
            {
                command.Connection = con;
                con.Open();
                command.CommandText = "delete from people where id='" + id_Txt.Text + "'";
                command.ExecuteNonQuery();
                con.Close();

                MessageBox.Show("Data deleted Successfully");
                id_Txt.Clear();
                f_txt.Clear();
                Lname_txt.Clear();
                m_txt.Clear();
                dateTimePicker1.Value = DateTime.Now;
                search();
            }

        }

        private void label1_Click(object sender, EventArgs e) {}

        private void button1_Click(object sender, EventArgs e) {}

        private void button2_Click(object sender, EventArgs e)
        {
            if (text_search.Text == "")
            {
                MessageBox.Show("Enter the Rollno.");
            }
            else {

            }
        }
    }
}