2012-07-26 49 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

相关问题