2011-09-30 50 views
0

我在C#是一個初學者,所以它不是鼓勵我看到你對我的基本問題否決...味精BOX概率在C#

這是一個按鈕在消息框中的結果顯示我的從我的BD中選擇。

我得到的錯誤:參數「2」:無法從「對象」到「串」, 參數轉換成「3」:無法從「對象」轉換爲「System.Windows.Forms.MessageBoxButtons」

感謝幫助我!

private void Button1Click(object sender, EventArgs e) 
    { 
     thisConnection.Open(); 
     SqlCommand thisCommand = thisConnection.CreateCommand(); 
     thisCommand.CommandText = "Select id_Client,numéro_Teléphone from Clients"; 

     SqlDataReader thisreader = thisCommand.ExecuteReader(); 

     while (thisreader.Read()) 
     { 
      MessageBox.Show(thisreader["id_Client"],thisreader["numéro_Teléphone"]); 
     } 
     thisreader.Close(); 
     thisConnection.Close(); 



    } 
+0

不知道爲什麼這是獲取DV'd。他先試了一下,然後發佈了他的代碼和錯誤消息。不能要求別的。 :) –

回答

2

嘗試,

MessageBox.Show(thisreader["id_Client"].ToString() + " " + 
     thisreader["numéro_Teléphone"].ToString()); 

MSDN文檔瞭解更多詳情。

編輯:

使用System.Text.StringBuilder來追加字符串。

System.Text.StringBuilder sb=new System.Text.StringBuilder(); 

while (thisreader.Read()) 
{ 
    sb.Append("\n" + thisreader["id_Client"].ToString() + " " + 
      thisreader["numéro_Teléphone"].ToString()); 

    //or 
    //sb.Append(string.Format("\n{0} {1}",thisreader["id_Client"],thisreader["numéro_Teléphone"]));  
} 
MessageBox.Show(sb.ToString()); 
+0

錯誤'System.Windows.Forms.MessageBox.Show(string,string)'的最佳重載方法匹配有一些無效參數 – FrankSharp

+0

錯誤'WindowsFormsApplication5.Form1'不包含' textBox1_TextChanged'並且沒有擴展方法'textBox1_TextChanged'接受'WindowsFormsApplication5.Form1'類型的第一個參數可以找到(你是否缺少using指令或程序集引用?) – FrankSharp

+0

同樣的錯誤它的網絡 – FrankSharp

2

夫婦的事情,我會在這裏添加。

我不認爲呼叫

thisreader["id_Client"].ToString() 

是DBNull的安全,這意味着如果實際值從數據庫返回的是NULL,這可能會導致問題。其次,在用戶點擊所有MessageBox之前,您的數據庫服務器連接將不會被關閉。這可能不是預期的行爲。我幾乎總是用一個DataTable和一個SqlDataAdapter來填充它,這樣我就知道我已經把所有的數據從服務器中取出,並且我的連接已經關閉,所以我沒有在服務器上佔用額外的資源。

也是另一種東西,因爲你是新來的C#你

thisreader.Close(); 
thisConnection.Close(); 

可能不會得到執行,你需要或者做

的try/catch /終於

或者一個

使用()

聲明。只是給你一些最佳實踐的東西來思考。