2012-07-26 85 views
-4

我想將整數值存儲在標籤中並傳遞給另一個表單。但價值獲得通過,但它通過system.data.dataset。以下是我的代碼。請幫我解決一下這個。從數據庫中獲取數據並傳遞給另一個表單

con.Open(); 
SqlCommand cmd1 = new SqlCommand("select balance from customer where namee='" + textBox1.Text + "'", con); 
SqlDataAdapter adapter = new SqlDataAdapter(cmd1); 
DataSet ds = new DataSet(); 
adapter.Fill(ds, "loki"); 
label4.Text = ds.ToString(); 
options frm = new options(label4.Text); 
frm.Show(); 
con.Close(); 
+1

有什麼問題爲int? – 2012-07-26 08:06:36

+0

將TextBox的*修飾符*設置爲* public *並以第二種形式寫入* code *。 – adatapost 2012-07-26 08:07:43

+0

你已經分配了System.Data.Dataset標籤並將其傳遞給另一個表單 – 2012-07-26 08:07:59

回答

0

嘗試

con.Open(); 
SqlCommand cmd1 = new SqlCommand("select balance from customer where namee='" 
         + textBox1.Text + "'", con); 
label4.Text = cmd1.ExecuteScalar(); 
1

你想得到'標量'值而不是數據集。

con.Open(); 
SqlCommand cmd1 = new SqlCommand("select balance from customer where namee='" 
         + textBox1.Text + "'", con); 
object balance = cmd.ExecuteScalar(); 
label4.Text = balance.ToString(); 

options frm = new options(label4.Text); 
frm.Show(); 
con.Close(); 
+0

對象是什麼? – user1526527 2012-07-26 08:32:08

+0

con.Open(); SqlCommand cmd1 = new SqlCommand(「select customer from where where namee ='」+ textBox1.Text +「'」,con); object balance = cmd.ExecuteScalar(); label4.Text = balance.ToString(); options frm = new options(label4.Text); frm.Show(); con.Close(); – user1526527 2012-07-26 08:39:25

+0

無法正常工作............... – user1526527 2012-07-26 08:40:39

2

你將不得不在label.Text通過平衡字段值

取而代之的是

label4.Text = ds.ToString(); 

你必須做這樣

label4.Text = ds.Tables[0].Rows["balance"].ToString(); 

請注意,因爲它將作爲字符串存儲在標籤的文本屬性0123中所以當你檢索出來的另一種形式你 必須把它轉換爲使用Int32.TryParse

+0

出現錯誤,無法將字符串轉換爲int並且system.data.datarowcollection.this [此]有一些無效參數 – user1526527 2012-07-26 08:21:22

+0

@ user1526527請請向我展示完整的例外情況以及代碼行數 – HatSoft 2012-07-26 08:23:17

+0

代碼未在此 – user1526527 2012-07-26 08:33:51

相關問題