c#填充到文本框中的SQL搜索按钮单击事件

时间:2016-06-08 14:08:15

标签: c# sql-server

我正在尝试映射SQL数据库查询,以通过搜索按钮上的单击事件从文本框中提取查询,并在同一表单上的其他文本框中显示结果。

但现在我被卡住了。我要么没有结果,要么得到未处理的异常。我一直在谷歌和SO上下,我不知道下一步该去哪里。

这是我到目前为止所做的:

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;
using System.Windows;


namespace EmployeeEvaluation
{
public partial class frmOpenRead : Form
{
    public frmOpenRead()
    {
        InitializeComponent();
    }

    private void label5_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        // QUIT BUTTON
        Close();
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        // THIS IS THE SEARCH BUTTON
        string conString = @"Data Source=SQL1;Initial Catalog=EE;Integrated Security=True";
        SqlConnection sc = new SqlConnection(conString);

        string selectSql = "SELECT *  FROM   tbl_TextBoxes   where   [Ename]  =  @Ename";
        SqlCommand com = new SqlCommand(selectSql, sc);

        com.Parameters.AddWithValue("@Ename", txtBoxSearch.Text);

        try
        {
            sc.Open();

            using (SqlDataReader read = com.ExecuteReader())
            {
                while (read.Read())
                {
                    textBox1.Text = (read["Ename"].ToString());
                    textBox2.Text = (read["Sname"].ToString());
                    textBox4.Text = (read["PositionUpdateBox"].ToString());
                    textBox5.Text = (read["PerFac1"].ToString());
                    textBox7.Text = (read["PerFac2"].ToString());
                    textBox10.Text = (read["PerFac3"].ToString());
                    textBox11.Text = (read["PerFac4"].ToString());
                    textBox12.Text = (read["PerFac5"].ToString());
                    textBox16.Text = (read["PerFac6"].ToString());
                    textBox17.Text = (read["PerFac7"].ToString());
                    textBox18.Text = (read["PerFac8"].ToString());
                    textBox22.Text = (read["PerFac9"].ToString());
                    textBox24.Text = (read["PerFac10"].ToString());
                    textBox25.Text = (read["PerFac11"].ToString());
                    textBox28.Text = (read["PerFac12"].ToString());
                    textBox30.Text = (read["PerFac13"].ToString());
                    textBox3.Text = (read["EvalDate"].ToString());
                    textBox6.Text = (read["Rating1"].ToString());
                    textBox8.Text = (read["Rating2"].ToString());
                    textBox13.Text = (read["Rating3"].ToString());
                    textBox14.Text = (read["Rating4"].ToString());
                    textBox15.Text = (read["Rating5"].ToString());
                    textBox19.Text = (read["Rating6"].ToString());
                    textBox20.Text = (read["Rating7"].ToString());
                    textBox21.Text = (read["Rating8"].ToString());
                    textBox23.Text = (read["Rating9"].ToString());
                    textBox26.Text = (read["Rating10"].ToString());
                    textBox27.Text = (read["Rating11"].ToString());
                    textBox29.Text = (read["Rating12"].ToString());
                    textBox31.Text = (read["Rating13"].ToString());
                }
            }
        }
        finally
        {
            sc.Close();
        }

    }

    private void frmOpenRead_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'eEDataSetTextBoxes.tbl_TextBoxes' table. You can move, or remove it, as needed.
        this.tbl_TextBoxesTableAdapter.Fill(this.eEDataSetTextBoxes.tbl_TextBoxes);

    }

    private void txtBoxSearch_Enter(object sender, EventArgs e)
    {
        txtBoxSearch.Text = String.Empty;
    }

    private void textBox8_TextChanged(object sender, EventArgs e)
    {

    }

    private void label4_Click(object sender, EventArgs e)
    {

    }

    private void startOverButton_Click(object sender, EventArgs e)
    {
        // START OVER BUTTON CLEARS CONTENT OF ALL TEXT BOXES
        textBox1.Text = String.Empty;
        textBox2.Text = String.Empty;
        textBox4.Text = String.Empty;
        textBox5.Text = String.Empty;
        textBox7.Text = String.Empty;
        textBox10.Text = String.Empty;
        textBox11.Text = String.Empty;
        textBox12.Text = String.Empty;
        textBox16.Text = String.Empty;
        textBox17.Text = String.Empty;
        textBox18.Text = String.Empty;
        textBox22.Text = String.Empty;
        textBox24.Text = String.Empty;
        textBox25.Text = String.Empty;
        textBox28.Text = String.Empty;
        textBox30.Text = String.Empty;
        textBox3.Text = String.Empty;
        textBox6.Text = String.Empty;
        textBox8.Text = String.Empty;
        textBox13.Text = String.Empty;
        textBox14.Text = String.Empty;
        textBox15.Text = String.Empty;
        textBox19.Text = String.Empty;
        textBox20.Text = String.Empty;
        textBox21.Text = String.Empty;
        textBox23.Text = String.Empty;
        textBox26.Text = String.Empty;
        textBox27.Text = String.Empty;
        textBox29.Text = String.Empty;
        textBox31.Text = String.Empty;
    }

        private void txtBoxSearch_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

除了c#w / SQL上的一些youtube视频之外,以下是我在制作目前为止所做的一些地方:

Get data from sql query into textbox https://msdn.microsoft.com/en-us/library/aa984467(v=vs.71).aspx

任何人都可以告诉我我在哪里出错,或者指向一两个可能帮助我的资源吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

X成为查询结果中包含null的列。因此read["X"]也将是null。我们不应该将null转换为空字符串。所以你需要做的是,改变如下的赋值语句:

textBoxN.Text = read["X"]==null||read["X"]==DBNull?"":read["X"].ToString();

答案 1 :(得分:0)

在粘贴到此处之前,我在我的一个sql字符串上更改了== to =,这就停止了异常。到目前为止,当我这样做时,结果也是按钮点击没有做任何事情。

我现在已经上下谷歌一周了,甚至放弃了一两天。我发誓我不会发布到SO,我会自己解决这个问题。然后我崩溃并发布到SO,再次运行我的代码,现在它工作。是的我是新手。实际上是第一篇文章。所以我想这个问题已经解决,但我不能说我改变了什么,只是给它时间?

感谢大家的回复。