Click here to Skip to main content
15,913,280 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello!
I'm trying to retrieve a radioButton value from database from SQL Server Compact
But my code doesn't work.. what is wrong?
thanks

C#
if (con.State != ConnectionState.Open)
    con.Open();

Boolean genderval = false;
SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_info WHERE Std_id='" + TextBox1.Text + "'", con);
genderval = Convert.ToBoolean(gendercmnd);
            if (genderval == true)
                Convert.ToBoolean(radioButton1.Checked);
            else
                Convert.ToBoolean(radioButton2.Checked);  
con.Close();   
Posted

If you are getting True or False value in genderval variable then assign value directly to the radio button like given below -

C#
if(genderval==true)
{
radioButton1.Checked=genderval;
}
 
Share this answer
 
Comments
Member 10417511 25-Nov-13 17:55pm    
I tried but the same error message shows at line: genderval = Convert.ToBoolean(gendercmnd);
error:{"Unable to cast object of type 'System.Data.SqlServerCe.SqlCeCommand' to type 'System.IConvertible'."}
Madhu Nair 26-Nov-13 5:24am    
You are not executing your command object and you are directly trying to access the columns from the command object. It is not possible. Assign the command object execution to datareader using ExecuteReader()Method and access the value from the data reader.


Refer this link http://www.akadia.com/services/dotnet_data_reader.html for more info on using Data Reader
It is solved..!
C#
//........ retrieving radioButton value ....... 

if (conn.State != ConnectionState.Open) 
    conn.Open(); 
try { 
      Boolean genderval = SqlCeCommand gendercmnd = new SqlCeCommand(@"SELECT Gender FROM Std_Info     WHERE Std_id='" + TextBox1.Text + "'", con); 

genderval = (Boolean)gendercmnd.ExecuteScalar(); 
if (genderval == true) 
    Convert.ToBoolean(radioButton1.Checked=true); 
else 
    Convert.ToBoolean(radioButton2.Checked = true); 
conn.Close(); 
} 
catch (Exception ex)
       {
             MessageBox.Show(ex.Message);
       }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900