2016-06-08 121 views
-1

我試圖映射一個SQL數據庫查詢,以通過搜索按鈕上的單擊事件從文本框中提取查詢,並在其他文本框中顯示結果相同的形式。c#SQL搜索按鈕點擊事件,填充到文本框

但現在我卡住了。我要麼沒有結果,要麼沒有處理異常。我一直在谷歌和谷歌,我不知道下一步該怎麼做。

這是我到目前爲止有:

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#中的一些YouTube視頻瓦特/ SQL我已經看過第一:

​​ https://msdn.microsoft.com/en-us/library/aa984467(v=vs.71).aspx

誰能告訴我在哪裏,我已經錯在這裏,或點我朝着資源或兩個,可以幫助我嗎?

謝謝!

+0

如果您遇到異常,您應該點擊[Copy exception details to clipboard](https://blogs.msdn.microsoft.com/saraford/2008/08/07/did-you-know-you-可以通過一次點擊從異常助理276 /複製異常詳細信息)並將信息粘貼到此處。如果您不告訴我們您遇到了什麼錯誤,我們無法幫助您。 –

+0

這裏發生錯誤嗎?確保有數據返回。 textBox6.Text =(read [「Rating1」]。ToString());例如應該工作,除非查詢結果爲空 –

+0

哪條代碼行拋出錯誤? – Pushpendra

回答

1

X成爲在查詢結果中具有null的列。所以read["X"]也將是null。我們不應該將null轉換爲空字符串。所以你需要做的是,改變賦值語句如下所示:

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

SqlDataReader將永遠不會包含null,並且DBNull.ToString()默認返回空。我懷疑這是個問題。 –

0

我改變==來=我的SQL字符串的一個粘貼這裏之前,並已停止例外。直到現在,當我這樣做時,結果也是按鈕點擊什麼都沒做。

我一直在上下谷歌一個星期,甚至放棄了一兩天。我發誓我不會發布到SO,我會自己弄清楚。然後我分手併發布到SO,再次運行我的代碼,現在它工作。是的,我是一個新手。首先在這裏發帖。所以我猜這個問題已經解決,但我不能說我改變了任何東西,只是給了它時間?

感謝大家的回覆。