2014-01-18 13 views
0

通过这个代码,我从数据库编辑表格以便在检索值:做一个单选按钮被查取决于值在数据库

OleDbCommand Comm1 = new OleDbCommand("select image1,image2,image3,image4,measurement,property_purpose,bedrooms,bathrooms,furnishing,property_price,property_price_per_mu,existing_customer from tb_property where property_id = ?", con); 
Comm1.Parameters.AddWithValue("property_id", txt_prop_id.Text); 
OleDbDataReader DR1 = Comm1.ExecuteReader(); 
if (DR1.Read()) 
     { 
      txt_image1.Text = DR1.GetValue(0).ToString(); 
      txt_image2.Text = DR1.GetValue(1).ToString(); 
      txt_image3.Text = DR1.GetValue(2).ToString(); 
      txt_image4.Text = DR1.GetValue(3).ToString(); 
      combo_measure.Text = DR1.GetValue(4).ToString(); 
      combo_purpose.Text = DR1.GetValue(5).ToString(); 

      combo_bedrooms.Text = DR1.GetValue(6).ToString(); 
      combo_bathrooms.Text = DR1.GetValue(7).ToString(); 
      combo_furnishing.Text = DR1.GetValue(8).ToString(); 
      txt_price.Text = DR1.GetValue(9).ToString(); 
      txt_price_per_mu.Text = DR1.GetValue(10).ToString(); 
      var val = DR1.GetValue(11).ToString(); 
      if (val == "Yes") 
      { 
       radioButton1.Checked; 
      } 
      if (val == "No") 
      { 
       radioButton2.Checked; 
      } 
     } 

radiobuttons如果valYes现在我有麻烦那么应该检查数据库radiobutton1

如果No在数据库中,则应选择radiobutton2。但语法是一个显示错误,任何人都可以请帮助我吗?

回答

4

语法设置单选按钮的检查属性为

radioButton1.Checked = true; 

所以我们的代码看起来像

  if (val == "Yes") 
      { 
       radioButton1.Checked=true; 
       radioButton2.Checked=false; 
      } 
      else if (val == "No") 
      { 
       radioButton2.Checked=true; 
       radioButton1.Checked=false; 
      } 
+0

thanku ..sir it workrd – user3181292

+0

两个if语句没有意义使用'else if' –

+0

@SriramSakthivel thanx更新! –

1

只需更新单选按钮的Checked属性:

radioButton1.Checked = (val == "Yes); 
    radioButton2.Checked = !radioButton1.Checked; 
0

尼廷Varpe的答案是伟大的,但还有一件事我会改善你的代码和我小号索引列通过它的

把这个代码类:

public static class DataExtensions 
    { 
    public static string GetSafeString(this OleDbDataReader reader, string colName) 
    { 

     if (reader[colName] != DBNull.Value) 
      return reader[colName].ToString(); 
     else 
      return string.Empty; 
    } 
} 

所以,当你打电话的价值会是这样的:

con.Open(); 
OleDbDataReader DR1 = Comm1.ExecuteReader(); 

if (DR1.Read()) 
{ 

    textBox1.Text = (DataExtensions.GetSafeString(DR1, "COLUMN")); 
    var val = (DataExtensions.GetSafeString(DR1, "COLUMN")); 

    if (val == "Yes") 
    { 
     radioButton1.Checked; 
    } 
    if (val == "No") 
    { 
     radioButton2.Checked; 
    } 
} 

con.Close(); 

索引列可能会导致搞乱了整个代码时改变表格结构。 希望它有一点帮助。

相关问题